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/volinfo.h>
93 #include <atalk/compat.h>
97 #define DBHOME ".AppleDB"
102 static volatile sig_atomic_t sigchild = 0;
105 #define MAXSPAWN 3 /* Max times respawned in.. */
106 #define TESTTIME 42 /* this much seconds apfd client tries to *
107 * to reconnect every 5 secondes, catch it */
109 #define DEFAULTHOST "localhost"
110 #define DEFAULTPORT "4700"
113 struct volinfo *volinfo;
115 time_t tm; /* When respawned last */
116 int count; /* Times respawned in the last TESTTIME secondes */
117 int control_fd; /* file descriptor to child cnid_dbd process */
120 static struct server srv[MAXVOLS];
122 /* Default logging config: log to syslog with level log_note */
123 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
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(struct volinfo *volinfo)
147 for (i = 0; i < maxvol; i++) {
148 if ((srv[i].volinfo) && (strcmp(srv[i].volinfo->v_path, volinfo->v_path) == 0)) {
155 /* -------------------- */
156 static int maybe_start_dbd(char *dbdpn, struct volinfo *volinfo)
165 char *volpath = volinfo->v_path;
167 LOG(log_debug, logtype_cnid, "maybe_start_dbd: Volume: \"%s\"", volpath);
169 up = test_usockfn(volinfo);
171 /* we already have a process, send our fd */
172 if (send_fd(up->control_fd, rqstfd) < 0) {
179 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet");
183 /* find an empty slot (i < maxvol) or the first free slot (i == maxvol)*/
184 for (i = 0; i <= maxvol; i++) {
185 if (srv[i].volinfo == NULL && i < MAXVOLS) {
187 up->volinfo = volinfo;
188 retainvolinfo(volinfo);
197 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
201 /* we have a slot but no process, check for respawn too fast */
202 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
204 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
205 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
206 return -1; /* just exit, dont sleep, because we might have work to do for another client */
208 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
209 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
214 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
215 if (up->count > MAXSPAWN) {
216 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
217 we will just return -1 above */
218 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
223 Create socketpair for comm between parent and child.
224 We use it to pass fds from connecting afpd processes to our
225 cnid_dbd child via fd passing.
227 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
228 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
232 if ((pid = fork()) < 0) {
233 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
239 * Child. Close descriptors and start the daemon. If it fails
240 * just log it. The client process will fail connecting
247 for (i = 0; i < MAXVOLS; i++) {
248 if (srv[i].pid && up != &srv[i]) {
249 close(srv[i].control_fd);
253 sprintf(buf1, "%i", sv[1]);
254 sprintf(buf2, "%i", rqstfd);
256 if (up->count == MAXSPAWN) {
257 /* there's a pb with the db inform child, it will delete the db */
258 LOG(log_warning, logtype_cnid,
259 "Multiple attempts to start CNID db daemon for \"%s\" failed, wiping the slate clean...",
260 up->volinfo->v_path);
261 ret = execlp(dbdpn, dbdpn, "-d", volpath, buf1, buf2, logconfig, NULL);
263 ret = execlp(dbdpn, dbdpn, volpath, buf1, buf2, logconfig, NULL);
265 /* Yikes! We're still here, so exec failed... */
266 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
274 up->control_fd = sv[0];
278 /* ------------------ */
279 static int set_dbdir(char *dbdir)
286 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
287 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
291 if (dbdir[len - 1] != '/') {
295 strcpy(dbdir + len, DBHOME);
296 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
297 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
303 /* ------------------ */
304 static uid_t user_to_uid (char *username)
306 struct passwd *this_passwd;
308 /* check for anything */
309 if ( !username || strlen ( username ) < 1 ) return 0;
311 /* grab the /etc/passwd record relating to username */
312 this_passwd = getpwnam ( username );
314 /* return false if there is no structure returned */
315 if (this_passwd == NULL) return 0;
317 /* return proper uid */
318 return this_passwd->pw_uid;
322 /* ------------------ */
323 static gid_t group_to_gid ( char *group)
325 struct group *this_group;
327 /* check for anything */
328 if ( !group || strlen ( group ) < 1 ) return 0;
330 /* grab the /etc/groups record relating to group */
331 this_group = getgrnam ( group );
333 /* return false if there is no structure returned */
334 if (this_group == NULL) return 0;
336 /* return proper gid */
337 return this_group->gr_gid;
341 /* ------------------ */
342 static void catch_child(int sig _U_)
347 /* ----------------------- */
348 static void set_signal(void)
353 memset(&sv, 0, sizeof(sv));
356 sv.sa_handler = catch_child;
357 sv.sa_flags = SA_NOCLDSTOP;
358 sigemptyset(&sv.sa_mask);
359 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
360 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
361 daemon_exit(EXITERR_SYS);
365 sv.sa_handler = sigterm_handler;
366 sigfillset(&sv.sa_mask );
367 if (sigaction(SIGTERM, &sv, NULL ) < 0 ) {
368 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
369 daemon_exit(EXITERR_SYS);
372 /* Ignore the rest */
373 sv.sa_handler = SIG_IGN;
374 sigemptyset(&sv.sa_mask );
375 if (sigaction(SIGALRM, &sv, NULL ) < 0 ) {
376 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
377 daemon_exit(EXITERR_SYS);
379 sv.sa_handler = SIG_IGN;
380 sigemptyset(&sv.sa_mask );
381 if (sigaction(SIGHUP, &sv, NULL ) < 0 ) {
382 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
383 daemon_exit(EXITERR_SYS);
385 sv.sa_handler = SIG_IGN;
386 sigemptyset(&sv.sa_mask );
387 if (sigaction(SIGUSR1, &sv, NULL ) < 0 ) {
388 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
389 daemon_exit(EXITERR_SYS);
391 sv.sa_handler = SIG_IGN;
392 sigemptyset(&sv.sa_mask );
393 if (sigaction(SIGUSR2, &sv, NULL ) < 0 ) {
394 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
395 daemon_exit(EXITERR_SYS);
397 sv.sa_handler = SIG_IGN;
398 sigemptyset(&sv.sa_mask );
399 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
400 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
401 daemon_exit(EXITERR_SYS);
404 /* block everywhere but in pselect */
406 sigaddset(&set, SIGCHLD);
407 sigprocmask(SIG_BLOCK, &set, NULL);
410 static int setlimits(void)
414 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
415 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
418 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
419 rlim.rlim_cur = 65535;
420 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
421 rlim.rlim_max = 65535;
422 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
423 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
430 /* ------------------ */
431 int main(int argc, char *argv[])
433 char volpath[MAXPATHLEN + 1];
437 char *dbdpn = _PATH_CNID_DBD;
438 char *host = DEFAULTHOST;
439 char *port = DEFAULTPORT;
447 char *loglevel = NULL;
448 char *logfile = NULL;
450 struct volinfo *volinfo;
452 set_processname("cnid_metad");
454 while (( cc = getopt( argc, argv, "vVds:p:h:u:g:l:f:")) != -1 ) {
458 printf("cnid_metad (Netatalk %s)\n", VERSION);
464 host = strdup(optarg);
467 uid = user_to_uid (optarg);
469 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
474 gid =group_to_gid (optarg);
476 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
481 port = strdup(optarg);
484 dbdpn = strdup(optarg);
487 loglevel = strdup(optarg);
490 logfile = strdup(optarg);
498 /* Check for PID lockfile */
499 if (check_lockfile("cnid_metad", _PATH_CNID_METAD_LOCK))
502 if (!debug && daemonize(0, 0) != 0)
505 /* Create PID lockfile */
506 if (create_lockfile("cnid_metad", _PATH_CNID_METAD_LOCK))
510 strlcpy(logconfig + 8, loglevel, 13);
512 strcat(logconfig, " ");
515 strlcat(logconfig, logfile, MAXPATHLEN);
521 LOG(log_error, logtype_cnid, "main: bad arguments");
527 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
532 LOG(log_debug, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
534 if (SWITCH_TO_GID(gid) < 0) {
535 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
540 if (SWITCH_TO_UID(uid) < 0) {
541 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
550 sigprocmask(SIG_SETMASK, NULL, &set);
551 sigdelset(&set, SIGCHLD);
554 rqstfd = usockfd_check(srvfd, &set);
555 /* Collect zombie processes and log what happened to them */
556 if (sigchild) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
557 for (i = 0; i < maxvol; i++) {
558 if (srv[i].pid == pid) {
560 close(srv[i].control_fd);
564 if (WIFEXITED(status)) {
565 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
566 pid, WEXITSTATUS(status));
568 else if (WIFSIGNALED(status)) {
569 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
570 pid, WTERMSIG(status));
577 ret = readt(rqstfd, &len, sizeof(int), 1, 4);
584 LOG(log_severe, logtype_cnid, "error read: %s", strerror(errno));
587 else if (ret != sizeof(int)) {
588 LOG(log_error, logtype_cnid, "short read: got %d", ret);
592 * checks for buffer overruns. The client libatalk side does it too
593 * before handing the dir path over but who trusts clients?
595 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
596 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
600 actual_len = readt(rqstfd, volpath, len, 1, 5);
601 if (actual_len < 0) {
602 LOG(log_severe, logtype_cnid, "Read(2) error : %s", strerror(errno));
605 if (actual_len != len) {
606 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
611 /* Load .volinfo file */
612 if ((volinfo = allocvolinfo(volpath)) == NULL) {
613 LOG(log_severe, logtype_cnid, "allocvolinfo(\"%s\"): %s",
614 volpath, strerror(errno));
618 if (set_dbdir(volinfo->v_dbpath) < 0) {
622 maybe_start_dbd(dbdpn, volinfo);
624 (void)closevolinfo(volinfo);