Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "compat.h"
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/uio.h>
22 #include <sys/un.h>
24 #include <errno.h>
25 #include <inttypes.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <syslog.h>
31 #include <unistd.h>
33 #include "ctl_parser.h"
34 #include "kamid.h"
35 #include "log.h"
37 __dead void usage(void);
39 struct imsgbuf *ibuf;
41 __dead void
42 usage(void)
43 {
44 /*
45 * XXX: this will print `kamid' if compat/getprogname.c is
46 * used.
47 */
48 fprintf(stderr, "usage: %s [-s socket] command [argument ...]\n",
49 getprogname());
50 exit(1);
51 }
53 int
54 main(int argc, char **argv)
55 {
56 struct sockaddr_un sun;
57 struct parse_result *res;
58 struct imsg imsg;
59 int ctl_sock;
60 int done = 0;
61 int n, verbose = 0;
62 int ch;
63 const char *sockname;
65 log_init(1, LOG_DAEMON); /* Log to stderr. */
67 sockname = KD_SOCKET;
68 while ((ch = getopt(argc, argv, "s:")) != -1) {
69 switch (ch) {
70 case 's':
71 sockname = optarg;
72 break;
73 default:
74 usage();
75 }
76 }
77 argc -= optind;
78 argv += optind;
80 /* parse command line */
81 if ((res = parse(argc, argv)) == NULL)
82 exit(1);
84 /* connect to control socket */
85 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
86 err(1, "socket");
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);
95 #ifdef __OpenBSD__
96 if (pledge("stdio", NULL) == -1)
97 err(1, "pledge");
98 #endif
100 if ((ibuf = calloc(1, sizeof(*ibuf))) == NULL)
101 err(1, NULL);
102 imsg_init(ibuf, ctl_sock);
103 done = 0;
105 /* process user request */
106 switch (res->action) {
107 case LOG_VERBOSE:
108 verbose = 1;
109 /* fallthrough */
110 case LOG_BRIEF:
111 imsg_compose(ibuf, IMSG_CTL_LOG_VERBOSE, 0, 0, -1,
112 &verbose, sizeof(verbose));
113 puts("logging request sent.");
114 done = 1;
115 break;
116 case RELOAD:
117 imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, -1, NULL, 0);
118 puts("reload request sent.");
119 done = 1;
120 break;
121 case DEBUG:
122 imsg_compose(ibuf, IMSG_CTL_DEBUG, 0, getpid(), -1, NULL, 0);
123 break;
124 default:
125 usage();
128 if (imsg_flush(ibuf) == -1)
129 err(1, "imsg_flush");
131 while (!done) {
132 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
133 errx(1, "write error");
134 if (n == 0)
135 errx(0, "pipe closed");
137 while (!done) {
138 if ((n = imsg_get(ibuf, &imsg)) == -1)
139 errx(1, "imsg_get error");
140 if (n == 0)
141 break;
143 switch (res->action) {
144 case DEBUG: {
145 struct kd_debug_info d;
147 if (imsg.hdr.type == IMSG_CTL_DEBUG_END) {
148 done = 1;
149 break;
152 if (imsg.hdr.type != IMSG_CTL_DEBUG_BACK ||
153 IMSG_DATA_SIZE(imsg) != sizeof(d))
154 errx(1, "got invalid reply (%d)",
155 imsg.hdr.type);
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);
163 break;
165 default:
166 break;
168 imsg_free(&imsg);
171 close(ctl_sock);
172 free(ibuf);
174 return 0;