--- ansh/src/anshd.c 2011/10/04 22:37:46 1.1.1.1 +++ ansh/src/anshd.c 2011/10/10 09:11:48 1.1.1.1.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ - * $Id: anshd.c,v 1.1.1.1 2011/10/04 22:37:46 misho Exp $ + * $Id: anshd.c,v 1.1.1.1.2.2 2011/10/10 09:11:48 misho Exp $ * *************************************************************************/ #include "global.h" @@ -12,9 +12,10 @@ intptr_t Kill; -int bpfLEN, Verbose, Crypted = 1; +int Verbose; +u_int Crypted = 1; proc_head_t pH; -int Daemon = 1; +int bpfLEN, Timeout, Daemon = 1; extern char compiled[], compiledby[], compilehost[]; @@ -28,6 +29,7 @@ Usage() "\t-i \tService ID (default is 42)\n" "\t-U \tRun service with other user\n" "\t-C \tRun service into chroot directory\n" + "\t-t \tTimeout of login if no activity (default is 0 sec)\n" "\t-u\t\tSwitch to unencrypted traffic between hosts\n" "\t-b\t\tRun into batch mode (default is daemon mode)\n" "\t-v\t\tVerbose (more -v, more verbosity ...)\n" @@ -42,21 +44,36 @@ sig(int s) switch (s) { case SIGHUP: - LOG("Got SIGHUP!\n"); + VERB(1) LOG("Got SIGHUP!\n"); break; case SIGTERM: - LOG("Got SIGTERM!\n"); Kill++; + VERB(1) LOG("Got SIGTERM!\n"); break; case SIGPIPE: - LOG("Got SIGPIPE!\n"); + VERB(1) LOG("Got SIGPIPE!\n"); break; case SIGCHLD: + VERB(1) LOG("Got SIGCHLD!\n"); while (waitpid(-1, &state, WNOHANG) > 0); break; } } +static void * +hook_error(void *root, void *arg) +{ +/* sched_root_task_t *r = root; */ + + if (!root) + return (void*) -1; + + if (arg == (void*) EINTR) + return (void*) -1; + + return NULL; +} + int main(int argc, char **argv) { @@ -70,7 +87,7 @@ main(int argc, char **argv) Get1stEth(szDev, STRSIZ); - while ((ch = getopt(argc, argv, "hvulbd:U:C:")) != -1) + while ((ch = getopt(argc, argv, "hvubt:d:i:U:C:")) != -1) switch (ch) { case 'U': pass = getpwnam(optarg); @@ -94,6 +111,9 @@ main(int argc, char **argv) case 'i': id = strtol(optarg, NULL, 0); break; + case 't': + Timeout = abs(strtol(optarg, NULL, 0)); + break; case 'd': strlcpy(szDev, optarg, sizeof szDev); break; @@ -152,9 +172,16 @@ main(int argc, char **argv) } } + if (ioCreatePIDFile(PIDFILE_ANSHD, 42)) { + ERR("Error:: already started anshd service ...\n"); + closelog(); + return 1; + } + h = PrepareL2(szDev, &bpfLEN); if (h == -1) { ERR("Error:: Descriptor not opened ... abort!\n"); + unlink(PIDFILE_ANSHD); closelog(); return 2; } @@ -163,6 +190,7 @@ main(int argc, char **argv) if (!(proc = InitProc(h, NULL, id, bpfLEN))) { ERR("Error:: Not enough memory ...\n"); close(h); + unlink(PIDFILE_ANSHD); closelog(); return 3; } @@ -172,9 +200,11 @@ main(int argc, char **argv) ERR("Scheduler not init #%d - %s\n", sched_GetErrno(), sched_GetError()); DestroyProc(id); close(h); + unlink(PIDFILE_ANSHD); closelog(); return 4; - } + } else + root->root_hooks.hook_root.error = hook_error; chdir("/"); chroot(szChroot); @@ -191,6 +221,7 @@ main(int argc, char **argv) schedEnd(&root); DestroyProc(id); close(h); + unlink(PIDFILE_ANSHD); closelog(); return 0; }