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>
47 #include <sys/socket.h>
52 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
53 #endif /* ! WEXITSTATUS */
55 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
56 #endif /* ! WIFEXITED */
58 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
62 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
65 #define WTERMSIG(status) ((status) & 0x7f)
68 /* functions for username and group */
75 #define USE_SETRESUID 1
76 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
77 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
78 #endif /* USE_SETRESUID */
82 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
83 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
84 #endif /* USE_SETEUID */
87 #include <atalk/util.h>
88 #include <atalk/logger.h>
89 #include <atalk/cnid_dbd_private.h>
90 #include <atalk/paths.h>
91 #include <atalk/volinfo.h>
95 #define DBHOME ".AppleDB"
100 static volatile sig_atomic_t sigchild = 0;
103 #define MAXSPAWN 3 /* Max times respawned in.. */
104 #define TESTTIME 42 /* this much seconds apfd client tries to *
105 * to reconnect every 5 secondes, catch it */
107 #define DEFAULTHOST "localhost"
108 #define DEFAULTPORT "4700"
111 struct volinfo *volinfo;
113 time_t tm; /* When respawned last */
114 int count; /* Times respawned in the last TESTTIME secondes */
115 int control_fd; /* file descriptor to child cnid_dbd process */
118 static struct server srv[MAXVOLS];
120 /* Default logging config: log to syslog with level log_note */
121 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
123 static void daemon_exit(int i)
125 server_unlock(_PATH_CNID_METAD_LOCK);
129 /* ------------------ */
130 static void sigterm_handler(int sig)
134 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
137 LOG(log_error, logtype_afpd, "unexpected signal: %d", sig);
142 static struct server *test_usockfn(struct volinfo *volinfo)
145 for (i = 0; i < maxvol; i++) {
146 if ((srv[i].volinfo) && (strcmp(srv[i].volinfo->v_path, volinfo->v_path) == 0)) {
153 /* -------------------- */
154 static int maybe_start_dbd(char *dbdpn, struct volinfo *volinfo)
163 char *volpath = volinfo->v_path;
165 LOG(log_debug, logtype_cnid, "maybe_start_dbd: Volume: \"%s\"", volpath);
167 up = test_usockfn(volinfo);
169 /* we already have a process, send our fd */
170 if (send_fd(up->control_fd, rqstfd) < 0) {
177 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet");
181 /* find an empty slot (i < maxvol) or the first free slot (i == maxvol)*/
182 for (i = 0; i <= maxvol; i++) {
183 if (srv[i].volinfo == NULL && i < MAXVOLS) {
185 up->volinfo = volinfo;
186 retainvolinfo(volinfo);
195 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
199 /* we have a slot but no process, check for respawn too fast */
200 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
202 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
203 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
204 return -1; /* just exit, dont sleep, because we might have work to do for another client */
206 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
207 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
212 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
213 if (up->count > MAXSPAWN) {
214 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
215 we will just return -1 above */
216 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
221 Create socketpair for comm between parent and child.
222 We use it to pass fds from connecting afpd processes to our
223 cnid_dbd child via fd passing.
225 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
226 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
230 if ((pid = fork()) < 0) {
231 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
237 * Child. Close descriptors and start the daemon. If it fails
238 * just log it. The client process will fail connecting
245 for (i = 0; i < MAXVOLS; i++) {
246 if (srv[i].pid && up != &srv[i]) {
247 close(srv[i].control_fd);
251 sprintf(buf1, "%i", sv[1]);
252 sprintf(buf2, "%i", rqstfd);
254 if (up->count == MAXSPAWN) {
255 /* there's a pb with the db inform child, it will delete the db */
256 LOG(log_warning, logtype_cnid,
257 "Multiple attempts to start CNID db daemon for \"%s\" failed, wiping the slate clean...",
258 up->volinfo->v_path);
259 ret = execlp(dbdpn, dbdpn, "-d", volpath, buf1, buf2, logconfig, NULL);
261 ret = execlp(dbdpn, dbdpn, volpath, buf1, buf2, logconfig, NULL);
263 /* Yikes! We're still here, so exec failed... */
264 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
272 up->control_fd = sv[0];
276 /* ------------------ */
277 static int set_dbdir(char *dbdir)
284 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
285 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
289 if (dbdir[len - 1] != '/') {
293 strcpy(dbdir + len, DBHOME);
294 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
295 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
301 /* ------------------ */
302 static uid_t user_to_uid (char *username)
304 struct passwd *this_passwd;
306 /* check for anything */
307 if ( !username || strlen ( username ) < 1 ) return 0;
309 /* grab the /etc/passwd record relating to username */
310 this_passwd = getpwnam ( username );
312 /* return false if there is no structure returned */
313 if (this_passwd == NULL) return 0;
315 /* return proper uid */
316 return this_passwd->pw_uid;
320 /* ------------------ */
321 static gid_t group_to_gid ( char *group)
323 struct group *this_group;
325 /* check for anything */
326 if ( !group || strlen ( group ) < 1 ) return 0;
328 /* grab the /etc/groups record relating to group */
329 this_group = getgrnam ( group );
331 /* return false if there is no structure returned */
332 if (this_group == NULL) return 0;
334 /* return proper gid */
335 return this_group->gr_gid;
339 /* ------------------ */
340 static void catch_child(int sig _U_)
345 /* ----------------------- */
346 static void set_signal(void)
351 memset(&sv, 0, sizeof(sv));
354 sv.sa_handler = catch_child;
355 sv.sa_flags = SA_NOCLDSTOP;
356 sigemptyset(&sv.sa_mask);
357 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
358 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
359 daemon_exit(EXITERR_SYS);
363 sv.sa_handler = sigterm_handler;
364 sigfillset(&sv.sa_mask );
365 if (sigaction(SIGTERM, &sv, NULL ) < 0 ) {
366 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
367 daemon_exit(EXITERR_SYS);
370 /* Ignore the rest */
371 sv.sa_handler = SIG_IGN;
372 sigemptyset(&sv.sa_mask );
373 if (sigaction(SIGALRM, &sv, NULL ) < 0 ) {
374 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
375 daemon_exit(EXITERR_SYS);
377 sv.sa_handler = SIG_IGN;
378 sigemptyset(&sv.sa_mask );
379 if (sigaction(SIGHUP, &sv, NULL ) < 0 ) {
380 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
381 daemon_exit(EXITERR_SYS);
383 sv.sa_handler = SIG_IGN;
384 sigemptyset(&sv.sa_mask );
385 if (sigaction(SIGUSR1, &sv, NULL ) < 0 ) {
386 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
387 daemon_exit(EXITERR_SYS);
389 sv.sa_handler = SIG_IGN;
390 sigemptyset(&sv.sa_mask );
391 if (sigaction(SIGUSR2, &sv, NULL ) < 0 ) {
392 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
393 daemon_exit(EXITERR_SYS);
395 sv.sa_handler = SIG_IGN;
396 sigemptyset(&sv.sa_mask );
397 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
398 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
399 daemon_exit(EXITERR_SYS);
402 /* block everywhere but in pselect */
404 sigaddset(&set, SIGCHLD);
405 sigprocmask(SIG_BLOCK, &set, NULL);
408 static int setlimits(void)
412 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
413 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
416 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
417 rlim.rlim_cur = 65535;
418 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
419 rlim.rlim_max = 65535;
420 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
421 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
428 /* ------------------ */
429 int main(int argc, char *argv[])
431 char volpath[MAXPATHLEN + 1];
435 char *dbdpn = _PATH_CNID_DBD;
436 char *host = DEFAULTHOST;
437 char *port = DEFAULTPORT;
445 char *loglevel = NULL;
446 char *logfile = NULL;
448 struct volinfo *volinfo;
450 set_processname("cnid_metad");
452 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:f:")) != -1 ) {
458 host = strdup(optarg);
461 uid = user_to_uid (optarg);
463 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
468 gid =group_to_gid (optarg);
470 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
475 port = strdup(optarg);
478 dbdpn = strdup(optarg);
481 loglevel = strdup(optarg);
484 logfile = strdup(optarg);
493 strlcpy(logconfig + 8, loglevel, 13);
495 strcat(logconfig, " ");
498 strlcat(logconfig, logfile, MAXPATHLEN);
504 LOG(log_error, logtype_cnid, "main: bad arguments");
510 /* Check PID lockfile and become a daemon */
511 switch(server_lock("cnid_metad", _PATH_CNID_METAD_LOCK, debug)) {
513 daemon_exit(EXITERR_SYS);
516 default: /* server */
520 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
525 LOG(log_debug, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
527 if (SWITCH_TO_GID(gid) < 0) {
528 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
533 if (SWITCH_TO_UID(uid) < 0) {
534 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
543 sigprocmask(SIG_SETMASK, NULL, &set);
544 sigdelset(&set, SIGCHLD);
547 rqstfd = usockfd_check(srvfd, &set);
548 /* Collect zombie processes and log what happened to them */
549 if (sigchild) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
550 for (i = 0; i < maxvol; i++) {
551 if (srv[i].pid == pid) {
553 close(srv[i].control_fd);
557 if (WIFEXITED(status)) {
558 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
559 pid, WEXITSTATUS(status));
561 else if (WIFSIGNALED(status)) {
562 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
563 pid, WTERMSIG(status));
570 ret = readt(rqstfd, &len, sizeof(int), 1, 4);
577 LOG(log_severe, logtype_cnid, "error read: %s", strerror(errno));
580 else if (ret != sizeof(int)) {
581 LOG(log_error, logtype_cnid, "short read: got %d", ret);
585 * checks for buffer overruns. The client libatalk side does it too
586 * before handing the dir path over but who trusts clients?
588 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
589 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
593 actual_len = readt(rqstfd, volpath, len, 1, 5);
594 if (actual_len < 0) {
595 LOG(log_severe, logtype_cnid, "Read(2) error : %s", strerror(errno));
598 if (actual_len != len) {
599 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
604 /* Load .volinfo file */
605 if ((volinfo = allocvolinfo(volpath)) == NULL) {
606 LOG(log_severe, logtype_cnid, "allocvolinfo(\"%s\"): %s",
607 volpath, strerror(errno));
611 if (set_dbdir(volinfo->v_dbpath) < 0) {
615 maybe_start_dbd(dbdpn, volinfo);
617 (void)closevolinfo(volinfo);