2 * $Id: cnid_metad.c,v 1.13 2009-07-21 13:41:16 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 */
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/util.h>
103 #include <atalk/logger.h>
104 #include <atalk/cnid_dbd_private.h>
106 #include "db_param.h"
109 #define DBHOME ".AppleDB"
114 volatile sig_atomic_t alarmed = 0;
116 #define MAXSPAWN 3 /* Max times respawned in.. */
117 #define TESTTIME 42 /* this much seconds apfd client tries to *
118 * to reconnect every 5 secondes, catch it */
120 #define DEFAULTHOST "localhost"
121 #define DEFAULTPORT 4700
126 time_t tm; /* When respawned last */
127 int count; /* Times respawned in the last TESTTIME secondes */
128 int control_fd; /* file descriptor to child cnid_dbd process */
131 static struct server srv[MAXVOLS];
133 /* Default logging config: log to syslog with level log_note */
134 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
136 static struct server *test_usockfn(char *dir)
139 for (i = 0; i < MAXVOLS; i++) {
140 if (srv[i].name && !strcmp(srv[i].name, dir)) {
147 /* -------------------- */
148 static int send_cred(int socket, int fd)
153 struct cmsghdr *cmsgp = NULL;
158 size = CMSG_SPACE(sizeof fd);
161 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
165 memset(&msgh,0,sizeof (msgh));
168 msgh.msg_name = NULL;
169 msgh.msg_namelen = 0;
174 iov[0].iov_base = &er;
175 iov[0].iov_len = sizeof(er);
177 msgh.msg_control = buf;
178 msgh.msg_controllen = size;
180 cmsgp = CMSG_FIRSTHDR(&msgh);
181 cmsgp->cmsg_level = SOL_SOCKET;
182 cmsgp->cmsg_type = SCM_RIGHTS;
183 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
185 *((int *)CMSG_DATA(cmsgp)) = fd;
186 msgh.msg_controllen = cmsgp->cmsg_len;
189 ret = sendmsg(socket,&msgh, 0);
190 } while ( ret == -1 && errno == EINTR );
192 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
200 /* -------------------- */
201 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
211 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: dbdir: '%s', UNIX socket file: '%s'",
214 up = test_usockfn(dbdir);
216 /* we already have a process, send our fd */
217 if (send_cred(up->control_fd, rqstfd) < 0) {
224 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet. Starting one ...");
228 /* find an empty slot */
229 for (i = 0; i < MAXVOLS; i++) {
230 if ( !srv[i].name ) {
234 up->name = strdup(dbdir);
239 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
244 /* we have a slot but no process, check for respawn too fast */
245 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
247 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
248 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
249 return -1; /* just exit, dont sleep, because we might have work to do for another client */
251 if ( t >= (up->tm + TESTTIME) ) { /* drop slot */
252 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended, dropping slot");
255 return -1; /* next time we'll try again with a new slot */
258 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
259 if (up->count > MAXSPAWN) {
260 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
261 we will just return -1 above */
262 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
267 Create socketpair for comm between parent and child.
268 We use it to pass fds from connecting afpd processes to our
269 cnid_dbd child via fd passing.
271 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
272 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
276 if ((pid = fork()) < 0) {
277 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
283 * Child. Close descriptors and start the daemon. If it fails
284 * just log it. The client process will fail connecting
291 for (i = 0; i < MAXVOLS; i++) {
292 if (srv[i].pid && up != &srv[i]) {
293 close(srv[i].control_fd);
297 sprintf(buf1, "%i", sv[1]);
298 sprintf(buf2, "%i", rqstfd);
300 if (up->count == MAXSPAWN) {
301 /* there's a pb with the db inform child
302 * it will run recover, delete the db whatever
304 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
305 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, logconfig, NULL);
308 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, logconfig, NULL);
311 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
320 up->control_fd = sv[0];
324 /* ------------------ */
325 static int set_dbdir(char *dbdir, int len)
332 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
333 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
337 if (dbdir[len - 1] != '/') {
341 strcpy(dbdir + len, DBHOME);
342 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
343 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
349 /* ------------------ */
350 uid_t user_to_uid ( username )
353 struct passwd *this_passwd;
355 /* check for anything */
356 if ( !username || strlen ( username ) < 1 ) return 0;
358 /* grab the /etc/passwd record relating to username */
359 this_passwd = getpwnam ( username );
361 /* return false if there is no structure returned */
362 if (this_passwd == NULL) return 0;
364 /* return proper uid */
365 return this_passwd->pw_uid;
369 /* ------------------ */
370 gid_t group_to_gid ( group )
373 struct group *this_group;
375 /* check for anything */
376 if ( !group || strlen ( group ) < 1 ) return 0;
378 /* grab the /etc/groups record relating to group */
379 this_group = getgrnam ( group );
381 /* return false if there is no structure returned */
382 if (this_group == NULL) return 0;
384 /* return proper gid */
385 return this_group->gr_gid;
389 /* ------------------ */
390 void catch_alarm(int sig _U_) {
394 /* ------------------ */
395 int main(int argc, char *argv[])
397 char dbdir[MAXPATHLEN + 1];
401 char *dbdpn = _PATH_CNID_DBD;
402 char *host = DEFAULTHOST;
403 u_int16_t port = DEFAULTPORT;
404 struct db_param *dbp;
412 char *loglevel = NULL;
413 char *logfile = NULL;
415 set_processname("cnid_metad");
417 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:f:")) != -1 ) {
423 host = strdup(optarg);
426 uid = user_to_uid (optarg);
428 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
433 gid =group_to_gid (optarg);
435 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
443 dbdpn = strdup(optarg);
446 loglevel = strdup(optarg);
449 logfile = strdup(optarg);
458 strlcpy(logconfig + 8, loglevel, 13);
460 strcat(logconfig, " ");
463 strlcat(logconfig, logfile, MAXPATHLEN);
469 LOG(log_error, logtype_cnid, "main: bad arguments");
484 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
485 (void)ioctl( i, TIOCNOTTY, 0 );
486 setpgid( 0, getpid());
491 setpgid( 0, getpid());
494 case -1 : /* error */
495 LOG(log_error, logtype_cnid, "detach from terminal: %s", strerror(errno));
497 default : /* server */
502 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
507 LOG(log_info, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
509 if (SWITCH_TO_GID(gid) < 0) {
510 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
515 if (SWITCH_TO_UID(uid) < 0) {
516 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
522 signal(SIGPIPE, SIG_IGN);
523 signal(SIGALRM, catch_alarm);
526 rqstfd = usockfd_check(srvfd, 10000000);
527 /* Collect zombie processes and log what happened to them */
528 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
529 for (i = 0; i < MAXVOLS; i++) {
530 if (srv[i].pid == pid) {
532 close(srv[i].control_fd);
536 if (WIFEXITED(status)) {
537 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
538 pid, WEXITSTATUS(status));
540 else if (WIFSIGNALED(status)) {
541 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
542 pid, WTERMSIG(status));
550 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
551 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
552 alarm(5); /* to prevent read from getting stuck */
553 ret = read(rqstfd, &len, sizeof(int));
557 LOG(log_severe, logtype_cnid, "Read(1) bailed with alarm (timeout)");
566 LOG(log_error, logtype_cnid, "error read: %s", strerror(errno));
569 else if (ret != sizeof(int)) {
570 LOG(log_error, logtype_cnid, "short read: got %d", ret);
574 * checks for buffer overruns. The client libatalk side does it too
575 * before handing the dir path over but who trusts clients?
577 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
578 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
583 actual_len = read(rqstfd, dbdir, len);
587 LOG(log_severe, logtype_cnid, "Read(2) bailed with alarm (timeout)");
590 if (actual_len != len) {
591 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
596 if (set_dbdir(dbdir, len) < 0) {
600 if ((dbp = db_param_read(dbdir, METAD)) == NULL) {
601 LOG(log_error, logtype_cnid, "Error reading config file");
604 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);