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 bbcba3ed 2022-01-10 op #include "compat.h"
18 bbcba3ed 2022-01-10 op
19 fb1a36c0 2022-01-09 op #include <sys/types.h>
20 fb1a36c0 2022-01-09 op #include <sys/socket.h>
21 fb1a36c0 2022-01-09 op #include <sys/uio.h>
22 fb1a36c0 2022-01-09 op #include <sys/un.h>
23 fb1a36c0 2022-01-09 op
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
31 fb1a36c0 2022-01-09 op #include "ctl_parser.h"
32 fb1a36c0 2022-01-09 op #include "kamid.h"
33 fb1a36c0 2022-01-09 op #include "log.h"
34 fb1a36c0 2022-01-09 op
35 fb1a36c0 2022-01-09 op __dead void usage(void);
36 fb1a36c0 2022-01-09 op
37 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
38 fb1a36c0 2022-01-09 op
39 fb1a36c0 2022-01-09 op __dead void
40 fb1a36c0 2022-01-09 op usage(void)
41 fb1a36c0 2022-01-09 op {
42 fb1a36c0 2022-01-09 op /*
43 fb1a36c0 2022-01-09 op * XXX: this will print `kamid' if compat/getprogname.c is
44 fb1a36c0 2022-01-09 op * used.
45 fb1a36c0 2022-01-09 op */
46 fb1a36c0 2022-01-09 op fprintf(stderr, "usage: %s [-s socket] command [argument ...]\n",
47 fb1a36c0 2022-01-09 op getprogname());
48 fb1a36c0 2022-01-09 op exit(1);
49 fb1a36c0 2022-01-09 op }
50 fb1a36c0 2022-01-09 op
51 fb1a36c0 2022-01-09 op int
52 fb1a36c0 2022-01-09 op main(int argc, char **argv)
53 fb1a36c0 2022-01-09 op {
54 fb1a36c0 2022-01-09 op struct sockaddr_un sun;
55 fb1a36c0 2022-01-09 op struct parse_result *res;
56 fb1a36c0 2022-01-09 op struct imsg imsg;
57 fb1a36c0 2022-01-09 op int ctl_sock;
58 fb1a36c0 2022-01-09 op int done = 0;
59 fb1a36c0 2022-01-09 op int n, verbose = 0;
60 fb1a36c0 2022-01-09 op int ch;
61 fb1a36c0 2022-01-09 op const char *sockname;
62 fb1a36c0 2022-01-09 op
63 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON); /* Log to stderr. */
64 fb1a36c0 2022-01-09 op
65 fb1a36c0 2022-01-09 op sockname = KD_SOCKET;
66 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "s:")) != -1) {
67 fb1a36c0 2022-01-09 op switch (ch) {
68 fb1a36c0 2022-01-09 op case 's':
69 fb1a36c0 2022-01-09 op sockname = optarg;
70 fb1a36c0 2022-01-09 op break;
71 fb1a36c0 2022-01-09 op default:
72 fb1a36c0 2022-01-09 op usage();
73 fb1a36c0 2022-01-09 op }
74 fb1a36c0 2022-01-09 op }
75 fb1a36c0 2022-01-09 op argc -= optind;
76 fb1a36c0 2022-01-09 op argv += optind;
77 fb1a36c0 2022-01-09 op
78 fb1a36c0 2022-01-09 op /* parse command line */
79 fb1a36c0 2022-01-09 op if ((res = parse(argc, argv)) == NULL)
80 fb1a36c0 2022-01-09 op exit(1);
81 fb1a36c0 2022-01-09 op
82 fb1a36c0 2022-01-09 op /* connect to control socket */
83 fb1a36c0 2022-01-09 op if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
84 fb1a36c0 2022-01-09 op err(1, "socket");
85 fb1a36c0 2022-01-09 op
86 fb1a36c0 2022-01-09 op memset(&sun, 0, sizeof(sun));
87 fb1a36c0 2022-01-09 op sun.sun_family = AF_UNIX;
88 fb1a36c0 2022-01-09 op strlcpy(sun.sun_path, sockname, sizeof(sun.sun_path));
89 fb1a36c0 2022-01-09 op
90 fb1a36c0 2022-01-09 op if (connect(ctl_sock, (struct sockaddr*)&sun, sizeof(sun)) == -1)
91 fb1a36c0 2022-01-09 op err(1, "connect: %s", sockname);
92 fb1a36c0 2022-01-09 op
93 fb1a36c0 2022-01-09 op #ifdef __OpenBSD__
94 fb1a36c0 2022-01-09 op if (pledge("stdio", NULL) == -1)
95 fb1a36c0 2022-01-09 op err(1, "pledge");
96 fb1a36c0 2022-01-09 op #endif
97 fb1a36c0 2022-01-09 op
98 fb1a36c0 2022-01-09 op if ((ibuf = calloc(1, sizeof(*ibuf))) == NULL)
99 fb1a36c0 2022-01-09 op err(1, NULL);
100 fb1a36c0 2022-01-09 op imsg_init(ibuf, ctl_sock);
101 fb1a36c0 2022-01-09 op done = 0;
102 fb1a36c0 2022-01-09 op
103 fb1a36c0 2022-01-09 op /* process user request */
104 fb1a36c0 2022-01-09 op switch (res->action) {
105 fb1a36c0 2022-01-09 op case LOG_VERBOSE:
106 fb1a36c0 2022-01-09 op verbose = 1;
107 fb1a36c0 2022-01-09 op /* fallthrough */
108 fb1a36c0 2022-01-09 op case LOG_BRIEF:
109 fb1a36c0 2022-01-09 op imsg_compose(ibuf, IMSG_CTL_LOG_VERBOSE, 0, 0, -1,
110 fb1a36c0 2022-01-09 op &verbose, sizeof(verbose));
111 fb1a36c0 2022-01-09 op puts("logging request sent.");
112 fb1a36c0 2022-01-09 op done = 1;
113 fb1a36c0 2022-01-09 op break;
114 fb1a36c0 2022-01-09 op case RELOAD:
115 fb1a36c0 2022-01-09 op imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, -1, NULL, 0);
116 fb1a36c0 2022-01-09 op puts("reload request sent.");
117 fb1a36c0 2022-01-09 op done = 1;
118 fb1a36c0 2022-01-09 op break;
119 fb1a36c0 2022-01-09 op default:
120 fb1a36c0 2022-01-09 op usage();
121 fb1a36c0 2022-01-09 op }
122 fb1a36c0 2022-01-09 op
123 fb1a36c0 2022-01-09 op imsg_flush(ibuf);
124 fb1a36c0 2022-01-09 op
125 fb1a36c0 2022-01-09 op /*
126 fb1a36c0 2022-01-09 op * Later we may add commands which requires a response.
127 fb1a36c0 2022-01-09 op */
128 fb1a36c0 2022-01-09 op while (!done) {
129 fb1a36c0 2022-01-09 op if ((n = imsg_get(ibuf, &imsg)) == -1)
130 fb1a36c0 2022-01-09 op errx(1, "imsg_get error");
131 fb1a36c0 2022-01-09 op if (n == 0)
132 fb1a36c0 2022-01-09 op break;
133 fb1a36c0 2022-01-09 op
134 fb1a36c0 2022-01-09 op switch (res->action) {
135 fb1a36c0 2022-01-09 op default:
136 fb1a36c0 2022-01-09 op break;
137 fb1a36c0 2022-01-09 op }
138 fb1a36c0 2022-01-09 op imsg_free(&imsg);
139 fb1a36c0 2022-01-09 op }
140 fb1a36c0 2022-01-09 op close(ctl_sock);
141 fb1a36c0 2022-01-09 op free(ibuf);
142 fb1a36c0 2022-01-09 op
143 fb1a36c0 2022-01-09 op return 0;
144 fb1a36c0 2022-01-09 op }