2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/select.h>
44 #include "sysevents.h"
51 struct module *modchain = NULL;
52 static struct timer *timers = NULL;
53 static struct child *children = NULL;
56 static volatile int childrendone = 0;
58 struct timer *timercallback(double at, void (*func)(int, void *), void *data)
62 new = smalloc(sizeof(*new));
74 void canceltimer(struct timer *timer)
76 if(timer->next != NULL)
77 timer->next->prev = timer->prev;
78 if(timer->prev != NULL)
79 timer->prev->next = timer->next;
82 timer->func(1, timer->data);
86 void childcallback(pid_t pid, void (*func)(pid_t, int, void *), void *data)
90 new = smalloc(sizeof(*new));
102 static void preinit(int hup)
106 for(mod = modchain; mod != NULL; mod = mod->next)
110 if(!hup && ((mod->conf.vars != NULL) || (mod->conf.cmds != NULL)))
111 confregmod(&mod->conf);
115 static void init(int hup)
119 for(mod = modchain; mod != NULL; mod = mod->next)
121 if(mod->init && mod->init(hup))
123 flog(LOG_CRIT, "initialization of \"%s\" failed", mod->name);
129 static void terminate(void)
133 for(mod = modchain; mod != NULL; mod = mod->next)
140 static void handler(int signum)
146 extern int numfnetnodes, numtransfers, numdcpeers;
158 while((pid = waitpid(-1, &status, WNOHANG)) > 0)
160 for(child = children; child != NULL; child = child->next)
162 if(child->pid == pid)
165 child->status = status;
172 flog(LOG_NOTICE, "forking and dumping core upon SIGUSR1");
177 flog(LOG_NOTICE, "dumping memstats to /tmp/dc-mem upon SIGUSR2");
178 if((dumpfile = fopen("/tmp/dc-mem", "w")) == NULL) {
179 flog(LOG_ERR, "could not dump stats: %s", strerror(errno));
182 fprintf(dumpfile, "%i %i %i\n", numfnetnodes, numtransfers, numdcpeers);
188 pid_t forksess(uid_t user, struct authhandle *auth, void (*ccbfunc)(pid_t, int, void *), void *data, ...)
197 int maxfd, type, numfiles;
200 struct passwd *pwent;
207 if((pwent = getpwuid(user)) == NULL)
209 flog(LOG_WARNING, "no passwd entry for uid %i, cannot fork session", user);
213 if((geteuid() != 0) && (user != geteuid()))
215 flog(LOG_WARNING, "cannot fork non-owning session when not running as root (EUID is %i, target UID is %i)", geteuid(), user);
219 va_start(args, data);
223 while((type = va_arg(args, int)) != FD_END)
225 files = srealloc(files, sizeof(*files) * (numfiles + 1));
226 files[numfiles].fd = va_arg(args, int);
227 if(files[numfiles].fd > maxfd)
228 maxfd = files[numfiles].fd;
229 acc = va_arg(args, int);
234 flog(LOG_CRIT, "could not create pipe(!): %s", strerror(errno));
235 for(i = 0; i < numfiles; i++)
239 ibuf = va_arg(args, int *);
243 files[numfiles].tfd = cpipe[0];
246 files[numfiles].tfd = cpipe[1];
248 } else if(type == FD_FILE) {
249 buf = va_arg(args, char *);
250 if((files[numfiles].tfd = open(buf, acc)) < 0)
252 flog(LOG_CRIT, "could not open file \"%s\": %s", buf, strerror(errno));
253 for(i = 0; i < numfiles; i++)
258 if(files[numfiles].tfd > maxfd)
259 maxfd = files[numfiles].tfd;
263 sigemptyset(&sigset);
264 sigaddset(&sigset, SIGCHLD);
265 sigprocmask(SIG_BLOCK, &sigset, NULL);
266 if((pid = fork()) < 0)
268 flog(LOG_WARNING, "could not fork(!) in forksess(): %s", strerror(errno));
269 for(i = 0; i < numfiles; i++)
271 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
275 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
276 signal(SIGPIPE, SIG_DFL);
277 signal(SIGCHLD, SIG_DFL);
278 signal(SIGINT, SIG_DFL);
279 signal(SIGTERM, SIG_DFL);
280 signal(SIGHUP, SIG_DFL);
281 for(i = 0; i < numfiles; i++)
283 if(dup2(files[i].tfd, maxfd + i + 1) < 0)
285 files[i].tfd = maxfd + i + 1;
287 for(i = 0; i < numfiles; i++)
289 if(dup2(files[i].tfd, files[i].fd) < 0)
293 for(i = 0; i < FD_SETSIZE; i++)
297 for(o = 0; o < numfiles; o++)
309 signal(SIGHUP, SIG_IGN);
312 keyctl_join_session_keyring(NULL);
313 keyctl_chown(KEY_SPEC_SESSION_KEYRING, pwent->pw_uid, pwent->pw_gid);
315 if((authopensess(auth)) != AUTH_SUCCESS)
317 flog(LOG_WARNING, "could not open session for user %s: %s", pwent->pw_name, (errno == 0)?"Unknown error - should be logged above":strerror(errno));
320 if((pid = fork()) < 0)
329 if(initgroups(pwent->pw_name, pwent->pw_gid))
331 flog(LOG_WARNING, "could not initgroups: %s", strerror(errno));
334 if(setgid(pwent->pw_gid))
336 flog(LOG_WARNING, "could not setgid: %s", strerror(errno));
339 if(setuid(pwent->pw_uid))
341 flog(LOG_WARNING, "could not setuid: %s", strerror(errno));
344 putenv(sprintf2("HOME=%s", pwent->pw_dir));
345 putenv(sprintf2("SHELL=%s", pwent->pw_shell));
346 putenv(sprintf2("PATH=%s/bin:/usr/local/bin:/bin:/usr/bin", pwent->pw_dir));
348 putenv(sprintf2("USER=%s", pwent->pw_name));
349 putenv(sprintf2("LOGNAME=%s", pwent->pw_name));
350 chdir(pwent->pw_dir);
353 for(i = 0; i < numfiles; i++)
355 while(((ret = waitpid(pid, &status, 0)) != pid) && (ret >= 0));
359 flog(LOG_WARNING, "waitpid(%i) said \"%s\"", pid, strerror(errno));
362 if(!WIFEXITED(status))
364 exit(WEXITSTATUS(status));
366 for(i = 0; i < numfiles; i++)
371 childcallback(pid, ccbfunc, data);
372 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
376 int main(int argc, char **argv)
382 FILE *pfstream, *confstream;
383 int delay, immsyslog;
390 immsyslog = nofork = 0;
391 syslogfac = LOG_DAEMON;
394 while((c = getopt(argc, argv, "p:C:f:hnsV")) != -1)
405 if(!strcmp(optarg, "auth"))
406 syslogfac = LOG_AUTH;
407 else if(!strcmp(optarg, "authpriv"))
408 syslogfac = LOG_AUTHPRIV;
409 else if(!strcmp(optarg, "cron"))
410 syslogfac = LOG_CRON;
411 else if(!strcmp(optarg, "daemon"))
412 syslogfac = LOG_DAEMON;
413 else if(!strcmp(optarg, "ftp"))
415 else if(!strcmp(optarg, "kern"))
416 syslogfac = LOG_KERN;
417 else if(!strcmp(optarg, "lpr"))
419 else if(!strcmp(optarg, "mail"))
420 syslogfac = LOG_MAIL;
421 else if(!strcmp(optarg, "news"))
422 syslogfac = LOG_NEWS;
423 else if(!strcmp(optarg, "syslog"))
424 syslogfac = LOG_SYSLOG;
425 else if(!strcmp(optarg, "user"))
426 syslogfac = LOG_USER;
427 else if(!strcmp(optarg, "uucp"))
428 syslogfac = LOG_UUCP;
429 else if(!strncmp(optarg, "local", 5) && (strlen(optarg) == 6))
430 syslogfac = LOG_LOCAL0 + (optarg[5] - '0');
432 fprintf(stderr, "unknown syslog facility %s, using daemon\n", optarg);
441 printf("%s", RELEASEINFO);
447 printf("usage: doldacond [-hnsV] [-C configfile] [-p pidfile] [-f facility]\n");
451 setlocale(LC_ALL, "");
458 signal(SIGPIPE, SIG_IGN);
459 signal(SIGHUP, handler);
460 signal(SIGINT, handler);
461 signal(SIGTERM, handler);
462 signal(SIGCHLD, handler);
463 signal(SIGUSR1, handler);
464 signal(SIGUSR2, handler);
466 if(configfile == NULL)
468 if((configfile = findfile("doldacond.conf", NULL, 0)) == NULL)
470 flog(LOG_CRIT, "could not find a configuration file");
477 if((pfstream = fopen(pidfile, "w")) == NULL)
479 flog(LOG_CRIT, "could not open specified PID file %s: %s", pidfile, strerror(errno));
483 if((confstream = fopen(configfile, "r")) == NULL)
485 flog(LOG_CRIT, "could not open configuration file %s: %s", configfile, strerror(errno));
488 readconfig(confstream);
495 flog(LOG_INFO, "daemonized");
498 if(pfstream != NULL) {
499 fprintf(pfstream, "%i\n", getpid());
502 flog(LOG_INFO, "startup took %f seconds", ntime() - now);
509 if((confstream = fopen(configfile, "r")) == NULL)
511 flog(LOG_ERR, "could not open configuration file %s: %s (ignoring HUP)", configfile, strerror(errno));
514 readconfig(confstream);
520 delay = 1000; /* -1; */
521 for(mod = modchain; mod != NULL; mod = mod->next)
523 if(mod->run && mod->run())
531 for(timer = timers; timer != NULL; timer = timer->next)
533 if((delay == -1) || ((int)((timer->at - now) * 1000.0) < delay))
534 delay = (int)((timer->at - now) * 1000.0);
546 for(timer = timers; timer != NULL; timer = timer->next)
550 if(timer->prev != NULL)
551 timer->prev->next = timer->next;
552 if(timer->next != NULL)
553 timer->next->prev = timer->prev;
555 timers = timer->next;
556 timer->func(0, timer->data);
560 } while(timer != NULL);
563 for(child = children; child != NULL; child = child->next)
567 child->callback(child->pid, child->status, child->data);
568 if(child == children)
569 children = child->next;
570 if(child->prev != NULL)
571 child->prev->next = child->next;
572 if(child->next != NULL)
573 child->next->prev = child->prev;
578 } while(child != NULL);
580 flog(LOG_INFO, "terminating...");