2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/socket.h>
33 #include "ctl_parser.h"
37 __dead void usage(void);
45 * XXX: this will print `kamid' if compat/getprogname.c is
48 fprintf(stderr, "usage: %s [-s socket] command [argument ...]\n",
54 main(int argc, char **argv)
56 struct sockaddr_un sun;
57 struct parse_result *res;
65 log_init(1, LOG_DAEMON); /* Log to stderr. */
68 while ((ch = getopt(argc, argv, "s:")) != -1) {
80 /* parse command line */
81 if ((res = parse(argc, argv)) == NULL)
84 /* connect to control socket */
85 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
88 memset(&sun, 0, sizeof(sun));
89 sun.sun_family = AF_UNIX;
90 strlcpy(sun.sun_path, sockname, sizeof(sun.sun_path));
92 if (connect(ctl_sock, (struct sockaddr*)&sun, sizeof(sun)) == -1)
93 err(1, "connect: %s", sockname);
96 if (pledge("stdio", NULL) == -1)
100 if ((ibuf = calloc(1, sizeof(*ibuf))) == NULL)
102 imsg_init(ibuf, ctl_sock);
105 /* process user request */
106 switch (res->action) {
111 imsg_compose(ibuf, IMSG_CTL_LOG_VERBOSE, 0, 0, -1,
112 &verbose, sizeof(verbose));
113 puts("logging request sent.");
117 imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, -1, NULL, 0);
118 puts("reload request sent.");
122 imsg_compose(ibuf, IMSG_CTL_DEBUG, 0, getpid(), -1, NULL, 0);
128 if (imsg_flush(ibuf) == -1)
129 err(1, "imsg_flush");
132 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
133 errx(1, "write error");
135 errx(0, "pipe closed");
138 if ((n = imsg_get(ibuf, &imsg)) == -1)
139 errx(1, "imsg_get error");
143 switch (res->action) {
145 struct kd_debug_info d;
147 if (imsg.hdr.type == IMSG_CTL_DEBUG_END) {
152 if (imsg.hdr.type != IMSG_CTL_DEBUG_BACK ||
153 IMSG_DATA_SIZE(imsg) != sizeof(d))
154 errx(1, "got invalid reply (%d)",
157 memcpy(&d, imsg.data, sizeof(d));
158 if (d.path[sizeof(d.path)-1] != '\0')
159 errx(1, "got invalid reply");
161 printf("%"PRIu32"\t%"PRIu32"\t%s\n",
162 d.client_id, d.fid, d.path);