2 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
3 * All rights reserved. See COPYRIGHT.
6 * handle inserting, removing, and freeing of children.
7 * this does it via a hash table. it incurs some overhead over
8 * a linear append/remove in total removal and kills, but it makes
9 * single-entry removals a fast operation. as total removals occur during
10 * child initialization and kills during server shutdown, this is
11 * probably a win for a lot of connections and unimportant for a small
12 * number of connections.
17 #endif /* HAVE_CONFIG_H */
23 #endif /* HAVE_UNISTD_H */
27 /* POSIX.1 sys/wait.h check */
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_WAIT_H
31 #endif /* HAVE_SYS_WAIT_H */
34 #include <atalk/logger.h>
35 #include <atalk/errchk.h>
36 #include <atalk/util.h>
37 #include <atalk/server_child.h>
40 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
41 #endif /* ! WEXITSTATUS */
43 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
44 #endif /* ! WIFEXITED */
46 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
49 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
52 #define WTERMSIG(status) ((status) & 0x7f)
55 /* hash/child functions: hash OR's pid */
56 #define CHILD_HASHSIZE 32
57 #define HASH(i) ((((i) >> 8) ^ (i)) & (CHILD_HASHSIZE - 1))
59 typedef struct server_child_fork {
60 struct server_child_data *table[CHILD_HASHSIZE];
61 void (*cleanup)(const pid_t);
64 int parent_or_child; /* 0: parent, 1: child */
66 static inline void hash_child(struct server_child_data **htable,
67 struct server_child_data *child)
69 struct server_child_data **table;
71 table = &htable[HASH(child->pid)];
72 if ((child->next = *table) != NULL)
73 (*table)->prevp = &child->next;
78 static inline void unhash_child(struct server_child_data *child)
82 child->next->prevp = child->prevp;
83 *(child->prevp) = child->next;
87 static struct server_child_data *resolve_child(struct server_child_data **table, pid_t pid)
89 struct server_child_data *child;
91 for (child = table[HASH(pid)]; child; child = child->next) {
92 if (child->pid == pid)
99 /* initialize server_child structure */
100 server_child *server_child_alloc(const int connections, const int nforks)
102 server_child *children;
104 children = (server_child *) calloc(1, sizeof(server_child));
108 children->nsessions = connections;
109 children->nforks = nforks;
110 children->fork = (void *) calloc(nforks, sizeof(server_child_fork));
112 if (!children->fork) {
122 * @return pointer to struct server_child_data on success, NULL on error
124 afp_child_t *server_child_add(server_child *children, int forkid, pid_t pid, uint ipc_fds[2])
126 server_child_fork *fork;
127 afp_child_t *child = NULL;
128 sigset_t sig, oldsig;
130 /* we need to prevent deletions from occuring before we get a
131 * chance to add the child in. */
133 sigaddset(&sig, SIGCHLD);
134 pthread_sigmask(SIG_BLOCK, &sig, &oldsig);
136 /* it's possible that the child could have already died before the
137 * pthread_sigmask. we need to check for this. */
138 if (kill(pid, 0) < 0)
141 fork = (server_child_fork *) children->fork + forkid;
143 /* if we already have an entry. just return. */
144 if (child = resolve_child(fork->table, pid))
147 if ((child = calloc(1, sizeof(afp_child_t))) == NULL)
153 child->ipc_fds[0] = ipc_fds[0];
154 child->ipc_fds[1] = ipc_fds[1];
156 hash_child(fork->table, child);
160 pthread_sigmask(SIG_SETMASK, &oldsig, NULL);
164 /* remove a child and free it */
165 int server_child_remove(server_child *children, const int forkid, pid_t pid)
168 server_child_fork *fork;
169 struct server_child_data *child;
171 fork = (server_child_fork *) children->fork + forkid;
172 if (!(child = resolve_child(fork->table, pid)))
176 if (child->clientid) {
177 free(child->clientid);
178 child->clientid = NULL;
181 /* In main:child_handler() we need the fd in order to remove it from the pollfd set */
182 fd = child->ipc_fds[0];
183 if (child->ipc_fds[0] != -1) {
184 close(child->ipc_fds[0]);
185 child->ipc_fds[0] = -1;
187 if (child->ipc_fds[1] != -1) {
188 close(child->ipc_fds[1]);
189 child->ipc_fds[1] = -1;
201 /* free everything: by using a hash table, this increases the cost of
202 * this part over a linked list by the size of the hash table */
203 void server_child_free(server_child *children)
205 server_child_fork *fork;
206 struct server_child_data *child, *tmp;
209 for (i = 0; i < children->nforks; i++) {
210 fork = (server_child_fork *) children->fork + i;
211 for (j = 0; j < CHILD_HASHSIZE; j++) {
212 child = fork->table[j]; /* start at the beginning */
215 if (child->clientid) {
216 free(child->clientid);
223 free(children->fork);
227 /* send signal to all child processes */
228 void server_child_kill(server_child *children, int forkid, int sig)
230 server_child_fork *fork;
231 struct server_child_data *child, *tmp;
234 fork = (server_child_fork *) children->fork + forkid;
235 for (i = 0; i < CHILD_HASHSIZE; i++) {
236 child = fork->table[i];
239 kill(child->pid, sig);
245 /* send kill to a child processes.
246 * a plain-old linked list
247 * FIXME use resolve_child ?
249 static int kill_child(struct server_child_data *child)
251 if (!child->killed) {
252 kill(child->pid, SIGTERM);
253 /* we don't wait because there's no guarantee that we can really kill it */
257 LOG(log_info, logtype_default, "Unresponsive child[%d], sending SIGKILL", child->pid);
258 kill(child->pid, SIGKILL);
264 * Try to find an old session and pass socket
265 * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
267 int server_child_transfer_session(server_child *children,
272 uint16_t DSI_requestID)
275 server_child_fork *fork;
276 struct server_child_data *child;
279 fork = (server_child_fork *) children->fork + forkid;
280 if ((child = resolve_child(fork->table, pid)) == NULL) {
281 LOG(log_note, logtype_default, "Reconnect: no child[%u]", pid);
282 if (kill(pid, 0) == 0) {
283 LOG(log_note, logtype_default, "Reconnect: terminating old session[%u]", pid);
286 if (kill(pid, 0) == 0) {
287 LOG(log_error, logtype_default, "Reconnect: killing old session[%u]", pid);
296 /* hmm, client 'guess' the pid, rogue? */
297 LOG(log_error, logtype_default, "Reconnect: invalidated child[%u]", pid);
299 } else if (child->uid != uid) {
300 LOG(log_error, logtype_default, "Reconnect: child[%u] not the same user", pid);
304 LOG(log_note, logtype_default, "Reconnect: transfering session to child[%u]", pid);
306 if (writet(child->ipc_fds[0], &DSI_requestID, 2, 0, 2) != 2) {
307 LOG(log_error, logtype_default, "Reconnect: error sending DSI id to child[%u]", pid);
311 EC_ZERO_LOG(send_fd(child->ipc_fds[0], afp_socket));
312 EC_ZERO_LOG(kill(pid, SIGURG));
321 /* see if there is a process for the same mac */
322 /* if the times don't match mac has been rebooted */
323 void server_child_kill_one_by_id(server_child *children, int forkid, pid_t pid,
324 uid_t uid, uint32_t idlen, char *id, uint32_t boottime)
326 server_child_fork *fork;
327 struct server_child_data *child, *tmp;
330 fork = (server_child_fork *)children->fork + forkid;
332 for (i = 0; i < CHILD_HASHSIZE; i++) {
333 child = fork->table[i];
336 if ( child->pid != pid) {
337 if (child->idlen == idlen && memcmp(child->clientid, id, idlen) == 0) {
338 if ( child->time != boottime ) {
339 /* Client rebooted */
340 if (uid == child->uid) {
342 LOG(log_warning, logtype_default,
343 "Terminated disconnected child[%u], client rebooted.",
346 LOG(log_warning, logtype_default,
347 "Session with different pid[%u]", child->pid);
350 /* One client with multiple sessions */
351 LOG(log_debug, logtype_default,
352 "Found another session[%u] for client[%u]", child->pid, pid);
356 /* update childs own slot */
357 child->time = boottime;
359 free(child->clientid);
360 LOG(log_debug, logtype_default, "Setting client ID for %u", child->pid);
363 child->idlen = idlen;
364 child->clientid = id;
371 /* for extra cleanup if necessary */
372 void server_child_setup(server_child *children, const int forkid,
373 void (*fcn)(const pid_t))
375 server_child_fork *fork;
377 fork = (server_child_fork *) children->fork + forkid;
382 /* ---------------------------
383 * reset children signals
385 void server_reset_signal(void)
389 const struct itimerval none = {{0, 0}, {0, 0}};
391 setitimer(ITIMER_REAL, &none, NULL);
392 memset(&sv, 0, sizeof(sv));
393 sv.sa_handler = SIG_DFL;
394 sigemptyset( &sv.sa_mask );
396 sigaction(SIGALRM, &sv, NULL );
397 sigaction(SIGHUP, &sv, NULL );
398 sigaction(SIGTERM, &sv, NULL );
399 sigaction(SIGUSR1, &sv, NULL );
400 sigaction(SIGCHLD, &sv, NULL );
403 sigaddset(&sigs, SIGALRM);
404 sigaddset(&sigs, SIGHUP);
405 sigaddset(&sigs, SIGUSR1);
406 sigaddset(&sigs, SIGCHLD);
407 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);