2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 fb1a36c0 2022-01-09 op #include <sys/types.h>
18 fb1a36c0 2022-01-09 op #include <sys/socket.h>
19 fb1a36c0 2022-01-09 op #include <sys/uio.h>
20 fb1a36c0 2022-01-09 op #include <sys/un.h>
22 fb1a36c0 2022-01-09 op #include <err.h>
23 fb1a36c0 2022-01-09 op #include <event.h>
24 fb1a36c0 2022-01-09 op #include <stdint.h>
25 fb1a36c0 2022-01-09 op #include <stdio.h>
26 fb1a36c0 2022-01-09 op #include <stdlib.h>
27 fb1a36c0 2022-01-09 op #include <string.h>
28 fb1a36c0 2022-01-09 op #include <syslog.h>
29 fb1a36c0 2022-01-09 op #include <unistd.h>
30 fb1a36c0 2022-01-09 op #include <imsg.h>
32 fb1a36c0 2022-01-09 op #include "ctl_parser.h"
33 fb1a36c0 2022-01-09 op #include "kamid.h"
34 fb1a36c0 2022-01-09 op #include "log.h"
36 fb1a36c0 2022-01-09 op __dead void usage(void);
38 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
44 fb1a36c0 2022-01-09 op * XXX: this will print `kamid' if compat/getprogname.c is
47 fb1a36c0 2022-01-09 op fprintf(stderr, "usage: %s [-s socket] command [argument ...]\n",
48 fb1a36c0 2022-01-09 op getprogname());
53 fb1a36c0 2022-01-09 op main(int argc, char **argv)
55 fb1a36c0 2022-01-09 op struct sockaddr_un sun;
56 fb1a36c0 2022-01-09 op struct parse_result *res;
57 fb1a36c0 2022-01-09 op struct imsg imsg;
60 fb1a36c0 2022-01-09 op int n, verbose = 0;
62 fb1a36c0 2022-01-09 op const char *sockname;
64 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON); /* Log to stderr. */
66 fb1a36c0 2022-01-09 op sockname = KD_SOCKET;
67 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "s:")) != -1) {
70 fb1a36c0 2022-01-09 op sockname = optarg;
76 fb1a36c0 2022-01-09 op argc -= optind;
77 fb1a36c0 2022-01-09 op argv += optind;
79 fb1a36c0 2022-01-09 op /* parse command line */
80 fb1a36c0 2022-01-09 op if ((res = parse(argc, argv)) == NULL)
83 fb1a36c0 2022-01-09 op /* connect to control socket */
84 fb1a36c0 2022-01-09 op if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
85 fb1a36c0 2022-01-09 op err(1, "socket");
87 fb1a36c0 2022-01-09 op memset(&sun, 0, sizeof(sun));
88 fb1a36c0 2022-01-09 op sun.sun_family = AF_UNIX;
89 fb1a36c0 2022-01-09 op strlcpy(sun.sun_path, sockname, sizeof(sun.sun_path));
91 fb1a36c0 2022-01-09 op if (connect(ctl_sock, (struct sockaddr*)&sun, sizeof(sun)) == -1)
92 fb1a36c0 2022-01-09 op err(1, "connect: %s", sockname);
94 fb1a36c0 2022-01-09 op #ifdef __OpenBSD__
95 fb1a36c0 2022-01-09 op if (pledge("stdio", NULL) == -1)
96 fb1a36c0 2022-01-09 op err(1, "pledge");
99 fb1a36c0 2022-01-09 op if ((ibuf = calloc(1, sizeof(*ibuf))) == NULL)
100 fb1a36c0 2022-01-09 op err(1, NULL);
101 fb1a36c0 2022-01-09 op imsg_init(ibuf, ctl_sock);
104 fb1a36c0 2022-01-09 op /* process user request */
105 fb1a36c0 2022-01-09 op switch (res->action) {
106 fb1a36c0 2022-01-09 op case LOG_VERBOSE:
108 fb1a36c0 2022-01-09 op /* fallthrough */
109 fb1a36c0 2022-01-09 op case LOG_BRIEF:
110 fb1a36c0 2022-01-09 op imsg_compose(ibuf, IMSG_CTL_LOG_VERBOSE, 0, 0, -1,
111 fb1a36c0 2022-01-09 op &verbose, sizeof(verbose));
112 fb1a36c0 2022-01-09 op puts("logging request sent.");
116 fb1a36c0 2022-01-09 op imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, -1, NULL, 0);
117 fb1a36c0 2022-01-09 op puts("reload request sent.");
124 fb1a36c0 2022-01-09 op imsg_flush(ibuf);
127 fb1a36c0 2022-01-09 op * Later we may add commands which requires a response.
129 fb1a36c0 2022-01-09 op while (!done) {
130 fb1a36c0 2022-01-09 op if ((n = imsg_get(ibuf, &imsg)) == -1)
131 fb1a36c0 2022-01-09 op errx(1, "imsg_get error");
135 fb1a36c0 2022-01-09 op switch (res->action) {
139 fb1a36c0 2022-01-09 op imsg_free(&imsg);
141 fb1a36c0 2022-01-09 op close(ctl_sock);