aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-server/slavery.c
blob: ddbfc8c438ad640036a9b44a04b99b04e6e975c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * mds — A micro-display server
 * Copyright © 2014, 2015, 2016, 2017  Mattias Andrée (maandree@kth.se)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "slavery.h"

#include "globals.h"
#include "client.h"

#include <libmdsserver/macros.h>
#include <libmdsserver/linked-list.h>

#include <pthread.h>
#include <stdint.h>
#include <errno.h>
#include <stdio.h>


/**
 * Master function for slave threads
 * 
 * @param   data  Input data
 * @return        Outout data
 */
void *slave_loop(void *);


/**
 * Receive a full message and update open status if the client closes
 * 
 * @param   client  The client
 * @return          Zero on success, -2 on failure, otherwise -1
 */
int
fetch_message(client_t *client)
{
	int r = mds_message_read(&(client->message), client->socket_fd);

	if (!r) {
		return 0;
	} else if (r == -2) {
		eprint("corrupt message received.");
		fail_if (1);
	} else if (errno == ECONNRESET) {
		r = mds_message_read(&(client->message), client->socket_fd);
		client->open = 0;
		/* Connection closed. */
	} else if (errno != EINTR) {
		xperror(*argv);
		fail_if (1);
	}

	fail_if (r == -2);
	return r;
fail:
	return -2;
}


/**
 * Create, start and detache a slave thread
 * 
 * @param   thread    The address at where to store the thread
 * @param   slave_fd  The file descriptor of the slave's socket
 * @return            Zero on success, -1 on error, error message will have been printed
 */
int
create_slave(pthread_t *thread_slot, int slave_fd)
{
	if ((errno = pthread_create(thread_slot, NULL, slave_loop, (void *)(intptr_t)slave_fd))) {
		xperror(*argv);
		with_mutex (slave_mutex, running_slaves--;);
		fail_if (1);
	}
	if ((errno = pthread_detach(*thread_slot))) {
		xperror(*argv);
			fail_if (1);
	}
	return 0;
fail:
	return -1;
}


/**
 * Initialise a client, except for threading
 * 
 * @param   client_fd  The file descriptor of the client's socket
 * @return             The client information, `NULL` on error
 */
client_t *
initialise_client(int client_fd)
{
	ssize_t entry = LINKED_LIST_UNUSED;
	client_t *information;
	int locked = 0, saved_errno;
	size_t tmp;

	/* Create information table. */
	fail_if (xmalloc(information, 1, client_t));
	client_initialise(information);

	/* Add to list of clients. */
	fail_if ((errno = pthread_mutex_lock(&slave_mutex)));
	locked = 1;
	entry = linked_list_insert_end(&client_list, (size_t)(void *)information);
	fail_if (entry == LINKED_LIST_UNUSED);

	/* Add client to table. */
	tmp = fd_table_put(&client_map, client_fd, (size_t)(void *)information);
	fail_if (!tmp && errno);
	pthread_mutex_unlock(&slave_mutex);
	locked = 0;

	/* Fill information table. */
	information->list_entry = entry;
	information->socket_fd = client_fd;
	information->open = 1;
	fail_if (mds_message_initialise(&(information->message)));

	return information;

fail:
	saved_errno = errno;
	if (locked)
		pthread_mutex_unlock(&slave_mutex);
	free(information);
	if (entry != LINKED_LIST_UNUSED)
		with_mutex (slave_mutex, linked_list_remove(&client_list, entry););
	return errno = saved_errno, NULL;
}