]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/server_child.c
Merge 2-2
[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 static inline void hash_child(struct server_child_data **htable,
59                               struct server_child_data *child)
60 {
61     struct server_child_data **table;
62
63     table = &htable[HASH(child->pid)];
64     if ((child->next = *table) != NULL)
65         (*table)->prevp = &child->next;
66     *table = child;
67     child->prevp = table;
68 }
69
70 static inline void unhash_child(struct server_child_data *child)
71 {
72     if (child->prevp) {
73         if (child->next)
74             child->next->prevp = child->prevp;
75         *(child->prevp) = child->next;
76     }
77 }
78
79 static struct server_child_data *resolve_child(struct server_child_data **table, pid_t pid)
80 {
81     struct server_child_data *child;
82
83     for (child = table[HASH(pid)]; child; child = child->next) {
84         if (child->pid == pid)
85             break;
86     }
87
88     return child;
89 }
90
91 /* initialize server_child structure */
92 server_child *server_child_alloc(const int connections, const int nforks)
93 {
94     server_child *children;
95
96     children = (server_child *) calloc(1, sizeof(server_child));
97     if (!children)
98         return NULL;
99
100     children->nsessions = connections;
101     children->nforks = nforks;
102     children->fork = (void *) calloc(nforks, sizeof(server_child_fork));
103
104     if (!children->fork) {
105         free(children);
106         return NULL;
107     }
108
109     return children;
110 }
111
112 /*!
113  * add a child
114  * @return pointer to struct server_child_data on success, NULL on error
115  */
116 afp_child_t *server_child_add(server_child *children, int forkid, pid_t pid, uint ipc_fds[2])
117 {
118     server_child_fork *fork;
119     afp_child_t *child = NULL;
120     sigset_t sig, oldsig;
121
122     /* we need to prevent deletions from occuring before we get a
123      * chance to add the child in. */
124     sigemptyset(&sig);
125     sigaddset(&sig, SIGCHLD);
126     pthread_sigmask(SIG_BLOCK, &sig, &oldsig);
127
128     /* it's possible that the child could have already died before the
129      * pthread_sigmask. we need to check for this. */
130     if (kill(pid, 0) < 0) {
131         LOG(log_error, logtype_default, "server_child_add: no such process pid [%d]", pid);
132         goto exit;
133     }
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     pid_t pid = getpid();
203
204     for (i = 0; i < children->nforks; i++) {
205         fork = (server_child_fork *) children->fork + i;
206         for (j = 0; j < CHILD_HASHSIZE; j++) {
207             child = fork->table[j]; /* start at the beginning */
208             while (child) {
209                 tmp = child->next;
210
211                 if (child->ipc_fds[0] != -1)
212                     close(child->ipc_fds[0]);
213                 if (child->clientid) {
214                     free(child->clientid);
215                 }
216                 free(child);
217                 child = tmp;
218             }
219         }
220     }
221     free(children->fork);
222     free(children);
223 }
224
225 /* send signal to all child processes */
226 void server_child_kill(server_child *children, int forkid, int sig)
227 {
228     server_child_fork *fork;
229     struct server_child_data *child, *tmp;
230     int i;
231
232     fork = (server_child_fork *) children->fork + forkid;
233     for (i = 0; i < CHILD_HASHSIZE; i++) {
234         child = fork->table[i];
235         while (child) {
236             tmp = child->next;
237             kill(child->pid, sig);
238             child = tmp;
239         }
240     }
241 }
242
243 /* send kill to a child processes.
244  * a plain-old linked list
245  * FIXME use resolve_child ?
246  */
247 static int kill_child(struct server_child_data *child)
248 {
249     if (!child->killed) {
250         kill(child->pid, SIGTERM);
251         /* we don't wait because there's no guarantee that we can really kill it */
252         child->killed = 1;
253         return 1;
254     } else {
255         LOG(log_info, logtype_default, "Unresponsive child[%d], sending SIGKILL", child->pid);
256         kill(child->pid, SIGKILL);
257     }
258     return 1;
259 }
260
261 /*!
262  * Try to find an old session and pass socket
263  * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
264  */
265 int server_child_transfer_session(server_child *children,
266                                   int forkid,
267                                   pid_t pid,
268                                   uid_t uid,
269                                   int afp_socket,
270                                   uint16_t DSI_requestID)
271 {
272     EC_INIT;
273     server_child_fork *fork;
274     struct server_child_data *child;
275     int i;
276
277     fork = (server_child_fork *) children->fork + forkid;
278     if ((child = resolve_child(fork->table, pid)) == NULL) {
279         LOG(log_note, logtype_default, "Reconnect: no child[%u]", pid);
280         if (kill(pid, 0) == 0) {
281             LOG(log_note, logtype_default, "Reconnect: terminating old session[%u]", pid);
282             kill(pid, SIGTERM);
283             sleep(2);
284             if (kill(pid, 0) == 0) {
285                 LOG(log_error, logtype_default, "Reconnect: killing old session[%u]", pid);
286                 kill(pid, SIGKILL);
287                 sleep(2);
288             }
289         }
290         return 0;
291     }
292
293     if (!child->valid) {
294         /* hmm, client 'guess' the pid, rogue? */
295         LOG(log_error, logtype_default, "Reconnect: invalidated child[%u]", pid);
296         return 0;
297     } else if (child->uid != uid) {
298         LOG(log_error, logtype_default, "Reconnect: child[%u] not the same user", pid);
299         return 0;
300     }
301
302     LOG(log_note, logtype_default, "Reconnect: transfering session to child[%u]", pid);
303     
304     if (writet(child->ipc_fds[0], &DSI_requestID, 2, 0, 2) != 2) {
305         LOG(log_error, logtype_default, "Reconnect: error sending DSI id to child[%u]", pid);
306         EC_STATUS(-1);
307         goto EC_CLEANUP;
308     }
309     EC_ZERO_LOG(send_fd(child->ipc_fds[0], afp_socket));
310     EC_ZERO_LOG(kill(pid, SIGURG));
311
312     EC_STATUS(1);
313
314 EC_CLEANUP:
315     EC_EXIT;
316 }
317
318
319 /* see if there is a process for the same mac     */
320 /* if the times don't match mac has been rebooted */
321 void server_child_kill_one_by_id(server_child *children, int forkid, pid_t pid,
322                                  uid_t uid, uint32_t idlen, char *id, uint32_t boottime)
323 {
324     server_child_fork *fork;
325     struct server_child_data *child, *tmp;
326     int i;
327
328     fork = (server_child_fork *)children->fork + forkid;
329
330     for (i = 0; i < CHILD_HASHSIZE; i++) {
331         child = fork->table[i];
332         while (child) {
333             tmp = child->next;
334             if ( child->pid != pid) {
335                 if (child->idlen == idlen && memcmp(child->clientid, id, idlen) == 0) {
336                     if ( child->time != boottime ) {
337                         /* Client rebooted */
338                         if (uid == child->uid) {
339                             kill_child(child);
340                             LOG(log_warning, logtype_default,
341                                 "Terminated disconnected child[%u], client rebooted.",
342                                 child->pid);
343                         } else {
344                             LOG(log_warning, logtype_default,
345                                 "Session with different pid[%u]", child->pid);
346                         }
347                     } else {
348                         /* One client with multiple sessions */
349                         LOG(log_debug, logtype_default,
350                             "Found another session[%u] for client[%u]", child->pid, pid);
351                     }
352                 }
353             } else {
354                 /* update childs own slot */
355                 child->time = boottime;
356                 if (child->clientid)
357                     free(child->clientid);
358                 LOG(log_debug, logtype_default, "Setting client ID for %u", child->pid);
359                 child->uid = uid;
360                 child->valid = 1;
361                 child->idlen = idlen;
362                 child->clientid = id;
363             }
364             child = tmp;
365         }
366     }
367 }
368
369 /* for extra cleanup if necessary */
370 void server_child_setup(server_child *children, const int forkid,
371                         void (*fcn)(const pid_t))
372 {
373     server_child_fork *fork;
374
375     fork = (server_child_fork *) children->fork + forkid;
376     fork->cleanup = fcn;
377 }
378
379
380 /* ---------------------------
381  * reset children signals
382  */
383 void server_reset_signal(void)
384 {
385     struct sigaction    sv;
386     sigset_t            sigs;
387     const struct itimerval none = {{0, 0}, {0, 0}};
388
389     setitimer(ITIMER_REAL, &none, NULL);
390     memset(&sv, 0, sizeof(sv));
391     sv.sa_handler =  SIG_DFL;
392     sigemptyset( &sv.sa_mask );
393
394     sigaction(SIGALRM, &sv, NULL );
395     sigaction(SIGHUP,  &sv, NULL );
396     sigaction(SIGTERM, &sv, NULL );
397     sigaction(SIGUSR1, &sv, NULL );
398     sigaction(SIGCHLD, &sv, NULL );
399
400     sigemptyset(&sigs);
401     sigaddset(&sigs, SIGALRM);
402     sigaddset(&sigs, SIGHUP);
403     sigaddset(&sigs, SIGUSR1);
404     sigaddset(&sigs, SIGCHLD);
405     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
406
407 }