File: server_child.c

package info (click to toggle)
netatalk 3.1.12~ds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,756 kB
  • sloc: ansic: 104,976; sh: 8,247; xml: 7,394; perl: 1,936; makefile: 1,430; python: 1,342; yacc: 309; lex: 49
file content (351 lines) | stat: -rw-r--r-- 10,525 bytes parent folder | download | duplicates (2)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
 * Copyright (c) 2013 Frank Lahm <franklahm@gmail.com
 * All rights reserved. See COPYRIGHT.
 *
 * handle inserting, removing, and freeing of children.
 * this does it via a hash table. it incurs some overhead over
 * a linear append/remove in total removal and kills, but it makes
 * single-entry removals a fast operation. as total removals occur during
 * child initialization and kills during server shutdown, this is
 * probably a win for a lot of connections and unimportant for a small
 * number of connections.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <pthread.h>

#include <atalk/logger.h>
#include <atalk/errchk.h>
#include <atalk/util.h>
#include <atalk/server_child.h>

#ifndef WEXITSTATUS
#define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
#endif /* ! WEXITSTATUS */
#ifndef WIFEXITED
#define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
#endif /* ! WIFEXITED */
#ifndef WIFSTOPPED
#define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
#endif
#ifndef WIFSIGNALED
#define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
#endif
#ifndef WTERMSIG
#define WTERMSIG(status)      ((status) & 0x7f)
#endif

/* hash/child functions: hash OR's pid */
#define HASH(i) ((((i) >> 8) ^ (i)) & (CHILD_HASHSIZE - 1))

static inline void hash_child(afp_child_t **htable, afp_child_t *child)
{
    afp_child_t **table;

    table = &htable[HASH(child->afpch_pid)];
    if ((child->afpch_next = *table) != NULL)
        (*table)->afpch_prevp = &child->afpch_next;
    *table = child;
    child->afpch_prevp = table;
}

static inline void unhash_child(afp_child_t *child)
{
    if (child->afpch_prevp) {
        if (child->afpch_next)
            child->afpch_next->afpch_prevp = child->afpch_prevp;
        *(child->afpch_prevp) = child->afpch_next;
    }
}

afp_child_t *server_child_resolve(server_child_t *childs, id_t pid)
{
    afp_child_t *child;

    for (child = childs->servch_table[HASH(pid)]; child; child = child->afpch_next) {
        if (child->afpch_pid == pid)
            break;
    }

    return child;
}

/* initialize server_child structure */
server_child_t *server_child_alloc(int connections)
{
    server_child_t *children;

    if (!(children = (server_child_t *)calloc(1, sizeof(server_child_t))))
        return NULL;

    children->servch_nsessions = connections;
    pthread_mutex_init(&children->servch_lock, NULL);
    return children;
}

/*!
 * add a child
 * @return pointer to struct server_child_data on success, NULL on error
 */
afp_child_t *server_child_add(server_child_t *children, pid_t pid, int ipc_fd)
{
    afp_child_t *child = NULL;

    pthread_mutex_lock(&children->servch_lock);

    /* it's possible that the child could have already died before the
     * pthread_sigmask. we need to check for this. */
    if (kill(pid, 0) < 0) {
        LOG(log_error, logtype_default, "server_child_add: no such process pid [%d]", pid);
        goto exit;
    }

    /* if we already have an entry. just return. */
    if ((child = server_child_resolve(children, pid)))
        goto exit;

    if ((child = calloc(1, sizeof(afp_child_t))) == NULL)
        goto exit;

    child->afpch_pid = pid;
    child->afpch_ipc_fd = ipc_fd;
    child->afpch_logintime = time(NULL);

    hash_child(children->servch_table, child);
    children->servch_count++;

exit:
    pthread_mutex_unlock(&children->servch_lock);
    return child;
}

/* remove a child and free it */
int server_child_remove(server_child_t *children, pid_t pid)
{
    int fd;
    afp_child_t *child;

    if (!(child = server_child_resolve(children, pid)))
        return -1;

    pthread_mutex_lock(&children->servch_lock);

    unhash_child(child);
    if (child->afpch_clientid) {
        free(child->afpch_clientid);
        child->afpch_clientid = NULL;
    }

    /* In main:child_handler() we need the fd in order to remove it from the pollfd set */
    fd = child->afpch_ipc_fd;
    if (fd != -1)
        close(fd);

    free(child);
    children->servch_count--;

    pthread_mutex_unlock(&children->servch_lock);

    return fd;
}

/* free everything: by using a hash table, this increases the cost of
 * this part over a linked list by the size of the hash table */
void server_child_free(server_child_t *children)
{
    afp_child_t *child, *tmp;
    int j;

    for (j = 0; j < CHILD_HASHSIZE; j++) {
        child = children->servch_table[j]; /* start at the beginning */
        while (child) {
            tmp = child->afpch_next;
            close(child->afpch_ipc_fd);
            if (child->afpch_clientid)
                free(child->afpch_clientid);
            if (child->afpch_volumes)
                free(child->afpch_volumes);
            free(child);
            child = tmp;
        }
    }

    free(children);
}

