2 * Copyright (C) Joerg Lenneis 2003
3 * Copyright (C) Frank Lahm 2009, 2010
5 * All Rights Reserved. See COPYING.
9 cnid_dbd metadaemon to start up cnid_dbd upon request from afpd.
13 1. afpd -------> cnid_metad
15 via UNIX domain socket
16 2. cnid_metad -------> cnid_dbd
19 3. cnid_metad -------> cnid_dbd
23 4. afpd -------> cnid_dbd
25 cnid_metad and cnid_dbd have been converted to non-blocking IO in 2010.
31 #endif /* HAVE_CONFIG_H */
37 #include <sys/param.h>
41 #include <sys/types.h>
43 #include <sys/resource.h>
48 #include <sys/socket.h>
53 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
54 #endif /* ! WEXITSTATUS */
56 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
57 #endif /* ! WIFEXITED */
59 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
63 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
66 #define WTERMSIG(status) ((status) & 0x7f)
69 /* functions for username and group */
76 #define USE_SETRESUID 1
77 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
78 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
79 #endif /* USE_SETRESUID */
83 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
84 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
85 #endif /* USE_SETEUID */
88 #include <atalk/util.h>
89 #include <atalk/logger.h>
90 #include <atalk/cnid_dbd_private.h>
91 #include <atalk/paths.h>
92 #include <atalk/compat.h>
93 #include <atalk/errchk.h>
94 #include <atalk/bstrlib.h>
95 #include <atalk/netatalk_conf.h>
96 #include <atalk/volume.h>
100 #define DBHOME ".AppleDB"
105 static volatile sig_atomic_t sigchild = 0;
108 #define MAXSPAWN 3 /* Max times respawned in.. */
109 #define TESTTIME 42 /* this much seconds apfd client tries to *
110 * to reconnect every 5 secondes, catch it */
112 #define DEFAULTHOST "localhost"
113 #define DEFAULTPORT "4700"
118 time_t tm; /* When respawned last */
119 int count; /* Times respawned in the last TESTTIME secondes */
120 int control_fd; /* file descriptor to child cnid_dbd process */
123 static struct server srv[MAXVOLS];
125 static void daemon_exit(int i)
127 server_unlock(_PATH_CNID_METAD_LOCK);
131 /* ------------------ */
132 static void sigterm_handler(int sig)
136 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
139 LOG(log_error, logtype_afpd, "unexpected signal: %d", sig);
144 static struct server *test_usockfn(const char *path)
148 for (i = 0; i < maxvol; i++) {
149 if (srv[i].v_path && STRCMP(path, ==, srv[i].v_path))
156 /* -------------------- */
157 static int maybe_start_dbd(const AFPObj *obj, char *dbdpn, const char *volpath)
167 LOG(log_debug, logtype_cnid, "maybe_start_dbd(\"%s\"): BEGIN", volpath);
169 up = test_usockfn(volpath);
171 /* we already have a process, send our fd */
172 LOG(log_debug, logtype_cnid, "maybe_start_dbd: cnid_dbd[%d] already serving", up->pid);
173 if (send_fd(up->control_fd, rqstfd) < 0) {
180 LOG(log_debug, logtype_cnid, "maybe_start_dbd: no cnid_dbd serving yet");
184 /* find an empty slot (i < maxvol) or the first free slot (i == maxvol)*/
185 for (i = 0; i <= maxvol; i++) {
186 if (srv[i].v_path == NULL && i < MAXVOLS) {
188 if ((up->v_path = strdup(volpath)) == NULL)
198 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
202 /* we have a slot but no process, check for respawn too fast */
203 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
205 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
206 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
207 return -1; /* just exit, dont sleep, because we might have work to do for another client */
209 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
210 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
215 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
216 if (up->count > MAXSPAWN) {
217 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
218 we will just return -1 above */
219 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
224 Create socketpair for comm between parent and child.
225 We use it to pass fds from connecting afpd processes to our
226 cnid_dbd child via fd passing.
228 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
229 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
233 if ((pid = fork()) < 0) {
234 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
240 * Child. Close descriptors and start the daemon. If it fails
241 * just log it. The client process will fail connecting
248 for (i = 0; i < MAXVOLS; i++) {
249 if (srv[i].pid && up != &srv[i]) {
250 close(srv[i].control_fd);
254 sprintf(buf1, "%i", sv[1]);
255 sprintf(buf2, "%i", rqstfd);
257 if (up->count == MAXSPAWN) {
258 /* there's a pb with the db inform child, it will delete the db */
259 LOG(log_warning, logtype_cnid,
260 "Multiple attempts to start CNID db daemon for \"%s\" failed, wiping the slate clean...",
262 ret = execlp(dbdpn, dbdpn, "-F", obj->options.configfile, "-p", volpath, "-t", buf1, "-l", buf2, "-d", NULL);
264 ret = execlp(dbdpn, dbdpn, "-F", obj->options.configfile, "-p", volpath, "-t", buf1, "-l", buf2, NULL);
266 /* Yikes! We're still here, so exec failed... */
267 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
275 up->control_fd = sv[0];
279 /* ------------------ */
280 static int set_dbdir(char *dbdir)
287 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
288 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
292 if (dbdir[len - 1] != '/') {
296 strcpy(dbdir + len, DBHOME);
297 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
298 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
304 /* ------------------ */
305 static uid_t user_to_uid (char *username)
307 struct passwd *this_passwd;
309 /* check for anything */
310 if ( !username || strlen ( username ) < 1 ) return 0;
312 /* grab the /etc/passwd record relating to username */
313 this_passwd = getpwnam ( username );
315 /* return false if there is no structure returned */
316 if (this_passwd == NULL) return 0;
318 /* return proper uid */
319 return this_passwd->pw_uid;
323 /* ------------------ */
324 static gid_t group_to_gid ( char *group)
326 struct group *this_group;
328 /* check for anything */
329 if ( !group || strlen ( group ) < 1 ) return 0;
331 /* grab the /etc/groups record relating to group */
332 this_group = getgrnam ( group );
334 /* return false if there is no structure returned */
335 if (this_group == NULL) return 0;
337 /* return proper gid */
338 return this_group->gr_gid;
342 /* ------------------ */
343 static void catch_child(int sig _U_)
348 /* ----------------------- */
349 static void set_signal(void)
354 memset(&sv, 0, sizeof(sv));
357 sv.sa_handler = catch_child;
358 sv.sa_flags = SA_NOCLDSTOP;
359 sigemptyset(&sv.sa_mask);
360 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
361 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
362 daemon_exit(EXITERR_SYS);
366 sv.sa_handler = sigterm_handler;
367 sigfillset(&sv.sa_mask );
368 if (sigaction(SIGTERM, &sv, NULL ) < 0 ) {
369 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
370 daemon_exit(EXITERR_SYS);
373 /* Ignore the rest */
374 sv.sa_handler = SIG_IGN;
375 sigemptyset(&sv.sa_mask );
376 if (sigaction(SIGALRM, &sv, NULL ) < 0 ) {
377 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
378 daemon_exit(EXITERR_SYS);
380 sv.sa_handler = SIG_IGN;
381 sigemptyset(&sv.sa_mask );
382 if (sigaction(SIGHUP, &sv, NULL ) < 0 ) {
383 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
384 daemon_exit(EXITERR_SYS);
386 sv.sa_handler = SIG_IGN;
387 sigemptyset(&sv.sa_mask );
388 if (sigaction(SIGUSR1, &sv, NULL ) < 0 ) {
389 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
390 daemon_exit(EXITERR_SYS);
392 sv.sa_handler = SIG_IGN;
393 sigemptyset(&sv.sa_mask );
394 if (sigaction(SIGUSR2, &sv, NULL ) < 0 ) {
395 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
396 daemon_exit(EXITERR_SYS);
398 sv.sa_handler = SIG_IGN;
399 sigemptyset(&sv.sa_mask );
400 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
401 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
402 daemon_exit(EXITERR_SYS);
405 /* block everywhere but in pselect */
407 sigaddset(&set, SIGCHLD);
408 sigprocmask(SIG_BLOCK, &set, NULL);
411 static int setlimits(void)
415 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
416 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
419 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
420 rlim.rlim_cur = 65535;
421 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
422 rlim.rlim_max = 65535;
423 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
424 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
431 /* ------------------ */
432 int main(int argc, char *argv[])
434 char volpath[MAXPATHLEN + 1];
438 char *dbdpn = _PATH_CNID_DBD;
452 while (( cc = getopt( argc, argv, "dF:vV")) != -1 ) {
458 obj.cmdlineconfigfile = strdup(optarg);
462 printf("cnid_metad (Netatalk %s)\n", VERSION);
465 printf("cnid_metad [-dvV] [-F alternate configfile ]\n");
470 /* Check for PID lockfile */
471 if (check_lockfile("cnid_metad", _PATH_CNID_METAD_LOCK))
474 if (!debug && daemonize(0, 0) != 0)
477 /* Create PID lockfile */
478 if (create_lockfile("cnid_metad", _PATH_CNID_METAD_LOCK))
481 if (afp_config_parse(&obj) != 0)
484 set_processname("cnid_metad");
485 setuplog(obj.options.logconfig, obj.options.logfile);
487 if (load_volumes(&obj, NULL) != 0)
492 host = iniparser_getstrdup(obj.iniconfig, INISEC_GLOBAL, "cnid listen", "localhost:4700");
493 if (port = strrchr(host, ':'))
497 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
500 LOG(log_note, logtype_afpd, "CNID Server listening on %s:%s", host, port);
504 LOG(log_debug, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
506 if (SWITCH_TO_GID(gid) < 0) {
507 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
512 if (SWITCH_TO_UID(uid) < 0) {
513 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
522 sigprocmask(SIG_SETMASK, NULL, &set);
523 sigdelset(&set, SIGCHLD);
526 rqstfd = usockfd_check(srvfd, &set);
527 /* Collect zombie processes and log what happened to them */
528 if (sigchild) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
529 for (i = 0; i < maxvol; i++) {
530 if (srv[i].pid == pid) {
532 close(srv[i].control_fd);
536 if (WIFEXITED(status)) {
537 LOG(log_info, logtype_cnid, "cnid_dbd[%i] exited with exit code %i",
538 pid, WEXITSTATUS(status));
540 else if (WIFSIGNALED(status)) {
541 LOG(log_info, logtype_cnid, "cnid_dbd[%i] got signal %i",
542 pid, WTERMSIG(status));
549 ret = readt(rqstfd, &len, sizeof(int), 1, 4);
556 LOG(log_severe, logtype_cnid, "error read: %s", strerror(errno));
559 else if (ret != sizeof(int)) {
560 LOG(log_error, logtype_cnid, "short read: got %d", ret);
564 * checks for buffer overruns. The client libatalk side does it too
565 * before handing the dir path over but who trusts clients?
567 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
568 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
572 actual_len = readt(rqstfd, volpath, len, 1, 5);
573 if (actual_len < 0) {
574 LOG(log_severe, logtype_cnid, "Read(2) error : %s", strerror(errno));
577 if (actual_len != len) {
578 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
583 LOG(log_debug, logtype_cnid, "main: request for volume: %s", volpath);
585 if (load_volumes(&obj, NULL) != 0) {
586 LOG(log_severe, logtype_cnid, "main: error reloading config");
590 if ((vol = getvolbypath(&obj, volpath)) == NULL) {
591 LOG(log_severe, logtype_cnid, "main: no volume for path \"%s\"", volpath);
595 LOG(log_maxdebug, logtype_cnid, "main: dbpath: %s", vol->v_dbpath);
597 if (set_dbdir(vol->v_dbpath) < 0) {
601 maybe_start_dbd(&obj, dbdpn, vol->v_path);