2 * Copyright (C) Joerg Lenneis 2003
3 * Copyright (C) Frank Lahm 2010
4 * All Rights Reserved. See COPYING.
8 cnid_dbd metadaemon to start up cnid_dbd upon request from afpd.
12 1. afpd -------> cnid_metad
14 via UNIX domain socket
15 2. cnid_metad -------> cnid_dbd
18 3. cnid_metad -------> cnid_dbd
22 4. afpd -------> cnid_dbd
28 #endif /* HAVE_CONFIG_H */
34 #include <sys/param.h>
38 #ifdef HAVE_SYS_TYPES_H
39 #include <sys/types.h>
41 #ifdef HAVE_SYS_TIME_H
44 #ifdef HAVE_SYS_WAIT_H
52 #include <sys/socket.h>
55 #include <sys/ioctl.h>
58 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
59 #endif /* ! WEXITSTATUS */
61 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
62 #endif /* ! WIFEXITED */
64 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
68 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
71 #define WTERMSIG(status) ((status) & 0x7f)
74 /* functions for username and group */
81 #define USE_SETRESUID 1
82 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
83 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
84 #endif /* USE_SETRESUID */
88 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
89 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
90 #endif /* USE_SETEUID */
93 #include <atalk/util.h>
94 #include <atalk/logger.h>
95 #include <atalk/cnid_dbd_private.h>
96 #include <atalk/paths.h>
101 #define DBHOME ".AppleDB"
106 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 /* Default logging config: log to syslog with level log_note */
126 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
128 static void daemon_exit(int i)
130 server_unlock(_PATH_CNID_METAD_LOCK);
134 /* ------------------ */
135 static void sigterm_handler(int sig)
139 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
142 LOG(log_error, logtype_afpd, "unexpected signal: %d", sig);
147 static struct server *test_usockfn(char *dir)
150 for (i = 0; i < MAXVOLS; i++) {
151 if (srv[i].name && !strcmp(srv[i].name, dir)) {
158 /* -------------------- */
159 static int send_cred(int socket, int fd)
164 struct cmsghdr *cmsgp = NULL;
169 size = CMSG_SPACE(sizeof fd);
172 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
176 memset(&msgh,0,sizeof (msgh));
179 msgh.msg_name = NULL;
180 msgh.msg_namelen = 0;
185 iov[0].iov_base = &er;
186 iov[0].iov_len = sizeof(er);
188 msgh.msg_control = buf;
189 msgh.msg_controllen = size;
191 cmsgp = CMSG_FIRSTHDR(&msgh);
192 cmsgp->cmsg_level = SOL_SOCKET;
193 cmsgp->cmsg_type = SCM_RIGHTS;
194 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
196 *((int *)CMSG_DATA(cmsgp)) = fd;
197 msgh.msg_controllen = cmsgp->cmsg_len;
200 ret = sendmsg(socket,&msgh, 0);
201 } while ( ret == -1 && errno == EINTR );
203 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
211 /* -------------------- */
212 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
222 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: dbdir: '%s', UNIX socket file: '%s'",
225 up = test_usockfn(dbdir);
227 /* we already have a process, send our fd */
228 if (send_cred(up->control_fd, rqstfd) < 0) {
235 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet. Starting one ...");
239 /* find an empty slot */
240 for (i = 0; i < MAXVOLS; i++) {
241 if ( !srv[i].name ) {
245 up->name = strdup(dbdir);
250 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
255 /* we have a slot but no process, check for respawn too fast */
256 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
258 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
259 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
260 return -1; /* just exit, dont sleep, because we might have work to do for another client */
262 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
263 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
268 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
269 if (up->count > MAXSPAWN) {
270 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
271 we will just return -1 above */
272 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
277 Create socketpair for comm between parent and child.
278 We use it to pass fds from connecting afpd processes to our
279 cnid_dbd child via fd passing.
281 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
282 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
286 if ((pid = fork()) < 0) {
287 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
293 * Child. Close descriptors and start the daemon. If it fails
294 * just log it. The client process will fail connecting
301 for (i = 0; i < MAXVOLS; i++) {
302 if (srv[i].pid && up != &srv[i]) {
303 close(srv[i].control_fd);
307 sprintf(buf1, "%i", sv[1]);
308 sprintf(buf2, "%i", rqstfd);
310 if (up->count == MAXSPAWN) {
311 /* there's a pb with the db inform child
312 * it will run recover, delete the db whatever
314 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
315 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, logconfig, NULL);
318 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, logconfig, NULL);
320 /* Yikes! We're still here, so exec failed... */
321 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
329 up->control_fd = sv[0];
333 /* ------------------ */
334 static int set_dbdir(char *dbdir, int len)
341 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
342 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
346 if (dbdir[len - 1] != '/') {
350 strcpy(dbdir + len, DBHOME);
351 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
352 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
358 /* ------------------ */
359 static uid_t user_to_uid (char *username)
361 struct passwd *this_passwd;
363 /* check for anything */
364 if ( !username || strlen ( username ) < 1 ) return 0;
366 /* grab the /etc/passwd record relating to username */
367 this_passwd = getpwnam ( username );
369 /* return false if there is no structure returned */
370 if (this_passwd == NULL) return 0;
372 /* return proper uid */
373 return this_passwd->pw_uid;
377 /* ------------------ */
378 static gid_t group_to_gid ( char *group)
380 struct group *this_group;
382 /* check for anything */
383 if ( !group || strlen ( group ) < 1 ) return 0;
385 /* grab the /etc/groups record relating to group */
386 this_group = getgrnam ( group );
388 /* return false if there is no structure returned */
389 if (this_group == NULL) return 0;
391 /* return proper gid */
392 return this_group->gr_gid;
396 /* ------------------ */
397 static void catch_child(int sig _U_)
402 /* ----------------------- */
403 static void set_signal(void)
408 memset(&sv, 0, sizeof(sv));
411 sv.sa_handler = catch_child;
412 sv.sa_flags = SA_NOCLDSTOP;
413 sigemptyset(&sv.sa_mask);
414 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
415 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
416 daemon_exit(EXITERR_SYS);
420 sv.sa_handler = sigterm_handler;
421 sigfillset(&sv.sa_mask );
422 if (sigaction(SIGTERM, &sv, NULL ) < 0 ) {
423 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
424 daemon_exit(EXITERR_SYS);
427 /* Ignore the rest */
428 sv.sa_handler = SIG_IGN;
429 sigemptyset(&sv.sa_mask );
430 if (sigaction(SIGALRM, &sv, NULL ) < 0 ) {
431 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
432 daemon_exit(EXITERR_SYS);
434 sv.sa_handler = SIG_IGN;
435 sigemptyset(&sv.sa_mask );
436 if (sigaction(SIGHUP, &sv, NULL ) < 0 ) {
437 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
438 daemon_exit(EXITERR_SYS);
440 sv.sa_handler = SIG_IGN;
441 sigemptyset(&sv.sa_mask );
442 if (sigaction(SIGUSR1, &sv, NULL ) < 0 ) {
443 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
444 daemon_exit(EXITERR_SYS);
446 sv.sa_handler = SIG_IGN;
447 sigemptyset(&sv.sa_mask );
448 if (sigaction(SIGUSR2, &sv, NULL ) < 0 ) {
449 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
450 daemon_exit(EXITERR_SYS);
452 sv.sa_handler = SIG_IGN;
453 sigemptyset(&sv.sa_mask );
454 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
455 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
456 daemon_exit(EXITERR_SYS);
459 /* block everywhere but in pselect */
461 sigaddset(&set, SIGCHLD);
462 sigprocmask(SIG_BLOCK, &set, NULL);
465 /* ------------------ */
466 int main(int argc, char *argv[])
468 char dbdir[MAXPATHLEN + 1];
472 char *dbdpn = _PATH_CNID_DBD;
473 char *host = DEFAULTHOST;
474 char *port = DEFAULTPORT;
475 struct db_param *dbp;
483 char *loglevel = NULL;
484 char *logfile = NULL;
487 set_processname("cnid_metad");
489 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:f:")) != -1 ) {
495 host = strdup(optarg);
498 uid = user_to_uid (optarg);
500 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
505 gid =group_to_gid (optarg);
507 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
512 port = strdup(optarg);
515 dbdpn = strdup(optarg);
518 loglevel = strdup(optarg);
521 logfile = strdup(optarg);
530 strlcpy(logconfig + 8, loglevel, 13);
532 strcat(logconfig, " ");
535 strlcat(logconfig, logfile, MAXPATHLEN);
541 LOG(log_error, logtype_cnid, "main: bad arguments");
545 /* Check PID lockfile and become a daemon */
546 switch(server_lock("cnid_metad", _PATH_CNID_METAD_LOCK, 0)) {
548 daemon_exit(EXITERR_SYS);
551 default: /* server */
555 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
560 LOG(log_debug, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
562 if (SWITCH_TO_GID(gid) < 0) {
563 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
568 if (SWITCH_TO_UID(uid) < 0) {
569 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
578 sigprocmask(SIG_SETMASK, NULL, &set);
579 sigdelset(&set, SIGCHLD);
582 rqstfd = usockfd_check(srvfd, &set);
583 /* Collect zombie processes and log what happened to them */
584 if (sigchild) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
585 for (i = 0; i < MAXVOLS; i++) {
586 if (srv[i].pid == pid) {
588 close(srv[i].control_fd);
592 if (WIFEXITED(status)) {
593 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
594 pid, WEXITSTATUS(status));
596 else if (WIFSIGNALED(status)) {
597 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
598 pid, WTERMSIG(status));
605 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
606 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
607 ret = readt(rqstfd, &len, sizeof(int), 1, 5);
613 LOG(log_severe, logtype_cnid, "error read: %s", strerror(errno));
616 else if (ret != sizeof(int)) {
617 LOG(log_error, logtype_cnid, "short read: got %d", ret);
621 * checks for buffer overruns. The client libatalk side does it too
622 * before handing the dir path over but who trusts clients?
624 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
625 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
629 actual_len = readt(rqstfd, dbdir, len, 1, 5);
630 if (actual_len < 0) {
631 LOG(log_severe, logtype_cnid, "Read(2) error : %s", strerror(errno));
634 if (actual_len != len) {
635 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
640 if (set_dbdir(dbdir, len) < 0) {
644 if ((dbp = db_param_read(dbdir, METAD)) == NULL) {
645 LOG(log_error, logtype_cnid, "Error reading config file");
648 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);