diff options
Diffstat (limited to 'utils/down.c')
-rw-r--r-- | utils/down.c | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/utils/down.c b/utils/down.c new file mode 100644 index 0000000..66d9d19 --- /dev/null +++ b/utils/down.c @@ -0,0 +1,129 @@ +/* + * A verbal system shutdown utility. + * $Id: down.c 1.2 Mon, 24 Mar 1997 12:25:37 -0500 dyfet $ + * Copyright (c) 1997 by Tycho Softworks. + * For conditions of distribution and reuse see product license. + * + * Abstract: + * A verbal system shutdown utility. You may need to adjust the name + * of the shutdown program used. This gives some warning and then + * initiates the shutdown processing, unless cancelled in time. + */ + +#include <std/signal.h> +#include <proc/process.h> +#include <other/string.h> +#include <std/files.h> +#include <other/config.h> +#include <net/stream.h> + +static char host[128] = "localhost"; +static char halt[60] = "/sbin/halt"; +static char abort_msg[] = "System shutdown overriden. Resuming normal operation.\n"; +static char down_msg[] = "%s system shutdown sequence initiated. You have %d seconds to override.\n"; +static int timer = 10; +static STREAM fp = NULL; + +/* + * If the user aborts the shutdown, then we send the cancellation message + * and gracefully exit the scene. + */ + +static void abort_sig() +{ + if(fp) + { + puttcp(abort_msg, fp); + closetcp(fp); + } + fputs("\n", stdout); + exit(-1); +} + +void main(int argc, char **argv) +{ + char *p; + char *reboot = halt; + int port = getservice("speak"); + CONFIG *cfg = sys_config("speak"); + char hostname[128]; + + if(argc < 1 || argc > 2) + fatal(EX_USAGE, "use: down [timer]\n"); + + if(!port) + fatal(EX_UNAVAILABLE, "down: speak: service unlisted in /etc/services\n"); + + if(!cfg) + fatal(EX_CONFIG, "down: speak.conf: config missing\n"); + + seek_config(cfg, "down"); + while(read_config(cfg)) + { + if(NULL != (p = get_config(cfg, "timer"))) + { + timer = atoi(p); + continue; + } + + if(NULL != (p = get_config(cfg, "halt"))) + { + strcpy(halt, p); + continue; + } + + if(NULL != (p = get_config(cfg, "host"))) + { + strcpy(host, p); + continue; + } + + if(NULL != (p = get_config(cfg, "server"))) + { + strcpy(host, p); + continue; + } + } + close_config(cfg); + if(argc > 1) + timer = atoi(argv[1]); + + fp = opentcp(host, port); + if(!fp) + fatal(EX_UNAVAILABLE, "down: %s: speak service unreachable\n", host); + + printf("System shutdown initiated...use <break> to override; "); + fflush(stdout); + signal(SIGINT, abort_sig); + gettcp(hostname, 127, fp); + gethostname(hostname, 80); + fprintf(fp, down_msg, hostname, timer); + fflush(fp); + sleep(strlen(down_msg) / 9 + 1); + + /* count down timer until 0 */ + + while(timer) + { + printf("%d ", timer); + fflush(stdout); + fprintf(fp, "%d\n", timer); + fflush(fp); + sleep(1); + --timer; + } + + /* too late now!! */ + + signal(SIGINT, SIG_IGN); + printf("\n"); + closetcp(fp); + + /* detach return to user and execute reboot program */ + + pdetach(D_KEEPALL); + argv[0] = reboot; + argv[1] = NULL; + execvp(reboot, argv); +} + |