2 * $Id: cnid_metad.c,v 1.22 2009-11-16 02:04:47 didg Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYING.
10 cnid_dbd metadaemon to start up cnid_dbd upon request from afpd.
14 1. afpd -------> cnid_metad
16 via UNIX domain socket
17 2. cnid_metad -------> cnid_dbd
20 3. cnid_metad -------> cnid_dbd
24 4. afpd -------> cnid_dbd
30 #endif /* HAVE_CONFIG_H */
36 #include <sys/param.h>
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
43 #ifdef HAVE_SYS_TIME_H
46 #ifdef HAVE_SYS_WAIT_H
54 #include <sys/socket.h>
57 #include <sys/ioctl.h>
60 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
61 #endif /* ! WEXITSTATUS */
63 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
64 #endif /* ! WIFEXITED */
66 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
70 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
73 #define WTERMSIG(status) ((status) & 0x7f)
76 /* functions for username and group */
83 #define USE_SETRESUID 1
84 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
85 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
86 #endif /* USE_SETRESUID */
90 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
91 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
92 #endif /* USE_SETEUID */
95 #include <atalk/util.h>
96 #include <atalk/logger.h>
97 #include <atalk/cnid_dbd_private.h>
98 #include <atalk/paths.h>
100 #include "db_param.h"
103 #define DBHOME ".AppleDB"
108 static volatile sig_atomic_t sigchild = 0;
110 #define MAXSPAWN 3 /* Max times respawned in.. */
111 #define TESTTIME 42 /* this much seconds apfd client tries to *
112 * to reconnect every 5 secondes, catch it */
114 #define DEFAULTHOST "localhost"
115 #define DEFAULTPORT "4700"
120 time_t tm; /* When respawned last */
121 int count; /* Times respawned in the last TESTTIME secondes */
122 int control_fd; /* file descriptor to child cnid_dbd process */
125 static struct server srv[MAXVOLS];
127 /* Default logging config: log to syslog with level log_note */
128 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
130 static void daemon_exit(int i)
132 server_unlock(_PATH_CNID_METAD_LOCK);
136 /* ------------------ */
137 static void sigterm_handler(int sig)
141 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
144 LOG(log_error, logtype_afpd, "unexpected signal: %d", sig);
149 static struct server *test_usockfn(char *dir)
152 for (i = 0; i < MAXVOLS; i++) {
153 if (srv[i].name && !strcmp(srv[i].name, dir)) {
160 /* -------------------- */
161 static int send_cred(int socket, int fd)
166 struct cmsghdr *cmsgp = NULL;
171 size = CMSG_SPACE(sizeof fd);
174 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
178 memset(&msgh,0,sizeof (msgh));
181 msgh.msg_name = NULL;
182 msgh.msg_namelen = 0;
187 iov[0].iov_base = &er;
188 iov[0].iov_len = sizeof(er);
190 msgh.msg_control = buf;
191 msgh.msg_controllen = size;
193 cmsgp = CMSG_FIRSTHDR(&msgh);
194 cmsgp->cmsg_level = SOL_SOCKET;
195 cmsgp->cmsg_type = SCM_RIGHTS;
196 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
198 *((int *)CMSG_DATA(cmsgp)) = fd;
199 msgh.msg_controllen = cmsgp->cmsg_len;
202 ret = sendmsg(socket,&msgh, 0);
203 } while ( ret == -1 && errno == EINTR );
205 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
213 /* -------------------- */
214 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
224 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: dbdir: '%s', UNIX socket file: '%s'",
227 up = test_usockfn(dbdir);
229 /* we already have a process, send our fd */
230 if (send_cred(up->control_fd, rqstfd) < 0) {
237 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet. Starting one ...");
241 /* find an empty slot */
242 for (i = 0; i < MAXVOLS; i++) {
243 if ( !srv[i].name ) {
247 up->name = strdup(dbdir);
252 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
257 /* we have a slot but no process, check for respawn too fast */
258 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
260 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
261 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
262 return -1; /* just exit, dont sleep, because we might have work to do for another client */
264 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
265 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
270 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
271 if (up->count > MAXSPAWN) {
272 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
273 we will just return -1 above */
274 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
279 Create socketpair for comm between parent and child.
280 We use it to pass fds from connecting afpd processes to our
281 cnid_dbd child via fd passing.
283 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
284 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
288 if ((pid = fork()) < 0) {
289 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
295 * Child. Close descriptors and start the daemon. If it fails
296 * just log it. The client process will fail connecting
303 for (i = 0; i < MAXVOLS; i++) {
304 if (srv[i].pid && up != &srv[i]) {
305 close(srv[i].control_fd);
309 sprintf(buf1, "%i", sv[1]);
310 sprintf(buf2, "%i", rqstfd);
312 if (up->count == MAXSPAWN) {
313 /* there's a pb with the db inform child
314 * it will run recover, delete the db whatever
316 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
317 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, logconfig, NULL);
320 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, logconfig, NULL);
323 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
332 up->control_fd = sv[0];
336 /* ------------------ */
337 static int set_dbdir(char *dbdir, int len)
344 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
345 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
349 if (dbdir[len - 1] != '/') {
353 strcpy(dbdir + len, DBHOME);
354 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
355 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
361 /* ------------------ */
362 static uid_t user_to_uid (char *username)
364 struct passwd *this_passwd;
366 /* check for anything */
367 if ( !username || strlen ( username ) < 1 ) return 0;
369 /* grab the /etc/passwd record relating to username */
370 this_passwd = getpwnam ( username );
372 /* return false if there is no structure returned */
373 if (this_passwd == NULL) return 0;
375 /* return proper uid */
376 return this_passwd->pw_uid;
380 /* ------------------ */
381 static gid_t group_to_gid ( char *group)
383 struct group *this_group;
385 /* check for anything */
386 if ( !group || strlen ( group ) < 1 ) return 0;
388 /* grab the /etc/groups record relating to group */
389 this_group = getgrnam ( group );
391 /* return false if there is no structure returned */
392 if (this_group == NULL) return 0;
394 /* return proper gid */
395 return this_group->gr_gid;
399 /* ------------------ */
400 static void catch_child(int sig _U_)
405 /* ----------------------- */
406 static void set_signal(void)
411 memset(&sv, 0, sizeof(sv));
414 sv.sa_handler = catch_child;
415 sv.sa_flags = SA_NOCLDSTOP;
416 sigemptyset(&sv.sa_mask);
417 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
418 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
419 daemon_exit(EXITERR_SYS);
423 sv.sa_handler = sigterm_handler;
424 sigfillset(&sv.sa_mask );
425 if (sigaction(SIGTERM, &sv, NULL ) < 0 ) {
426 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
427 daemon_exit(EXITERR_SYS);
430 /* Ignore the rest */
431 sv.sa_handler = SIG_IGN;
432 sigemptyset(&sv.sa_mask );
433 if (sigaction(SIGALRM, &sv, NULL ) < 0 ) {
434 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
435 daemon_exit(EXITERR_SYS);
437 sv.sa_handler = SIG_IGN;
438 sigemptyset(&sv.sa_mask );
439 if (sigaction(SIGHUP, &sv, NULL ) < 0 ) {
440 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
441 daemon_exit(EXITERR_SYS);
443 sv.sa_handler = SIG_IGN;
444 sigemptyset(&sv.sa_mask );
445 if (sigaction(SIGUSR1, &sv, NULL ) < 0 ) {
446 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
447 daemon_exit(EXITERR_SYS);
449 sv.sa_handler = SIG_IGN;
450 sigemptyset(&sv.sa_mask );
451 if (sigaction(SIGUSR2, &sv, NULL ) < 0 ) {
452 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
453 daemon_exit(EXITERR_SYS);
455 sv.sa_handler = SIG_IGN;
456 sigemptyset(&sv.sa_mask );
457 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
458 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
459 daemon_exit(EXITERR_SYS);
462 /* block everywhere but in pselect */
464 sigaddset(&set, SIGCHLD);
465 sigprocmask(SIG_BLOCK, &set, NULL);
468 /* ------------------ */
469 int main(int argc, char *argv[])
471 char dbdir[MAXPATHLEN + 1];
475 char *dbdpn = _PATH_CNID_DBD;
476 char *host = DEFAULTHOST;
477 char *port = DEFAULTPORT;
478 struct db_param *dbp;
486 char *loglevel = NULL;
487 char *logfile = NULL;
490 set_processname("cnid_metad");
492 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:f:")) != -1 ) {
498 host = strdup(optarg);
501 uid = user_to_uid (optarg);
503 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
508 gid =group_to_gid (optarg);
510 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
515 port = strdup(optarg);
518 dbdpn = strdup(optarg);
521 loglevel = strdup(optarg);
524 logfile = strdup(optarg);
533 strlcpy(logconfig + 8, loglevel, 13);
535 strcat(logconfig, " ");
538 strlcat(logconfig, logfile, MAXPATHLEN);
544 LOG(log_error, logtype_cnid, "main: bad arguments");
548 /* Check PID lockfile and become a daemon */
549 switch(server_lock("cnid_metad", _PATH_CNID_METAD_LOCK, 0)) {
551 daemon_exit(EXITERR_SYS);
554 default: /* server */
558 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
563 LOG(log_debug, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
565 if (SWITCH_TO_GID(gid) < 0) {
566 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
571 if (SWITCH_TO_UID(uid) < 0) {
572 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
581 sigprocmask(SIG_SETMASK, NULL, &set);
582 sigdelset(&set, SIGCHLD);
585 rqstfd = usockfd_check(srvfd, &set);
586 /* Collect zombie processes and log what happened to them */
587 if (sigchild) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
588 for (i = 0; i < MAXVOLS; i++) {
589 if (srv[i].pid == pid) {
591 close(srv[i].control_fd);
595 if (WIFEXITED(status)) {
596 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
597 pid, WEXITSTATUS(status));
599 else if (WIFSIGNALED(status)) {
600 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
601 pid, WTERMSIG(status));
608 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
609 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
610 ret = read(rqstfd, &len, sizeof(int));
616 LOG(log_severe, logtype_cnid, "error read: %s", strerror(errno));
619 else if (ret != sizeof(int)) {
620 LOG(log_error, logtype_cnid, "short read: got %d", ret);
624 * checks for buffer overruns. The client libatalk side does it too
625 * before handing the dir path over but who trusts clients?
627 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
628 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
632 actual_len = read(rqstfd, dbdir, len);
633 if (actual_len < 0) {
634 LOG(log_severe, logtype_cnid, "Read(2) error : %s", strerror(errno));
637 if (actual_len != len) {
638 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
643 if (set_dbdir(dbdir, len) < 0) {
647 if ((dbp = db_param_read(dbdir, METAD)) == NULL) {
648 LOG(log_error, logtype_cnid, "Error reading config file");
651 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);