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 */
24 #include <sys/types.h>
28 #include <atalk/logger.h>
29 #include <atalk/errchk.h>
30 #include <atalk/util.h>
31 #include <atalk/server_child.h>
34 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
35 #endif /* ! WEXITSTATUS */
37 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
38 #endif /* ! WIFEXITED */
40 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
43 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
46 #define WTERMSIG(status) ((status) & 0x7f)
49 /* hash/child functions: hash OR's pid */
50 #define CHILD_HASHSIZE 32
51 #define HASH(i) ((((i) >> 8) ^ (i)) & (CHILD_HASHSIZE - 1))
53 typedef struct server_child_fork {
54 struct server_child_data *table[CHILD_HASHSIZE];
55 void (*cleanup)(const pid_t);
58 static inline void hash_child(struct server_child_data **htable,
59 struct server_child_data *child)
61 struct server_child_data **table;
63 table = &htable[HASH(child->pid)];
64 if ((child->next = *table) != NULL)
65 (*table)->prevp = &child->next;
70 static inline void unhash_child(struct server_child_data *child)
74 child->next->prevp = child->prevp;
75 *(child->prevp) = child->next;
79 static struct server_child_data *resolve_child(struct server_child_data **table, pid_t pid)
81 struct server_child_data *child;
83 for (child = table[HASH(pid)]; child; child = child->next) {
84 if (child->pid == pid)
91 /* initialize server_child structure */
92 server_child *server_child_alloc(const int connections, const int nforks)
94 server_child *children;
96 children = (server_child *) calloc(1, sizeof(server_child));
100 children->nsessions = connections;
101 children->nforks = nforks;
102 children->fork = (void *) calloc(nforks, sizeof(server_child_fork));
104 if (!children->fork) {
114 * @return pointer to struct server_child_data on success, NULL on error
116 afp_child_t *server_child_add(server_child *children, int forkid, pid_t pid, uint ipc_fds[2])
118 server_child_fork *fork;
119 afp_child_t *child = NULL;
120 sigset_t sig, oldsig;
122 /* we need to prevent deletions from occuring before we get a
123 * chance to add the child in. */
125 sigaddset(&sig, SIGCHLD);
126 pthread_sigmask(SIG_BLOCK, &sig, &oldsig);
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);
135 fork = (server_child_fork *) children->fork + forkid;
137 /* if we already have an entry. just return. */
138 if (child = resolve_child(fork->table, pid))
141 if ((child = calloc(1, sizeof(afp_child_t))) == NULL)
147 child->ipc_fds[0] = ipc_fds[0];
148 child->ipc_fds[1] = ipc_fds[1];
150 hash_child(fork->table, child);
154 pthread_sigmask(SIG_SETMASK, &oldsig, NULL);
158 /* remove a child and free it */
159 int server_child_remove(server_child *children, const int forkid, pid_t pid)
162 server_child_fork *fork;
163 struct server_child_data *child;
165 fork = (server_child_fork *) children->fork + forkid;
166 if (!(child = resolve_child(fork->table, pid)))
170 if (child->clientid) {
171 free(child->clientid);
172 child->clientid = NULL;
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;
181 if (child->ipc_fds[1] != -1) {
182 close(child->ipc_fds[1]);
183 child->ipc_fds[1] = -1;
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)
199 server_child_fork *fork;
200 struct server_child_data *child, *tmp;
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 */
209 if (child->clientid) {
210 free(child->clientid);
217 free(children->fork);
221 /* send signal to all child processes */
222 void server_child_kill(server_child *children, int forkid, int sig)
224 server_child_fork *fork;
225 struct server_child_data *child, *tmp;
228 fork = (server_child_fork *) children->fork + forkid;
229 for (i = 0; i < CHILD_HASHSIZE; i++) {
230 child = fork->table[i];
233 kill(child->pid, sig);
239 /* send kill to a child processes.
240 * a plain-old linked list
241 * FIXME use resolve_child ?
243 static int kill_child(struct server_child_data *child)
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 */
251 LOG(log_info, logtype_default, "Unresponsive child[%d], sending SIGKILL", child->pid);
252 kill(child->pid, SIGKILL);
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
261 int server_child_transfer_session(server_child *children,
266 uint16_t DSI_requestID)
269 server_child_fork *fork;
270 struct server_child_data *child;
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 if (kill(pid, 0) == 0) {
277 LOG(log_note, logtype_default, "Reconnect: terminating old session[%u]", pid);
280 if (kill(pid, 0) == 0) {
281 LOG(log_error, logtype_default, "Reconnect: killing old session[%u]", pid);
290 /* hmm, client 'guess' the pid, rogue? */
291 LOG(log_error, logtype_default, "Reconnect: invalidated child[%u]", pid);
293 } else if (child->uid != uid) {
294 LOG(log_error, logtype_default, "Reconnect: child[%u] not the same user", pid);
298 LOG(log_note, logtype_default, "Reconnect: transfering session to child[%u]", pid);
300 if (writet(child->ipc_fds[0], &DSI_requestID, 2, 0, 2) != 2) {
301 LOG(log_error, logtype_default, "Reconnect: error sending DSI id to child[%u]", pid);
305 EC_ZERO_LOG(send_fd(child->ipc_fds[0], afp_socket));
306 EC_ZERO_LOG(kill(pid, SIGURG));
315 /* see if there is a process for the same mac */
316 /* if the times don't match mac has been rebooted */
317 void server_child_kill_one_by_id(server_child *children, int forkid, pid_t pid,
318 uid_t uid, uint32_t idlen, char *id, uint32_t boottime)
320 server_child_fork *fork;
321 struct server_child_data *child, *tmp;
324 fork = (server_child_fork *)children->fork + forkid;
326 for (i = 0; i < CHILD_HASHSIZE; i++) {
327 child = fork->table[i];
330 if ( child->pid != pid) {
331 if (child->idlen == idlen && memcmp(child->clientid, id, idlen) == 0) {
332 if ( child->time != boottime ) {
333 /* Client rebooted */
334 if (uid == child->uid) {
336 LOG(log_warning, logtype_default,
337 "Terminated disconnected child[%u], client rebooted.",
340 LOG(log_warning, logtype_default,
341 "Session with different pid[%u]", child->pid);
344 /* One client with multiple sessions */
345 LOG(log_debug, logtype_default,
346 "Found another session[%u] for client[%u]", child->pid, pid);
350 /* update childs own slot */
351 child->time = boottime;
353 free(child->clientid);
354 LOG(log_debug, logtype_default, "Setting client ID for %u", child->pid);
357 child->idlen = idlen;
358 child->clientid = id;
365 /* for extra cleanup if necessary */
366 void server_child_setup(server_child *children, const int forkid,
367 void (*fcn)(const pid_t))
369 server_child_fork *fork;
371 fork = (server_child_fork *) children->fork + forkid;
376 /* ---------------------------
377 * reset children signals
379 void server_reset_signal(void)
383 const struct itimerval none = {{0, 0}, {0, 0}};
385 setitimer(ITIMER_REAL, &none, NULL);
386 memset(&sv, 0, sizeof(sv));
387 sv.sa_handler = SIG_DFL;
388 sigemptyset( &sv.sa_mask );
390 sigaction(SIGALRM, &sv, NULL );
391 sigaction(SIGHUP, &sv, NULL );
392 sigaction(SIGTERM, &sv, NULL );
393 sigaction(SIGUSR1, &sv, NULL );
394 sigaction(SIGCHLD, &sv, NULL );
397 sigaddset(&sigs, SIGALRM);
398 sigaddset(&sigs, SIGHUP);
399 sigaddset(&sigs, SIGUSR1);
400 sigaddset(&sigs, SIGCHLD);
401 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);