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