2 * $Id: cnid_metad.c,v 1.20 2009-10-29 11:19:23 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 */
35 #undef _USE_GNU_SOURCE
36 #define _USE_GNU_SOURCE
39 #endif /* HAVE_UNISTD_H */
40 #include <sys/param.h>
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
47 #ifdef HAVE_SYS_TIME_H
50 #ifdef HAVE_SYS_WAIT_H
58 #include <sys/socket.h>
61 #include <sys/ioctl.h>
64 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
65 #endif /* ! WEXITSTATUS */
67 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
68 #endif /* ! WIFEXITED */
70 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
74 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
77 #define WTERMSIG(status) ((status) & 0x7f)
80 /* functions for username and group */
87 #define USE_SETRESUID 1
88 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
89 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
94 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
95 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
99 #include <atalk/util.h>
100 #include <atalk/logger.h>
101 #include <atalk/cnid_dbd_private.h>
103 #include "db_param.h"
106 #define DBHOME ".AppleDB"
111 static volatile sig_atomic_t sigchild = 0;
113 #define MAXSPAWN 3 /* Max times respawned in.. */
114 #define TESTTIME 42 /* this much seconds apfd client tries to *
115 * to reconnect every 5 secondes, catch it */
117 #define DEFAULTHOST "localhost"
118 #define DEFAULTPORT 4700
123 time_t tm; /* When respawned last */
124 int count; /* Times respawned in the last TESTTIME secondes */
125 int control_fd; /* file descriptor to child cnid_dbd process */
128 static struct server srv[MAXVOLS];
130 /* Default logging config: log to syslog with level log_note */
131 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
133 static struct server *test_usockfn(char *dir)
136 for (i = 0; i < MAXVOLS; i++) {
137 if (srv[i].name && !strcmp(srv[i].name, dir)) {
144 /* -------------------- */
145 static int send_cred(int socket, int fd)
150 struct cmsghdr *cmsgp = NULL;
155 size = CMSG_SPACE(sizeof fd);
158 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
162 memset(&msgh,0,sizeof (msgh));
165 msgh.msg_name = NULL;
166 msgh.msg_namelen = 0;
171 iov[0].iov_base = &er;
172 iov[0].iov_len = sizeof(er);
174 msgh.msg_control = buf;
175 msgh.msg_controllen = size;
177 cmsgp = CMSG_FIRSTHDR(&msgh);
178 cmsgp->cmsg_level = SOL_SOCKET;
179 cmsgp->cmsg_type = SCM_RIGHTS;
180 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
182 *((int *)CMSG_DATA(cmsgp)) = fd;
183 msgh.msg_controllen = cmsgp->cmsg_len;
186 ret = sendmsg(socket,&msgh, 0);
187 } while ( ret == -1 && errno == EINTR );
189 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
197 /* -------------------- */
198 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
208 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: dbdir: '%s', UNIX socket file: '%s'",
211 up = test_usockfn(dbdir);
213 /* we already have a process, send our fd */
214 if (send_cred(up->control_fd, rqstfd) < 0) {
221 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet. Starting one ...");
225 /* find an empty slot */
226 for (i = 0; i < MAXVOLS; i++) {
227 if ( !srv[i].name ) {
231 up->name = strdup(dbdir);
236 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
241 /* we have a slot but no process, check for respawn too fast */
242 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
244 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
245 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
246 return -1; /* just exit, dont sleep, because we might have work to do for another client */
248 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
249 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
254 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
255 if (up->count > MAXSPAWN) {
256 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
257 we will just return -1 above */
258 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
263 Create socketpair for comm between parent and child.
264 We use it to pass fds from connecting afpd processes to our
265 cnid_dbd child via fd passing.
267 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
268 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
272 if ((pid = fork()) < 0) {
273 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
279 * Child. Close descriptors and start the daemon. If it fails
280 * just log it. The client process will fail connecting
287 for (i = 0; i < MAXVOLS; i++) {
288 if (srv[i].pid && up != &srv[i]) {
289 close(srv[i].control_fd);
293 sprintf(buf1, "%i", sv[1]);
294 sprintf(buf2, "%i", rqstfd);
296 if (up->count == MAXSPAWN) {
297 /* there's a pb with the db inform child
298 * it will run recover, delete the db whatever
300 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
301 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, logconfig, NULL);
304 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, logconfig, NULL);
307 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
316 up->control_fd = sv[0];
320 /* ------------------ */
321 static int set_dbdir(char *dbdir, int len)
328 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
329 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
333 if (dbdir[len - 1] != '/') {
337 strcpy(dbdir + len, DBHOME);
338 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
339 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
345 /* ------------------ */
346 static uid_t user_to_uid (char *username)
348 struct passwd *this_passwd;
350 /* check for anything */
351 if ( !username || strlen ( username ) < 1 ) return 0;
353 /* grab the /etc/passwd record relating to username */
354 this_passwd = getpwnam ( username );
356 /* return false if there is no structure returned */
357 if (this_passwd == NULL) return 0;
359 /* return proper uid */
360 return this_passwd->pw_uid;
364 /* ------------------ */
365 static gid_t group_to_gid ( char *group)
367 struct group *this_group;
369 /* check for anything */
370 if ( !group || strlen ( group ) < 1 ) return 0;
372 /* grab the /etc/groups record relating to group */
373 this_group = getgrnam ( group );
375 /* return false if there is no structure returned */
376 if (this_group == NULL) return 0;
378 /* return proper gid */
379 return this_group->gr_gid;
383 /* ------------------ */
384 static void catch_child(int sig _U_)
389 /* ----------------------- */
390 static void set_signal(void)
395 signal(SIGPIPE, SIG_IGN);
397 sv.sa_handler = catch_child;
398 sv.sa_flags = SA_NOCLDSTOP;
399 sigemptyset(&sv.sa_mask);
400 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
401 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
404 /* block everywhere but in pselect */
406 sigaddset(&set, SIGCHLD);
407 sigprocmask(SIG_BLOCK, &set, NULL);
410 /* ------------------ */
411 int main(int argc, char *argv[])
413 char dbdir[MAXPATHLEN + 1];
417 char *dbdpn = _PATH_CNID_DBD;
418 char *host = DEFAULTHOST;
419 u_int16_t port = DEFAULTPORT;
420 struct db_param *dbp;
428 char *loglevel = NULL;
429 char *logfile = NULL;
432 set_processname("cnid_metad");
434 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:f:")) != -1 ) {
440 host = strdup(optarg);
443 uid = user_to_uid (optarg);
445 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
450 gid =group_to_gid (optarg);
452 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
460 dbdpn = strdup(optarg);
463 loglevel = strdup(optarg);
466 logfile = strdup(optarg);
475 strlcpy(logconfig + 8, loglevel, 13);
477 strcat(logconfig, " ");
480 strlcat(logconfig, logfile, MAXPATHLEN);
486 LOG(log_error, logtype_cnid, "main: bad arguments");
501 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
502 (void)ioctl( i, TIOCNOTTY, 0 );
503 setpgid( 0, getpid());
508 setpgid( 0, getpid());
511 case -1 : /* error */
512 LOG(log_error, logtype_cnid, "detach from terminal: %s", strerror(errno));
514 default : /* server */
519 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
524 LOG(log_info, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
526 if (SWITCH_TO_GID(gid) < 0) {
527 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
532 if (SWITCH_TO_UID(uid) < 0) {
533 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
542 sigprocmask(SIG_SETMASK, NULL, &set);
543 sigdelset(&set, SIGCHLD);
546 rqstfd = usockfd_check(srvfd, &set);
547 /* Collect zombie processes and log what happened to them */
548 if (sigchild) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
549 for (i = 0; i < MAXVOLS; i++) {
550 if (srv[i].pid == pid) {
552 close(srv[i].control_fd);
556 if (WIFEXITED(status)) {
557 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
558 pid, WEXITSTATUS(status));
560 else if (WIFSIGNALED(status)) {
561 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
562 pid, WTERMSIG(status));
569 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
570 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
571 ret = read(rqstfd, &len, sizeof(int));
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 = read(rqstfd, dbdir, len);
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 if (set_dbdir(dbdir, len) < 0) {
608 if ((dbp = db_param_read(dbdir, METAD)) == NULL) {
609 LOG(log_error, logtype_cnid, "Error reading config file");
612 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);