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