]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/server_child.c
Merge master
[netatalk.git] / libatalk / util / server_child.c
1 /*
2  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
3  * All rights reserved. See COPYRIGHT.
4  *
5  *
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.
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <signal.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <sys/time.h>
27
28 #include <atalk/logger.h>
29 #include <atalk/errchk.h>
30 #include <atalk/util.h>
31 #include <atalk/server_child.h>
32
33 #ifndef WEXITSTATUS
34 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
35 #endif /* ! WEXITSTATUS */
36 #ifndef WIFEXITED
37 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
38 #endif /* ! WIFEXITED */
39 #ifndef WIFSTOPPED
40 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
41 #endif
42 #ifndef WIFSIGNALED
43 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
44 #endif
45 #ifndef WTERMSIG
46 #define WTERMSIG(status)      ((status) & 0x7f)
47 #endif
48
49 /* hash/child functions: hash OR's pid */
50 #define CHILD_HASHSIZE 32
51 #define HASH(i) ((((i) >> 8) ^ (i)) & (CHILD_HASHSIZE - 1))
52
53 typedef struct server_child_fork {
54     struct server_child_data *table[CHILD_HASHSIZE];
55     void (*cleanup)(const pid_t);
56 } server_child_fork;
57
58 int parent_or_child; /* 0: parent, 1: child */
59
60 static inline void hash_child(struct server_child_data **htable,
61                               struct server_child_data *child)
62 {
63     struct server_child_data **table;
64
65     table = &htable[HASH(child->pid)];
66     if ((child->next = *table) != NULL)
67         (*table)->prevp = &child->next;
68     *table = child;
69     child->prevp = table;
70 }
71
72 static inline void unhash_child(struct server_child_data *child)
73 {
74     if (child->prevp) {
75         if (child->next)
76             child->next->prevp = child->prevp;
77         *(child->prevp) = child->next;
78     }
79 }
80
81 static struct server_child_data *resolve_child(struct server_child_data **table, pid_t pid)
82 {
83     struct server_child_data *child;
84
85     for (child = table[HASH(pid)]; child; child = child->next) {
86         if (child->pid == pid)
87             break;
88     }
89
90     return child;
91 }
92
93 /* initialize server_child structure */
94 server_child *server_child_alloc(const int connections, const int nforks)
95 {
96     server_child *children;
97
98     children = (server_child *) calloc(1, sizeof(server_child));
99     if (!children)
100         return NULL;
101
102     children->nsessions = connections;
103     children->nforks = nforks;
104     children->fork = (void *) calloc(nforks, sizeof(server_child_fork));
105
106     if (!children->fork) {
107         free(children);
108         return NULL;
109     }
110
111     return children;
112 }
113
114 /*!
115  * add a child
116  * @return pointer to struct server_child_data on success, NULL on error
117  */
118 afp_child_t *server_child_add(server_child *children, int forkid, pid_t pid, uint ipc_fds[2])
119 {
120     server_child_fork *fork;
121     afp_child_t *child = NULL;
122     sigset_t sig, oldsig;
123
124     /* we need to prevent deletions from occuring before we get a
125      * chance to add the child in. */
126     sigemptyset(&sig);
127     sigaddset(&sig, SIGCHLD);
128     pthread_sigmask(SIG_BLOCK, &sig, &oldsig);
129
130     /* it's possible that the child could have already died before the
131      * pthread_sigmask. we need to check for this. */
132     if (kill(pid, 0) < 0)
133         goto exit;
134
135     fork = (server_child_fork *) children->fork + forkid;
136
137     /* if we already have an entry. just return. */
138     if (child = resolve_child(fork->table, pid))
139         goto exit;
140
141     if ((child = calloc(1, sizeof(afp_child_t))) == NULL)
142         goto exit;
143
144     child->pid = pid;
145     child->valid = 0;
146     child->killed = 0;
147     child->ipc_fds[0] = ipc_fds[0];
148     child->ipc_fds[1] = ipc_fds[1];
149
150     hash_child(fork->table, child);
151     children->count++;
152
153 exit:
154     pthread_sigmask(SIG_SETMASK, &oldsig, NULL);
155     return child;
156 }
157
158 /* remove a child and free it */
159 int server_child_remove(server_child *children, const int forkid, pid_t pid)
160 {
161     int fd;
162     server_child_fork *fork;
163     struct server_child_data *child;
164
165     fork = (server_child_fork *) children->fork + forkid;
166     if (!(child = resolve_child(fork->table, pid)))
167         return -1;
168
169     unhash_child(child);
170     if (child->clientid) {
171         free(child->clientid);
172         child->clientid = NULL;
173     }
174
175     /* In main:child_handler() we need the fd in order to remove it from the pollfd set */
176     fd = child->ipc_fds[0];
177     if (child->ipc_fds[0] != -1) {
178         close(child->ipc_fds[0]);
179         child->ipc_fds[0] = -1;
180     }
181     if (child->ipc_fds[1] != -1) {
182         close(child->ipc_fds[1]);
183         child->ipc_fds[1] = -1;
184     }
185
186     free(child);
187     children->count--;
188
189     if (fork->cleanup)
190         fork->cleanup(pid);
191
192     return fd;
193 }
194
195 /* free everything: by using a hash table, this increases the cost of
196  * this part over a linked list by the size of the hash table */
197 void server_child_free(server_child *children)
198 {
199     server_child_fork *fork;
200     struct server_child_data *child, *tmp;
201     int i, j;
202
203     for (i = 0; i < children->nforks; i++) {
204         fork = (server_child_fork *) children->fork + i;
205         for (j = 0; j < CHILD_HASHSIZE; j++) {
206             child = fork->table[j]; /* start at the beginning */
207             while (child) {
208                 tmp = child->next;
209                 if (child->clientid) {
210                     free(child->clientid);
211                 }
212                 free(child);
213                 child = tmp;
214             }
215         }
216     }
217     free(children->fork);
218     free(children);
219 }
220
221 /* send signal to all child processes */
222 void server_child_kill(server_child *children, int forkid, int sig)
223 {
224     server_child_fork *fork;
225     struct server_child_data *child, *tmp;
226     int i;
227
228     fork = (server_child_fork *) children->fork + forkid;
229     for (i = 0; i < CHILD_HASHSIZE; i++) {
230         child = fork->table[i];
231         while (child) {
232             tmp = child->next;
233             kill(child->pid, sig);
234             child = tmp;
235         }
236     }
237 }
238
239 /* send kill to a child processes.
240  * a plain-old linked list
241  * FIXME use resolve_child ?
242  */
243 static int kill_child(struct server_child_data *child)
244 {
245     if (!child->killed) {
246         kill(child->pid, SIGTERM);
247         /* we don't wait because there's no guarantee that we can really kill it */
248         child->killed = 1;
249         return 1;
250     } else {
251         LOG(log_info, logtype_default, "Unresponsive child[%d], sending SIGKILL", child->pid);
252         kill(child->pid, SIGKILL);
253     }
254     return 1;
255 }
256
257 /*!
258  * Try to find an old session and pass socket
259  * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
260  */
261 int server_child_transfer_session(server_child *children,
262                                   int forkid,
263                                   pid_t pid,
264                                   uid_t uid,
265                                   int afp_socket,
266                                   uint16_t DSI_requestID)
267 {
268     EC_INIT;
269     server_child_fork *fork;
270     struct server_child_data *child;
271     int i;
272
273     fork = (server_child_fork *) children->fork + forkid;
274     if ((child = resolve_child(fork->table, pid)) == NULL) {
275         LOG(log_note, logtype_default, "Reconnect: no child[%u]", pid);
276         return 0;
277     }
278
279     if (!child->valid) {
280         /* hmm, client 'guess' the pid, rogue? */
281         LOG(log_error, logtype_default, "Reconnect: invalidated child[%u]", pid);
282         return 0;
283     } else if (child->uid != uid) {
284         LOG(log_error, logtype_default, "Reconnect: child[%u] not the same user", pid);
285         return 0;
286     }
287
288     LOG(log_note, logtype_default, "Reconnect: transfering session to child[%u]", pid);
289     
290     if (writet(child->ipc_fds[0], &DSI_requestID, 2, 0, 2) != 2) {
291         LOG(log_error, logtype_default, "Reconnect: error sending DSI id to child[%u]", pid);
292         EC_STATUS(-1);
293         goto EC_CLEANUP;
294     }
295     EC_ZERO_LOG(send_fd(child->ipc_fds[0], afp_socket));
296     EC_ZERO_LOG(kill(pid, SIGURG));
297
298     EC_STATUS(1);
299
300 EC_CLEANUP:
301     EC_EXIT;
302 }
303
304
305 /* see if there is a process for the same mac     */
306 /* if the times don't match mac has been rebooted */
307 void server_child_kill_one_by_id(server_child *children, int forkid, pid_t pid,
308                                  uid_t uid, uint32_t idlen, char *id, uint32_t boottime)
309 {
310     server_child_fork *fork;
311     struct server_child_data *child, *tmp;
312     int i;
313
314     fork = (server_child_fork *)children->fork + forkid;
315
316     for (i = 0; i < CHILD_HASHSIZE; i++) {
317         child = fork->table[i];
318         while (child) {
319             tmp = child->next;
320             if ( child->pid != pid) {
321                 if (child->idlen == idlen && memcmp(child->clientid, id, idlen) == 0) {
322                     if ( child->time != boottime ) {
323                         /* Client rebooted */
324                         if (uid == child->uid) {
325                             kill_child(child);
326                             LOG(log_warning, logtype_default,
327                                 "Terminated disconnected child[%u], client rebooted.",
328                                 child->pid);
329                         } else {
330                             LOG(log_warning, logtype_default,
331                                 "Session with different pid[%u]", child->pid);
332                         }
333                     } else {
334                         /* One client with multiple sessions */
335                         LOG(log_debug, logtype_default,
336                             "Found another session[%u] for client[%u]", child->pid, pid);
337                     }
338                 }
339             } else {
340                 /* update childs own slot */
341                 child->time = boottime;
342                 if (child->clientid)
343                     free(child->clientid);
344                 LOG(log_debug, logtype_default, "Setting client ID for %u", child->pid);
345                 child->uid = uid;
346                 child->valid = 1;
347                 child->idlen = idlen;
348                 child->clientid = id;
349             }
350             child = tmp;
351         }
352     }
353 }
354
355 /* for extra cleanup if necessary */
356 void server_child_setup(server_child *children, const int forkid,
357                         void (*fcn)(const pid_t))
358 {
359     server_child_fork *fork;
360
361     fork = (server_child_fork *) children->fork + forkid;
362     fork->cleanup = fcn;
363 }
364
365
366 /* ---------------------------
367  * reset children signals
368  */
369 void server_reset_signal(void)
370 {
371     struct sigaction    sv;
372     sigset_t            sigs;
373     const struct itimerval none = {{0, 0}, {0, 0}};
374
375     setitimer(ITIMER_REAL, &none, NULL);
376     memset(&sv, 0, sizeof(sv));
377     sv.sa_handler =  SIG_DFL;
378     sigemptyset( &sv.sa_mask );
379
380     sigaction(SIGALRM, &sv, NULL );
381     sigaction(SIGHUP,  &sv, NULL );
382     sigaction(SIGTERM, &sv, NULL );
383     sigaction(SIGUSR1, &sv, NULL );
384     sigaction(SIGCHLD, &sv, NULL );
385
386     sigemptyset(&sigs);
387     sigaddset(&sigs, SIGALRM);
388     sigaddset(&sigs, SIGHUP);
389     sigaddset(&sigs, SIGUSR1);
390     sigaddset(&sigs, SIGCHLD);
391     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
392
393 }