2 * keydctl.c - A simple program to control a running keyd instance
4 * Copyright 2011 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <sys/types.h>
27 #include <sys/socket.h>
31 #include "onak-conf.h"
34 /* HACK: We need to stop onak-conf.o requiring this. */
37 static int keyd_fd = -1;
38 static int verbose = 0;
40 static int keyd_do_command(enum keyd_ops cmd, void *buf, size_t len)
49 if (write(keyd_fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
52 "Couldn't write keyd command %d: %s (%d)\n",
53 cmd, strerror(errno), errno);
56 } else if (read(keyd_fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
59 "Couldn't read keyd command %d reply: "
61 cmd, strerror(errno), errno);
64 } else if (tmp != KEYD_REPLY_OK) {
66 } else if (buf == NULL) {
68 } else if (read(keyd_fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
71 "Couldn't read keyd command %d reply length: "
73 cmd, strerror(errno), errno);
76 } else if (tmp > len) {
77 /* TODO: Read what we can into buf and skip the rest */
80 return read(keyd_fd, buf, tmp);
84 static void keyd_connect(void)
86 struct sockaddr_un sock;
87 uint32_t reply = KEYD_REPLY_UNKNOWN_CMD;
89 keyd_fd = socket(PF_UNIX, SOCK_STREAM, 0);
93 "Couldn't open socket: %s (%d)\n",
100 sock.sun_family = AF_UNIX;
101 snprintf(sock.sun_path, sizeof(sock.sun_path) - 1, "%s/%s",
104 if (connect(keyd_fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
107 "Couldn't connect to socket %s: %s (%d)\n",
115 keyd_do_command(KEYD_CMD_VERSION, &reply, sizeof(reply));
116 if (reply != keyd_version) {
118 fprintf(stderr, "Error! keyd protocol version "
119 "mismatch. (us = %d, it = %d)\n",
120 keyd_version, reply);
128 static void keyd_close(void)
130 uint32_t cmd = KEYD_CMD_CLOSE;
132 if (write(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd) && verbose >= 0) {
133 fprintf(stderr, "Couldn't send close cmd: %s (%d)\n",
138 if (shutdown(keyd_fd, SHUT_RDWR) < 0 && verbose >= 0) {
139 fprintf(stderr, "Error shutting down socket: %d\n",
142 if (close(keyd_fd) < 0 && verbose >= 0) {
143 fprintf(stderr, "Error closing down socket: %d\n",
152 static void keyd_status(void)
155 struct keyd_stats stats;
157 keyd_do_command(KEYD_CMD_VERSION, &reply, sizeof(reply));
158 printf("Using keyd protocol version %d.\n", reply);
160 keyd_do_command(KEYD_CMD_STATS, &stats, sizeof(stats));
161 printf("keyd running since %s", ctime(&stats.started));
162 printf("%d client connections received\n", stats.connects);
164 printf("Command statistics:\n");
165 printf(" Version: %d\n",
166 stats.command_stats[KEYD_CMD_VERSION]);
167 printf(" Get key: %d\n", stats.command_stats[KEYD_CMD_GET]);
168 printf(" Store key: %d\n",
169 stats.command_stats[KEYD_CMD_STORE]);
170 printf(" Delete key: %d\n",
171 stats.command_stats[KEYD_CMD_DELETE]);
172 printf(" Search key: %d\n",
173 stats.command_stats[KEYD_CMD_GETTEXT]);
174 printf(" Get full keyid: %d\n",
175 stats.command_stats[KEYD_CMD_GETFULLKEYID]);
176 printf(" Iterate all keys: %d\n",
177 stats.command_stats[KEYD_CMD_KEYITER]);
178 printf(" Close: %d\n",
179 stats.command_stats[KEYD_CMD_CLOSE]);
180 printf(" Quit: %d\n", stats.command_stats[KEYD_CMD_QUIT]);
181 printf(" Get statistics: %d\n",
182 stats.command_stats[KEYD_CMD_STATS]);
183 printf(" Unknown: %d\n",
184 stats.command_stats[KEYD_CMD_UNKNOWN]);
189 static void usage(void)
191 puts("keydctl " ONAK_VERSION " - control an onak keyd instance.\n");
193 puts("\tkeydctl [options] <command> <parameters>\n");
194 puts("\tCommands:\n");
195 puts("\tcheck - check if keyd is running");
196 puts("\tquit - request that keyd cleanly shuts down");
197 puts("\tstatus - display running keyd status");
201 int main(int argc, char *argv[])
204 char *configfile = NULL;
206 while ((optchar = getopt(argc, argv, "c:h")) != -1 ) {
209 configfile = strdup(optarg);
218 readconfig(configfile);
222 if ((argc - optind) < 1) {
224 } else if (!strcmp("check", argv[optind])) {
225 /* Just do the connect and close quietly */
229 } else if (!strcmp("status", argv[optind])) {
233 } else if (!strcmp("quit", argv[optind])) {
235 keyd_do_command(KEYD_CMD_QUIT, NULL, 0);