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