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 bbcba3ed 2022-01-10 op #include "compat.h"
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>
24 4fa9a307 2022-01-29 op #include <errno.h>
25 4fa9a307 2022-01-29 op #include <inttypes.h>
26 fb1a36c0 2022-01-09 op #include <stdint.h>
27 fb1a36c0 2022-01-09 op #include <stdio.h>
28 fb1a36c0 2022-01-09 op #include <stdlib.h>
29 fb1a36c0 2022-01-09 op #include <string.h>
30 fb1a36c0 2022-01-09 op #include <syslog.h>
31 fb1a36c0 2022-01-09 op #include <unistd.h>
33 fb1a36c0 2022-01-09 op #include "ctl_parser.h"
34 fb1a36c0 2022-01-09 op #include "kamid.h"
35 fb1a36c0 2022-01-09 op #include "log.h"
37 fb1a36c0 2022-01-09 op __dead void usage(void);
39 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
45 fb1a36c0 2022-01-09 op * XXX: this will print `kamid' if compat/getprogname.c is
48 fb1a36c0 2022-01-09 op fprintf(stderr, "usage: %s [-s socket] command [argument ...]\n",
49 fb1a36c0 2022-01-09 op getprogname());
54 fb1a36c0 2022-01-09 op main(int argc, char **argv)
56 fb1a36c0 2022-01-09 op struct sockaddr_un sun;
57 fb1a36c0 2022-01-09 op struct parse_result *res;
58 fb1a36c0 2022-01-09 op struct imsg imsg;
61 fb1a36c0 2022-01-09 op int n, verbose = 0;
63 fb1a36c0 2022-01-09 op const char *sockname;
65 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON); /* Log to stderr. */
67 fb1a36c0 2022-01-09 op sockname = KD_SOCKET;
68 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "s:")) != -1) {
71 fb1a36c0 2022-01-09 op sockname = optarg;
77 fb1a36c0 2022-01-09 op argc -= optind;
78 fb1a36c0 2022-01-09 op argv += optind;
80 fb1a36c0 2022-01-09 op /* parse command line */
81 fb1a36c0 2022-01-09 op if ((res = parse(argc, argv)) == NULL)
84 fb1a36c0 2022-01-09 op /* connect to control socket */
85 fb1a36c0 2022-01-09 op if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
86 fb1a36c0 2022-01-09 op err(1, "socket");
88 fb1a36c0 2022-01-09 op memset(&sun, 0, sizeof(sun));
89 fb1a36c0 2022-01-09 op sun.sun_family = AF_UNIX;
90 fb1a36c0 2022-01-09 op strlcpy(sun.sun_path, sockname, sizeof(sun.sun_path));
92 fb1a36c0 2022-01-09 op if (connect(ctl_sock, (struct sockaddr*)&sun, sizeof(sun)) == -1)
93 fb1a36c0 2022-01-09 op err(1, "connect: %s", sockname);
95 fb1a36c0 2022-01-09 op #ifdef __OpenBSD__
96 fb1a36c0 2022-01-09 op if (pledge("stdio", NULL) == -1)
97 fb1a36c0 2022-01-09 op err(1, "pledge");
100 fb1a36c0 2022-01-09 op if ((ibuf = calloc(1, sizeof(*ibuf))) == NULL)
101 fb1a36c0 2022-01-09 op err(1, NULL);
102 fb1a36c0 2022-01-09 op imsg_init(ibuf, ctl_sock);
105 fb1a36c0 2022-01-09 op /* process user request */
106 fb1a36c0 2022-01-09 op switch (res->action) {
107 fb1a36c0 2022-01-09 op case LOG_VERBOSE:
109 fb1a36c0 2022-01-09 op /* fallthrough */
110 fb1a36c0 2022-01-09 op case LOG_BRIEF:
111 fb1a36c0 2022-01-09 op imsg_compose(ibuf, IMSG_CTL_LOG_VERBOSE, 0, 0, -1,
112 fb1a36c0 2022-01-09 op &verbose, sizeof(verbose));
113 fb1a36c0 2022-01-09 op puts("logging request sent.");
117 fb1a36c0 2022-01-09 op imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, -1, NULL, 0);
118 fb1a36c0 2022-01-09 op puts("reload request sent.");
122 4fa9a307 2022-01-29 op imsg_compose(ibuf, IMSG_CTL_DEBUG, 0, getpid(), -1, NULL, 0);
128 4fa9a307 2022-01-29 op if (imsg_flush(ibuf) == -1)
129 4fa9a307 2022-01-29 op err(1, "imsg_flush");
131 fb1a36c0 2022-01-09 op while (!done) {
132 4fa9a307 2022-01-29 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
133 4fa9a307 2022-01-29 op errx(1, "write error");
135 4fa9a307 2022-01-29 op errx(0, "pipe closed");
137 4fa9a307 2022-01-29 op while (!done) {
138 4fa9a307 2022-01-29 op if ((n = imsg_get(ibuf, &imsg)) == -1)
139 4fa9a307 2022-01-29 op errx(1, "imsg_get error");
143 4fa9a307 2022-01-29 op switch (res->action) {
144 4fa9a307 2022-01-29 op case DEBUG: {
145 4fa9a307 2022-01-29 op struct kd_debug_info d;
147 4fa9a307 2022-01-29 op if (imsg.hdr.type == IMSG_CTL_DEBUG_END) {
152 4fa9a307 2022-01-29 op if (imsg.hdr.type != IMSG_CTL_DEBUG_BACK ||
153 4fa9a307 2022-01-29 op IMSG_DATA_SIZE(imsg) != sizeof(d))
154 4fa9a307 2022-01-29 op errx(1, "got invalid reply (%d)",
155 4fa9a307 2022-01-29 op imsg.hdr.type);
157 4fa9a307 2022-01-29 op memcpy(&d, imsg.data, sizeof(d));
158 4fa9a307 2022-01-29 op if (d.path[sizeof(d.path)-1] != '\0')
159 4fa9a307 2022-01-29 op errx(1, "got invalid reply");
161 4fa9a307 2022-01-29 op printf("%"PRIu32"\t%"PRIu32"\t%s\n",
162 4fa9a307 2022-01-29 op d.client_id, d.fid, d.path);
168 4fa9a307 2022-01-29 op imsg_free(&imsg);
171 fb1a36c0 2022-01-09 op close(ctl_sock);