2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
13 static const char *config_file = CONFIG;
17 signal_handler(int sig_num)
22 while (waitpid(-1, &sig_num, WNOHANG) > 0) ;
32 main(int argc, char *argv[])
34 struct shttpd_ctx *ctx;
36 current_time = time(NULL);
37 if (argc > 1 && argv[argc - 2][0] != '-' && argv[argc - 1][0] != '-')
38 config_file = argv[argc - 1];
41 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') {
44 exit(edit_passwords(argv[2],argv[3],argv[4],argv[5]));
48 ctx = init_from_argc_argv(config_file, argc, argv);
51 /* Switch to alternate UID, it is safe now, after shttpd_listen() */
52 if (ctx->uid != NULL) {
55 if ((pw = getpwnam(ctx->uid)) == NULL)
56 elog(E_FATAL, 0, "main: unknown user [%s]", ctx->uid);
57 else if (setgid(pw->pw_gid) == -1)
58 elog(E_FATAL, NULL, "main: setgid(%s): %s",
59 ctx->uid, strerror(errno));
60 else if (setuid(pw->pw_uid) == -1)
61 elog(E_FATAL, NULL, "main: setuid(%s): %s",
62 ctx->uid, strerror(errno));
64 (void) signal(SIGCHLD, signal_handler);
65 (void) signal(SIGPIPE, SIG_IGN);
68 (void) signal(SIGTERM, signal_handler);
69 (void) signal(SIGINT, signal_handler);
71 if (ctx->inetd_mode) {
72 (void) freopen("/dev/null", "a", stderr);
73 shttpd_add_socket(ctx, fileno(stdin));
75 open_listening_ports(ctx);
78 elog(E_LOG, NULL, "shttpd %s started on port(s) %s, serving %s",
79 VERSION, ctx->ports, ctx->document_root);
81 while (exit_flag == 0)
82 shttpd_poll(ctx, 5000);
84 elog(E_LOG, NULL, "%d requests %.2lf Mb in %.2lf Mb out. "
85 "Exit on signal %d", ctx->nrequests, (double) (ctx->in / 1048576),
86 (double) ctx->out / 1048576, exit_flag);
90 return (EXIT_SUCCESS);