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>
50 #include <sys/ioctl.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>
96 #define DBHOME ".AppleDB"
101 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"
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(char *dir)
145 for (i = 0; i < MAXVOLS; i++) {
146 if (srv[i].name && !strcmp(srv[i].name, dir)) {
153 /* -------------------- */
154 static int send_cred(int socket, int fd)
159 struct cmsghdr *cmsgp = NULL;
164 size = CMSG_SPACE(sizeof fd);
167 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
171 memset(&msgh,0,sizeof (msgh));
174 msgh.msg_name = NULL;
175 msgh.msg_namelen = 0;
180 iov[0].iov_base = &er;
181 iov[0].iov_len = sizeof(er);
183 msgh.msg_control = buf;
184 msgh.msg_controllen = size;
186 cmsgp = CMSG_FIRSTHDR(&msgh);
187 cmsgp->cmsg_level = SOL_SOCKET;
188 cmsgp->cmsg_type = SCM_RIGHTS;
189 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
191 *((int *)CMSG_DATA(cmsgp)) = fd;
192 msgh.msg_controllen = cmsgp->cmsg_len;
195 ret = sendmsg(socket,&msgh, 0);
196 } while ( ret == -1 && errno == EINTR );
198 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
206 /* -------------------- */
207 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
217 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: dbdir: '%s', UNIX socket file: '%s'",
220 up = test_usockfn(dbdir);
222 /* we already have a process, send our fd */
223 if (send_cred(up->control_fd, rqstfd) < 0) {
230 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet. Starting one ...");
234 /* find an empty slot */
235 for (i = 0; i < MAXVOLS; i++) {
236 if ( !srv[i].name ) {
240 up->name = strdup(dbdir);
245 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
250 /* we have a slot but no process, check for respawn too fast */
251 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
253 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
254 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
255 return -1; /* just exit, dont sleep, because we might have work to do for another client */
257 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
258 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
263 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
264 if (up->count > MAXSPAWN) {
265 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
266 we will just return -1 above */
267 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
272 Create socketpair for comm between parent and child.
273 We use it to pass fds from connecting afpd processes to our
274 cnid_dbd child via fd passing.
276 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
277 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
281 if ((pid = fork()) < 0) {
282 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
288 * Child. Close descriptors and start the daemon. If it fails
289 * just log it. The client process will fail connecting
296 for (i = 0; i < MAXVOLS; i++) {
297 if (srv[i].pid && up != &srv[i]) {
298 close(srv[i].control_fd);
302 sprintf(buf1, "%i", sv[1]);
303 sprintf(buf2, "%i", rqstfd);
305 if (up->count == MAXSPAWN) {
306 /* there's a pb with the db inform child
307 * it will run recover, delete the db whatever
309 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
310 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, logconfig, NULL);
313 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, logconfig, NULL);
315 /* Yikes! We're still here, so exec failed... */
316 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
324 up->control_fd = sv[0];
328 /* ------------------ */
329 static int set_dbdir(char *dbdir, int len)
336 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
337 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
341 if (dbdir[len - 1] != '/') {
345 strcpy(dbdir + len, DBHOME);
346 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
347 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
353 /* ------------------ */
354 static uid_t user_to_uid (char *username)
356 struct passwd *this_passwd;
358 /* check for anything */
359 if ( !username || strlen ( username ) < 1 ) return 0;
361 /* grab the /etc/passwd record relating to username */
362 this_passwd = getpwnam ( username );
364 /* return false if there is no structure returned */
365 if (this_passwd == NULL) return 0;
367 /* return proper uid */
368 return this_passwd->pw_uid;
372 /* ------------------ */
373 static gid_t group_to_gid ( char *group)
375 struct group *this_group;
377 /* check for anything */
378 if ( !group || strlen ( group ) < 1 ) return 0;
380 /* grab the /etc/groups record relating to group */
381 this_group = getgrnam ( group );
383 /* return false if there is no structure returned */
384 if (this_group == NULL) return 0;
386 /* return proper gid */
387 return this_group->gr_gid;
391 /* ------------------ */
392 static void catch_child(int sig _U_)
397 /* ----------------------- */
398 static void set_signal(void)
403 memset(&sv, 0, sizeof(sv));
406 sv.sa_handler = catch_child;
407 sv.sa_flags = SA_NOCLDSTOP;
408 sigemptyset(&sv.sa_mask);
409 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
410 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
411 daemon_exit(EXITERR_SYS);
415 sv.sa_handler = sigterm_handler;
416 sigfillset(&sv.sa_mask );
417 if (sigaction(SIGTERM, &sv, NULL ) < 0 ) {
418 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
419 daemon_exit(EXITERR_SYS);
422 /* Ignore the rest */
423 sv.sa_handler = SIG_IGN;
424 sigemptyset(&sv.sa_mask );
425 if (sigaction(SIGALRM, &sv, NULL ) < 0 ) {
426 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
427 daemon_exit(EXITERR_SYS);
429 sv.sa_handler = SIG_IGN;
430 sigemptyset(&sv.sa_mask );
431 if (sigaction(SIGHUP, &sv, NULL ) < 0 ) {
432 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
433 daemon_exit(EXITERR_SYS);
435 sv.sa_handler = SIG_IGN;
436 sigemptyset(&sv.sa_mask );
437 if (sigaction(SIGUSR1, &sv, NULL ) < 0 ) {
438 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
439 daemon_exit(EXITERR_SYS);
441 sv.sa_handler = SIG_IGN;
442 sigemptyset(&sv.sa_mask );
443 if (sigaction(SIGUSR2, &sv, NULL ) < 0 ) {
444 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
445 daemon_exit(EXITERR_SYS);
447 sv.sa_handler = SIG_IGN;
448 sigemptyset(&sv.sa_mask );
449 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
450 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
451 daemon_exit(EXITERR_SYS);
454 /* block everywhere but in pselect */
456 sigaddset(&set, SIGCHLD);
457 sigprocmask(SIG_BLOCK, &set, NULL);
460 /* ------------------ */
461 int main(int argc, char *argv[])
463 char dbdir[MAXPATHLEN + 1];
467 char *dbdpn = _PATH_CNID_DBD;
468 char *host = DEFAULTHOST;
469 char *port = DEFAULTPORT;
470 struct db_param *dbp;
478 char *loglevel = NULL;
479 char *logfile = NULL;
482 set_processname("cnid_metad");
484 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:f:")) != -1 ) {
490 host = strdup(optarg);
493 uid = user_to_uid (optarg);
495 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
500 gid =group_to_gid (optarg);
502 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
507 port = strdup(optarg);
510 dbdpn = strdup(optarg);
513 loglevel = strdup(optarg);
516 logfile = strdup(optarg);
525 strlcpy(logconfig + 8, loglevel, 13);
527 strcat(logconfig, " ");
530 strlcat(logconfig, logfile, MAXPATHLEN);
536 LOG(log_error, logtype_cnid, "main: bad arguments");
540 /* Check PID lockfile and become a daemon */
541 switch(server_lock("cnid_metad", _PATH_CNID_METAD_LOCK, 0)) {
543 daemon_exit(EXITERR_SYS);
546 default: /* server */
550 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
555 LOG(log_debug, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
557 if (SWITCH_TO_GID(gid) < 0) {
558 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
563 if (SWITCH_TO_UID(uid) < 0) {
564 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
573 sigprocmask(SIG_SETMASK, NULL, &set);
574 sigdelset(&set, SIGCHLD);
577 rqstfd = usockfd_check(srvfd, &set);
578 /* Collect zombie processes and log what happened to them */
579 if (sigchild) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
580 for (i = 0; i < MAXVOLS; i++) {
581 if (srv[i].pid == pid) {
583 close(srv[i].control_fd);
587 if (WIFEXITED(status)) {
588 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
589 pid, WEXITSTATUS(status));
591 else if (WIFSIGNALED(status)) {
592 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
593 pid, WTERMSIG(status));
600 ret = readt(rqstfd, &len, sizeof(int), 4);
606 LOG(log_severe, logtype_cnid, "error read: %s", strerror(errno));
609 else if (ret != sizeof(int)) {
610 LOG(log_error, logtype_cnid, "short read: got %d", ret);
614 * checks for buffer overruns. The client libatalk side does it too
615 * before handing the dir path over but who trusts clients?
617 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
618 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
622 actual_len = readt(rqstfd, dbdir, len, 1);
623 if (actual_len < 0) {
624 LOG(log_severe, logtype_cnid, "Read(2) error : %s", strerror(errno));
627 if (actual_len != len) {
628 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
633 if (set_dbdir(dbdir, len) < 0) {
637 if ((dbp = db_param_read(dbdir, METAD)) == NULL) {
638 LOG(log_error, logtype_cnid, "Error reading config file");
641 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);