2 * $Id: cnid_metad.c,v 1.10 2009-05-20 10:36:09 franklahm 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 */
38 #endif /* HAVE_UNISTD_H */
39 #include <sys/param.h>
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
46 #ifdef HAVE_SYS_TIME_H
49 #ifdef HAVE_SYS_WAIT_H
57 #include <sys/socket.h>
60 #include <sys/ioctl.h>
63 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
64 #endif /* ! WEXITSTATUS */
66 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
67 #endif /* ! WIFEXITED */
69 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
73 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
76 #define WTERMSIG(status) ((status) & 0x7f)
80 #define fork aTaC_fork
83 /* functions for username and group */
90 #define USE_SETRESUID 1
91 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
92 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
97 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
98 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
102 #include <atalk/logger.h>
103 #include <atalk/cnid_dbd_private.h>
105 #include "db_param.h"
108 #define DBHOME ".AppleDB"
113 volatile sig_atomic_t alarmed = 0;
115 #define MAXSPAWN 3 /* Max times respawned in.. */
116 #define TESTTIME 42 /* this much seconds apfd client tries to *
117 * to reconnect every 5 secondes, catch it */
119 #define DEFAULTHOST "localhost"
120 #define DEFAULTPORT 4700
125 time_t tm; /* When respawned last */
126 int count; /* Times respawned in the last TESTTIME secondes */
127 int control_fd; /* file descriptor to child cnid_dbd process */
130 static struct server srv[MAXVOLS];
132 /* Default logging config: log to syslog with level log_note */
133 static char *logconfig = "default log_note";
135 static struct server *test_usockfn(char *dir)
138 for (i = 0; i < MAXVOLS; i++) {
139 if (srv[i].name && !strcmp(srv[i].name, dir)) {
146 /* -------------------- */
147 static int send_cred(int socket, int fd)
152 struct cmsghdr *cmsgp = NULL;
157 size = CMSG_SPACE(sizeof fd);
160 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
164 memset(&msgh,0,sizeof (msgh));
167 msgh.msg_name = NULL;
168 msgh.msg_namelen = 0;
173 iov[0].iov_base = &er;
174 iov[0].iov_len = sizeof(er);
176 msgh.msg_control = buf;
177 msgh.msg_controllen = size;
179 cmsgp = CMSG_FIRSTHDR(&msgh);
180 cmsgp->cmsg_level = SOL_SOCKET;
181 cmsgp->cmsg_type = SCM_RIGHTS;
182 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
184 *((int *)CMSG_DATA(cmsgp)) = fd;
185 msgh.msg_controllen = cmsgp->cmsg_len;
188 ret = sendmsg(socket,&msgh, 0);
189 } while ( ret == -1 && errno == EINTR );
191 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
199 /* -------------------- */
200 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
210 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: dbdir: '%s', UNIX socket file: '%s'",
213 up = test_usockfn(dbdir);
215 /* we already have a process, send our fd */
216 if (send_cred(up->control_fd, rqstfd) < 0) {
223 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet. Starting one ...");
227 /* find an empty slot */
228 for (i = 0; i < MAXVOLS; i++) {
233 up->name = strdup(dbdir);
238 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
243 /* we have a slot but no process, check for respawn too fast */
244 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
246 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
247 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
248 return -1; /* just exit, dont sleep, because we might have work to do for another client */
250 if ( t >= (up->tm + TESTTIME) ) { /* drop slot */
251 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended, dropping slot");
254 return -1; /* next time we'll try again with a new slot */
257 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
258 if (up->count > MAXSPAWN) {
259 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
260 we will just return -1 above */
261 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
266 Create socketpair for comm between parent and child.
267 We use it to pass fds from connecting afpd processes to our
268 cnid_dbd child via fd passing.
270 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
271 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
275 if ((pid = fork()) < 0) {
276 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
282 * Child. Close descriptors and start the daemon. If it fails
283 * just log it. The client process will fail connecting
290 for (i = 0; i < MAXVOLS; i++) {
291 if (srv[i].pid && up != &srv[i]) {
292 close(srv[i].control_fd);
296 sprintf(buf1, "%i", sv[1]);
297 sprintf(buf2, "%i", rqstfd);
299 if (up->count == MAXSPAWN) {
300 /* there's a pb with the db inform child
301 * it will run recover, delete the db whatever
303 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
304 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, logconfig, NULL);
307 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, logconfig, NULL);
310 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
319 up->control_fd = sv[0];
323 /* ------------------ */
324 static int set_dbdir(char *dbdir, int len)
331 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
332 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
336 if (dbdir[len - 1] != '/') {
340 strcpy(dbdir + len, DBHOME);
341 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
342 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
348 /* ------------------ */
349 uid_t user_to_uid ( username )
352 struct passwd *this_passwd;
354 /* check for anything */
355 if ( !username || strlen ( username ) < 1 ) return 0;
357 /* grab the /etc/passwd record relating to username */
358 this_passwd = getpwnam ( username );
360 /* return false if there is no structure returned */
361 if (this_passwd == NULL) return 0;
363 /* return proper uid */
364 return this_passwd->pw_uid;
368 /* ------------------ */
369 gid_t group_to_gid ( group )
372 struct group *this_group;
374 /* check for anything */
375 if ( !group || strlen ( group ) < 1 ) return 0;
377 /* grab the /etc/groups record relating to group */
378 this_group = getgrnam ( group );
380 /* return false if there is no structure returned */
381 if (this_group == NULL) return 0;
383 /* return proper gid */
384 return this_group->gr_gid;
388 /* ------------------ */
389 void catch_alarm(int sig) {
393 /* ------------------ */
394 int main(int argc, char *argv[])
396 char dbdir[MAXPATHLEN + 1];
400 char *dbdpn = _PATH_CNID_DBD;
401 char *host = DEFAULTHOST;
402 u_int16_t port = DEFAULTPORT;
403 struct db_param *dbp;
412 set_processname("cnid_metad");
414 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:")) != -1 ) {
420 host = strdup(optarg);
423 uid = user_to_uid (optarg);
425 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
430 gid =group_to_gid (optarg);
432 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
440 dbdpn = strdup(optarg);
443 logconfig = strdup(optarg);
454 LOG(log_error, logtype_cnid, "main: bad arguments");
469 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
470 (void)ioctl( i, TIOCNOTTY, 0 );
471 setpgid( 0, getpid());
476 setpgid( 0, getpid());
479 case -1 : /* error */
480 LOG(log_error, logtype_cnid, "detach from terminal: %s", strerror(errno));
482 default : /* server */
487 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
492 LOG(log_info, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
494 if (SWITCH_TO_GID(gid) < 0) {
495 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
500 if (SWITCH_TO_UID(uid) < 0) {
501 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
507 signal(SIGPIPE, SIG_IGN);
508 signal(SIGALRM, catch_alarm);
511 rqstfd = usockfd_check(srvfd, 10000000);
512 /* Collect zombie processes and log what happened to them */
513 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
514 for (i = 0; i < MAXVOLS; i++) {
515 if (srv[i].pid == pid) {
517 close(srv[i].control_fd);
521 if (WIFEXITED(status)) {
522 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
523 pid, WEXITSTATUS(status));
525 else if (WIFSIGNALED(status)) {
526 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
527 pid, WTERMSIG(status));
535 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
536 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
537 alarm(5); /* to prevent read from getting stuck */
538 ret = read(rqstfd, &len, sizeof(int));
542 LOG(log_severe, logtype_cnid, "Read(1) bailed with alarm (timeout)");
551 LOG(log_error, logtype_cnid, "error read: %s", strerror(errno));
554 else if (ret != sizeof(int)) {
555 LOG(log_error, logtype_cnid, "short read: got %d", ret);
559 * checks for buffer overruns. The client libatalk side does it too
560 * before handing the dir path over but who trusts clients?
562 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
563 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
568 actual_len = read(rqstfd, dbdir, len);
572 LOG(log_severe, logtype_cnid, "Read(2) bailed with alarm (timeout)");
575 if (actual_len != len) {
576 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
581 if (set_dbdir(dbdir, len) < 0) {
585 if ((dbp = db_param_read(dbdir, METAD)) == NULL) {
586 LOG(log_error, logtype_cnid, "Error reading config file");
589 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);