2 * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/param.h>
19 #include <sys/queue.h>
20 #include <sys/socket.h>
24 #include <netinet/in.h>
40 #include "got_compat.h"
41 #include "got_opentemp.h"
42 #include "got_reference.h"
47 __dead
void usage(void);
49 int main(int, char **);
50 int gotwebd_configure(struct gotwebd
*);
51 void gotwebd_configure_done(struct gotwebd
*);
52 void gotwebd_sighdlr(int sig
, short event
, void *arg
);
53 void gotwebd_shutdown(void);
54 int gotwebd_dispatch_sockets(int, struct privsep_proc
*, struct imsg
*);
56 struct gotwebd
*gotwebd_env
;
58 static struct privsep_proc procs
[] = {
59 { "sockets", PROC_SOCKS
, gotwebd_dispatch_sockets
, sockets
,
64 gotwebd_dispatch_sockets(int fd
, struct privsep_proc
*p
, struct imsg
*imsg
)
66 struct privsep
*ps
= p
->p_ps
;
67 struct gotwebd
*env
= ps
->ps_env
;
69 switch (imsg
->hdr
.type
) {
71 gotwebd_configure_done(env
);
81 gotwebd_sighdlr(int sig
, short event
, void *arg
)
83 /* struct privsep *ps = arg; */
85 if (privsep_process
!= PROC_GOTWEBD
)
90 log_info("%s: ignoring SIGHUP", __func__
);
93 log_info("%s: ignoring SIGPIPE", __func__
);
96 log_info("%s: ignoring SIGUSR1", __func__
);
103 fatalx("unexpected signal");
110 fprintf(stderr
, "usage: %s [-dnv] [-D macro=value] [-f file]\n",
116 main(int argc
, char **argv
)
122 const char *conffile
= GOTWEBD_CONF
;
123 enum privsep_procid proc_id
= PROC_GOTWEBD
;
124 int proc_instance
= 0;
125 const char *errp
, *title
= NULL
;
128 env
= calloc(1, sizeof(*env
));
130 fatal("%s: calloc", __func__
);
132 /* XXX: add s and S for both sockets */
133 while ((ch
= getopt(argc
, argv
, "D:df:I:nP:v")) != -1) {
136 if (cmdline_symset(optarg
) < 0)
137 log_warnx("could not parse macro definition %s",
141 env
->gotwebd_debug
= 2;
147 proc_instance
= strtonum(optarg
, 0,
148 PROC_MAX_INSTANCES
, &errp
);
150 fatalx("invalid process instance");
153 env
->gotwebd_debug
= 2;
154 env
->gotwebd_noaction
= 1;
158 proc_id
= proc_getid(procs
, nitems(procs
), title
);
159 if (proc_id
== PROC_MAX
)
160 fatalx("invalid process name");
163 env
->gotwebd_verbose
++;
170 /* log to stderr until daemonized */
171 log_init(env
->gotwebd_debug
? env
->gotwebd_debug
: 1, LOG_DAEMON
);
177 ps
= calloc(1, sizeof(*ps
));
179 fatal("%s: calloc:", __func__
);
182 env
->gotwebd_ps
= ps
;
184 env
->gotwebd_conffile
= conffile
;
186 if (parse_config(env
->gotwebd_conffile
, env
) == -1)
189 if (env
->gotwebd_noaction
&& !env
->gotwebd_debug
)
190 env
->gotwebd_debug
= 1;
192 /* check for root privileges */
193 if (env
->gotwebd_noaction
== 0) {
195 fatalx("need root privileges");
198 ps
->ps_pw
= getpwnam(GOTWEBD_USER
);
199 if (ps
->ps_pw
== NULL
)
200 fatalx("unknown user %s", GOTWEBD_USER
);
202 log_init(env
->gotwebd_debug
, LOG_DAEMON
);
203 log_setverbose(env
->gotwebd_verbose
);
205 if (env
->gotwebd_noaction
)
208 ps
->ps_instances
[PROC_SOCKS
] = env
->prefork_gotwebd
;
209 ps
->ps_instance
= proc_instance
;
211 ps
->ps_title
[proc_id
] = title
;
213 for (proc
= 0; proc
< nitems(procs
); proc
++)
214 procs
[proc
].p_chroot
= env
->httpd_chroot
;
216 /* only the gotwebd returns */
217 proc_init(ps
, procs
, nitems(procs
), argc0
, argv
, proc_id
);
219 log_procinit("gotwebd");
220 if (!env
->gotwebd_debug
&& daemon(0, 0) == -1)
221 fatal("can't daemonize");
223 if (ps
->ps_noaction
== 0)
224 log_info("%s startup", getprogname());
228 signal_set(&ps
->ps_evsigint
, SIGINT
, gotwebd_sighdlr
, ps
);
229 signal_set(&ps
->ps_evsigterm
, SIGTERM
, gotwebd_sighdlr
, ps
);
230 signal_set(&ps
->ps_evsighup
, SIGHUP
, gotwebd_sighdlr
, ps
);
231 signal_set(&ps
->ps_evsigpipe
, SIGPIPE
, gotwebd_sighdlr
, ps
);
232 signal_set(&ps
->ps_evsigusr1
, SIGUSR1
, gotwebd_sighdlr
, ps
);
234 signal_add(&ps
->ps_evsigint
, NULL
);
235 signal_add(&ps
->ps_evsigterm
, NULL
);
236 signal_add(&ps
->ps_evsighup
, NULL
);
237 signal_add(&ps
->ps_evsigpipe
, NULL
);
238 signal_add(&ps
->ps_evsigusr1
, NULL
);
240 if (!env
->gotwebd_noaction
)
243 if (gotwebd_configure(env
) == -1)
244 fatalx("configuration failed");
247 if (unveil("gmon.out", "rwc") != 0)
251 if (unveil(env
->httpd_chroot
, "rwc") == -1)
254 if (unveil(GOT_TMPDIR_STR
, "rw") == -1)
257 if (unveil(GOTWEBD_CONF
, "r") == -1)
260 if (unveil(NULL
, NULL
) != 0)
264 if (pledge("stdio rpath wpath cpath inet unix", NULL
) == -1)
270 log_debug("%s gotwebd exiting", getprogname());
276 gotwebd_configure(struct gotwebd
*env
)
282 if (env
->gotwebd_noaction
) {
283 fprintf(stderr
, "configuration OK\n");
284 proc_kill(env
->gotwebd_ps
);
288 /* gotweb need to reload its config. */
289 env
->gotwebd_reload
= env
->prefork_gotwebd
;
291 /* send our gotweb servers */
292 TAILQ_FOREACH(srv
, &env
->servers
, entry
) {
293 if (config_setserver(env
, srv
) == -1)
294 fatalx("%s: send server error", __func__
);
297 /* send our sockets */
298 TAILQ_FOREACH(sock
, &env
->sockets
, entry
) {
299 if (config_setsock(env
, sock
) == -1)
300 fatalx("%s: send socket error", __func__
);
301 if (config_setfd(env
, sock
) == -1)
302 fatalx("%s: send priv_fd error", __func__
);
305 for (id
= 0; id
< PROC_MAX
; id
++) {
306 if (id
== privsep_process
)
308 proc_compose(env
->gotwebd_ps
, id
, IMSG_CFG_DONE
, NULL
, 0);
315 gotwebd_configure_done(struct gotwebd
*env
)
319 if (env
->gotwebd_reload
== 0) {
320 log_warnx("%s: configuration already finished", __func__
);
324 env
->gotwebd_reload
--;
325 if (env
->gotwebd_reload
== 0) {
326 for (id
= 0; id
< PROC_MAX
; id
++) {
327 if (id
== privsep_process
)
329 proc_compose(env
->gotwebd_ps
, id
, IMSG_CTL_START
,
336 gotwebd_shutdown(void)
338 proc_kill(gotwebd_env
->gotwebd_ps
);
340 /* unlink(gotwebd_env->gotweb->gotweb_conf.gotweb_unix_socket_name); */
341 /* free(gotwebd_env->gotweb); */
344 log_warnx("gotwebd terminating");