2 * $Id: cnid_metad.c,v 1.14 2009-09-14 00:02:21 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)
79 /* functions for username and group */
86 #define USE_SETRESUID 1
87 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
88 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
93 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
94 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
98 #include <atalk/util.h>
99 #include <atalk/logger.h>
100 #include <atalk/cnid_dbd_private.h>
102 #include "db_param.h"
105 #define DBHOME ".AppleDB"
110 volatile sig_atomic_t alarmed = 0;
112 #define MAXSPAWN 3 /* Max times respawned in.. */
113 #define TESTTIME 42 /* this much seconds apfd client tries to *
114 * to reconnect every 5 secondes, catch it */
116 #define DEFAULTHOST "localhost"
117 #define DEFAULTPORT 4700
122 time_t tm; /* When respawned last */
123 int count; /* Times respawned in the last TESTTIME secondes */
124 int control_fd; /* file descriptor to child cnid_dbd process */
127 static struct server srv[MAXVOLS];
129 /* Default logging config: log to syslog with level log_note */
130 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
132 static struct server *test_usockfn(char *dir)
135 for (i = 0; i < MAXVOLS; i++) {
136 if (srv[i].name && !strcmp(srv[i].name, dir)) {
143 /* -------------------- */
144 static int send_cred(int socket, int fd)
149 struct cmsghdr *cmsgp = NULL;
154 size = CMSG_SPACE(sizeof fd);
157 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
161 memset(&msgh,0,sizeof (msgh));
164 msgh.msg_name = NULL;
165 msgh.msg_namelen = 0;
170 iov[0].iov_base = &er;
171 iov[0].iov_len = sizeof(er);
173 msgh.msg_control = buf;
174 msgh.msg_controllen = size;
176 cmsgp = CMSG_FIRSTHDR(&msgh);
177 cmsgp->cmsg_level = SOL_SOCKET;
178 cmsgp->cmsg_type = SCM_RIGHTS;
179 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
181 *((int *)CMSG_DATA(cmsgp)) = fd;
182 msgh.msg_controllen = cmsgp->cmsg_len;
185 ret = sendmsg(socket,&msgh, 0);
186 } while ( ret == -1 && errno == EINTR );
188 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
196 /* -------------------- */
197 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
207 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: dbdir: '%s', UNIX socket file: '%s'",
210 up = test_usockfn(dbdir);
212 /* we already have a process, send our fd */
213 if (send_cred(up->control_fd, rqstfd) < 0) {
220 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet. Starting one ...");
224 /* find an empty slot */
225 for (i = 0; i < MAXVOLS; i++) {
226 if ( !srv[i].name ) {
230 up->name = strdup(dbdir);
235 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
240 /* we have a slot but no process, check for respawn too fast */
241 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
243 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
244 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
245 return -1; /* just exit, dont sleep, because we might have work to do for another client */
247 if ( t >= (up->tm + TESTTIME) ) { /* drop slot */
248 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended, dropping slot");
251 return -1; /* next time we'll try again with a new slot */
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 uid_t user_to_uid ( username )
349 struct passwd *this_passwd;
351 /* check for anything */
352 if ( !username || strlen ( username ) < 1 ) return 0;
354 /* grab the /etc/passwd record relating to username */
355 this_passwd = getpwnam ( username );
357 /* return false if there is no structure returned */
358 if (this_passwd == NULL) return 0;
360 /* return proper uid */
361 return this_passwd->pw_uid;
365 /* ------------------ */
366 gid_t group_to_gid ( group )
369 struct group *this_group;
371 /* check for anything */
372 if ( !group || strlen ( group ) < 1 ) return 0;
374 /* grab the /etc/groups record relating to group */
375 this_group = getgrnam ( group );
377 /* return false if there is no structure returned */
378 if (this_group == NULL) return 0;
380 /* return proper gid */
381 return this_group->gr_gid;
385 /* ------------------ */
386 void catch_alarm(int sig _U_) {
390 /* ------------------ */
391 int main(int argc, char *argv[])
393 char dbdir[MAXPATHLEN + 1];
397 char *dbdpn = _PATH_CNID_DBD;
398 char *host = DEFAULTHOST;
399 u_int16_t port = DEFAULTPORT;
400 struct db_param *dbp;
408 char *loglevel = NULL;
409 char *logfile = NULL;
411 set_processname("cnid_metad");
413 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:f:")) != -1 ) {
419 host = strdup(optarg);
422 uid = user_to_uid (optarg);
424 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
429 gid =group_to_gid (optarg);
431 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
439 dbdpn = strdup(optarg);
442 loglevel = strdup(optarg);
445 logfile = strdup(optarg);
454 strlcpy(logconfig + 8, loglevel, 13);
456 strcat(logconfig, " ");
459 strlcat(logconfig, logfile, MAXPATHLEN);
465 LOG(log_error, logtype_cnid, "main: bad arguments");
480 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
481 (void)ioctl( i, TIOCNOTTY, 0 );
482 setpgid( 0, getpid());
487 setpgid( 0, getpid());
490 case -1 : /* error */
491 LOG(log_error, logtype_cnid, "detach from terminal: %s", strerror(errno));
493 default : /* server */
498 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
503 LOG(log_info, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
505 if (SWITCH_TO_GID(gid) < 0) {
506 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
511 if (SWITCH_TO_UID(uid) < 0) {
512 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
518 signal(SIGPIPE, SIG_IGN);
519 signal(SIGALRM, catch_alarm);
522 rqstfd = usockfd_check(srvfd, 10000000);
523 /* Collect zombie processes and log what happened to them */
524 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
525 for (i = 0; i < MAXVOLS; i++) {
526 if (srv[i].pid == pid) {
528 close(srv[i].control_fd);
532 if (WIFEXITED(status)) {
533 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
534 pid, WEXITSTATUS(status));
536 else if (WIFSIGNALED(status)) {
537 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
538 pid, WTERMSIG(status));
546 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
547 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
548 alarm(5); /* to prevent read from getting stuck */
549 ret = read(rqstfd, &len, sizeof(int));
553 LOG(log_severe, logtype_cnid, "Read(1) bailed with alarm (timeout)");
562 LOG(log_error, logtype_cnid, "error read: %s", strerror(errno));
565 else if (ret != sizeof(int)) {
566 LOG(log_error, logtype_cnid, "short read: got %d", ret);
570 * checks for buffer overruns. The client libatalk side does it too
571 * before handing the dir path over but who trusts clients?
573 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
574 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
579 actual_len = read(rqstfd, dbdir, len);
583 LOG(log_severe, logtype_cnid, "Read(2) bailed with alarm (timeout)");
586 if (actual_len != len) {
587 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
592 if (set_dbdir(dbdir, len) < 0) {
596 if ((dbp = db_param_read(dbdir, METAD)) == NULL) {
597 LOG(log_error, logtype_cnid, "Error reading config file");
600 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);