Blame


1 fb1a36c0 2022-01-09 op /*
2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 fb1a36c0 2022-01-09 op *
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.
7 fb1a36c0 2022-01-09 op *
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.
15 fb1a36c0 2022-01-09 op */
16 fb1a36c0 2022-01-09 op
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>
21 fb1a36c0 2022-01-09 op
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>
31 fb1a36c0 2022-01-09 op
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"
35 fb1a36c0 2022-01-09 op
36 fb1a36c0 2022-01-09 op __dead void usage(void);
37 fb1a36c0 2022-01-09 op
38 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
39 fb1a36c0 2022-01-09 op
40 fb1a36c0 2022-01-09 op __dead void
41 fb1a36c0 2022-01-09 op usage(void)
42 fb1a36c0 2022-01-09 op {
43 fb1a36c0 2022-01-09 op /*
44 fb1a36c0 2022-01-09 op * XXX: this will print `kamid' if compat/getprogname.c is
45 fb1a36c0 2022-01-09 op * used.
46 fb1a36c0 2022-01-09 op */
47 fb1a36c0 2022-01-09 op fprintf(stderr, "usage: %s [-s socket] command [argument ...]\n",
48 fb1a36c0 2022-01-09 op getprogname());
49 fb1a36c0 2022-01-09 op exit(1);
50 fb1a36c0 2022-01-09 op }
51 fb1a36c0 2022-01-09 op
52 fb1a36c0 2022-01-09 op int
53 fb1a36c0 2022-01-09 op main(int argc, char **argv)
54 fb1a36c0 2022-01-09 op {
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;
58 fb1a36c0 2022-01-09 op int ctl_sock;
59 fb1a36c0 2022-01-09 op int done = 0;
60 fb1a36c0 2022-01-09 op int n, verbose = 0;
61 fb1a36c0 2022-01-09 op int ch;
62 fb1a36c0 2022-01-09 op const char *sockname;
63 fb1a36c0 2022-01-09 op
64 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON); /* Log to stderr. */
65 fb1a36c0 2022-01-09 op
66 fb1a36c0 2022-01-09 op sockname = KD_SOCKET;
67 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "s:")) != -1) {
68 fb1a36c0 2022-01-09 op switch (ch) {
69 fb1a36c0 2022-01-09 op case 's':
70 fb1a36c0 2022-01-09 op sockname = optarg;
71 fb1a36c0 2022-01-09 op break;
72 fb1a36c0 2022-01-09 op default:
73 fb1a36c0 2022-01-09 op usage();
74 fb1a36c0 2022-01-09 op }
75 fb1a36c0 2022-01-09 op }
76 fb1a36c0 2022-01-09 op argc -= optind;
77 fb1a36c0 2022-01-09 op argv += optind;
78 fb1a36c0 2022-01-09 op
79 fb1a36c0 2022-01-09 op /* parse command line */
80 fb1a36c0 2022-01-09 op if ((res = parse(argc, argv)) == NULL)
81 fb1a36c0 2022-01-09 op exit(1);
82 fb1a36c0 2022-01-09 op
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");
86 fb1a36c0 2022-01-09 op
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));
90 fb1a36c0 2022-01-09 op
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);
93 fb1a36c0 2022-01-09 op
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");
97 fb1a36c0 2022-01-09 op #endif
98 fb1a36c0 2022-01-09 op
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);
102 fb1a36c0 2022-01-09 op done = 0;
103 fb1a36c0 2022-01-09 op
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:
107 fb1a36c0 2022-01-09 op verbose = 1;
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.");
113 fb1a36c0 2022-01-09 op done = 1;
114 fb1a36c0 2022-01-09 op break;
115 fb1a36c0 2022-01-09 op case RELOAD:
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.");
118 fb1a36c0 2022-01-09 op done = 1;
119 fb1a36c0 2022-01-09 op break;
120 fb1a36c0 2022-01-09 op default:
121 fb1a36c0 2022-01-09 op usage();
122 fb1a36c0 2022-01-09 op }
123 fb1a36c0 2022-01-09 op
124 fb1a36c0 2022-01-09 op imsg_flush(ibuf);
125 fb1a36c0 2022-01-09 op
126 fb1a36c0 2022-01-09 op /*
127 fb1a36c0 2022-01-09 op * Later we may add commands which requires a response.
128 fb1a36c0 2022-01-09 op */
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");
132 fb1a36c0 2022-01-09 op if (n == 0)
133 fb1a36c0 2022-01-09 op break;
134 fb1a36c0 2022-01-09 op
135 fb1a36c0 2022-01-09 op switch (res->action) {
136 fb1a36c0 2022-01-09 op default:
137 fb1a36c0 2022-01-09 op break;
138 fb1a36c0 2022-01-09 op }
139 fb1a36c0 2022-01-09 op imsg_free(&imsg);
140 fb1a36c0 2022-01-09 op }
141 fb1a36c0 2022-01-09 op close(ctl_sock);
142 fb1a36c0 2022-01-09 op free(ibuf);
143 fb1a36c0 2022-01-09 op
144 fb1a36c0 2022-01-09 op return 0;
145 fb1a36c0 2022-01-09 op }