/* send signal to all child processes */
void server_child_kill(server_child_t *children, int sig)
{
    afp_child_t *child, *tmp;
    int i;

    for (i = 0; i < CHILD_HASHSIZE; i++) {
        child = children->servch_table[i];
        while (child) {
            tmp = child->afpch_next;
            kill(child->afpch_pid, sig);
            child = tmp;
        }
    }
}

/* send kill to a child processes */
static int kill_child(afp_child_t *child)
{
    if (!child->afpch_killed) {
        kill(child->afpch_pid, SIGTERM);
        /* we don't wait because there's no guarantee that we can really kill it */
        child->afpch_killed = 1;
        return 1;
    } else {
        LOG(log_info, logtype_default, "Unresponsive child[%d], sending SIGKILL", child->afpch_pid);
        kill(child->afpch_pid, SIGKILL);
    }
    return 1;
}

/*!
 * Try to find an old session and pass socket
 * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
 */
int server_child_transfer_session(server_child_t *children,
                                  pid_t pid,
                                  uid_t uid,
                                  int afp_socket,
                                  uint16_t DSI_requestID)
{
    EC_INIT;
    afp_child_t *child;

    if ((child = server_child_resolve(children, pid)) == NULL) {
        LOG(log_note, logtype_default, "Reconnect: no child[%u]", pid);
        if (kill(pid, 0) == 0) {
            LOG(log_note, logtype_default, "Reconnect: terminating old session[%u]", pid);
            kill(pid, SIGTERM);
            sleep(2);
            if (kill(pid, 0) == 0) {
                LOG(log_error, logtype_default, "Reconnect: killing old session[%u]", pid);
                kill(pid, SIGKILL);
                sleep(2);
            }
        }
        return 0;
    }

    if (!child->afpch_valid) {
        /* hmm, client 'guess' the pid, rogue? */
        LOG(log_error, logtype_default, "Reconnect: invalidated child[%u]", pid);
        return 0;
    } else if (child->afpch_uid != uid) {
        LOG(log_error, logtype_default, "Reconnect: child[%u] not the same user", pid);
        return 0;
    }

    LOG(log_note, logtype_default, "Reconnect: transferring session to child[%u]", pid);
    
    if (writet(child->afpch_ipc_fd, &DSI_requestID, 2, 0, 2) != 2) {
        LOG(log_error, logtype_default, "Reconnect: error sending DSI id to child[%u]", pid);
        EC_STATUS(-1);
        goto EC_CLEANUP;
    }
    EC_ZERO_LOG(send_fd(child->afpch_ipc_fd, afp_socket));
    EC_ZERO_LOG(kill(pid, SIGURG));

    EC_STATUS(1);

EC_CLEANUP:
    EC_EXIT;
}


/* see if there is a process for the same mac     */
/* if the times don't match mac has been rebooted */
void server_child_kill_one_by_id(server_child_t *children, pid_t pid,
                                 uid_t uid, uint32_t idlen, char *id, uint32_t boottime)
{
    afp_child_t *child, *tmp;
    int i;

    pthread_mutex_lock(&children->servch_lock);
    
    for (i = 0; i < CHILD_HASHSIZE; i++) {
        child = children->servch_table[i];
        while (child) {
            tmp = child->afpch_next;
            if (child->afpch_pid != pid) {
                if (child->afpch_idlen == idlen && memcmp(child->afpch_clientid, id, idlen) == 0) {
                    if ( child->afpch_boottime != boottime ) {
                        /* Client rebooted */
                        if (uid == child->afpch_uid) {
                            kill_child(child);
                            LOG(log_warning, logtype_default,
                                "Terminated disconnected child[%u], client rebooted.",
                                child->afpch_pid);
                        } else {
                            LOG(log_warning, logtype_default,
                                "Session with different pid[%u]", child->afpch_pid);
                        }
                    } else {
                        /* One client with multiple sessions */
                        LOG(log_debug, logtype_default,
                            "Found another session[%u] for client[%u]", child->afpch_pid, pid);
                    }
                }
            } else {
                /* update childs own slot */
                child->afpch_boottime = boottime;
                if (child->afpch_clientid)
                    free(child->afpch_clientid);
                LOG(log_debug, logtype_default, "Setting client ID for %u", child->afpch_pid);
                child->afpch_uid = uid;
                child->afpch_valid = 1;
                child->afpch_idlen = idlen;
                child->afpch_clientid = id;
            }
            child = tmp;
        }
    }

    pthread_mutex_unlock(&children->servch_lock);
}

/* ---------------------------
 * reset children signals
 */
void server_reset_signal(void)
{
    struct sigaction    sv;
    sigset_t            sigs;
    const struct itimerval none = {{0, 0}, {0, 0}};

    setitimer(ITIMER_REAL, &none, NULL);
    memset(&sv, 0, sizeof(sv));
    sv.sa_handler =  SIG_DFL;
    sigemptyset( &sv.sa_mask );

    sigaction(SIGALRM, &sv, NULL );
    sigaction(SIGHUP,  &sv, NULL );
    sigaction(SIGTERM, &sv, NULL );
    sigaction(SIGUSR1, &sv, NULL );
    sigaction(SIGCHLD, &sv, NULL );

    sigemptyset(&sigs);
    sigaddset(&sigs, SIGALRM);
    sigaddset(&sigs, SIGHUP);
    sigaddset(&sigs, SIGUSR1);
    sigaddset(&sigs, SIGCHLD);
    pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);

}