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