2 * uuidd.c --- UUID-generation daemon
4 * Copyright (C) 2007 Theodore Ts'o
7 * This file may be redistributed under the terms of the GNU Public
12 #define _GNU_SOURCE /* for setres[ug]id() */
21 #include <sys/types.h>
23 #include <sys/socket.h>
31 extern int getopt(int argc, char * const argv[], const char *optstring);
35 #include "uuid/uuid.h"
36 #include "uuid/uuidd.h"
37 #include "nls-enable.h"
40 #define CODE_ATTR(x) __attribute__(x)
45 static void usage(const char *progname)
47 fprintf(stderr, _("Usage: %s [-d] [-p pidfile] [-s socketpath] "
48 "[-T timeout]\n"), progname);
49 fprintf(stderr, _(" %s [-r|t] [-n num] [-s socketpath]\n"),
51 fprintf(stderr, _(" %s -k\n"), progname);
55 static void die(const char *msg)
61 static void create_daemon(void)
70 } else if (pid != 0) {
77 open("/dev/null", O_RDWR);
78 open("/dev/null", O_RDWR);
79 open("/dev/null", O_RDWR);
81 if (chdir("/")) {} /* Silence warn_unused_result warning */
84 if (setreuid(euid, euid) < 0)
88 static ssize_t read_all(int fd, char *buf, size_t count)
94 memset(buf, 0, count);
96 ret = read(fd, buf, count);
98 if ((errno == EAGAIN || errno == EINTR || ret == 0) &&
112 static int write_all(int fd, char *buf, size_t count)
118 ret = write(fd, buf, count);
120 if ((errno == EAGAIN) || (errno == EINTR))
131 static const char *cleanup_pidfile, *cleanup_socket;
133 static void terminate_intr(int signo CODE_ATTR((unused)))
135 (void) unlink(cleanup_pidfile);
137 (void) unlink(cleanup_socket);
141 static int call_daemon(const char *socket_path, int op, char *buf,
142 int buflen, int *num, const char **err_context)
148 int32_t reply_len = 0;
149 struct sockaddr_un srv_addr;
151 if (((op == 4) || (op == 5)) && !num) {
153 *err_context = _("bad arguments");
158 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
160 *err_context = _("socket");
164 srv_addr.sun_family = AF_UNIX;
165 strncpy(srv_addr.sun_path, socket_path, sizeof(srv_addr.sun_path));
166 srv_addr.sun_path[sizeof(srv_addr.sun_path)-1] = '\0';
168 if (connect(s, (const struct sockaddr *) &srv_addr,
169 sizeof(struct sockaddr_un)) < 0) {
171 *err_context = _("connect");
177 if ((*num)*16 > buflen-4)
178 *num = (buflen-4) / 16;
182 if ((op == 4) || (op == 5)) {
183 memcpy(op_buf+1, num, sizeof(int));
184 op_len += sizeof(int);
187 ret = write_all(s, op_buf, op_len);
190 *err_context = _("write");
195 ret = read_all(s, (char *) &reply_len, sizeof(reply_len));
198 *err_context = _("read count");
202 if (reply_len < 0 || reply_len > buflen) {
204 *err_context = _("bad response length");
208 ret = read_all(s, (char *) buf, reply_len);
210 if ((ret > 0) && (op == 4)) {
211 if (reply_len >= (int) (16+sizeof(int)))
212 memcpy(buf+16, num, sizeof(int));
216 if ((ret > 0) && (op == 5)) {
217 if (*num >= (int) sizeof(int))
218 memcpy(buf, num, sizeof(int));
228 static void server_loop(const char *socket_path, const char *pidfile_path,
229 int debug, int timeout, int quiet)
231 struct sockaddr_un my_addr, from_addr;
234 int32_t reply_len = 0;
237 char reply_buf[1024], *cp;
239 int i, s, ns, len, num;
242 fd_pidfile = open(pidfile_path, O_CREAT | O_RDWR, 0664);
243 if (fd_pidfile < 0) {
245 fprintf(stderr, "Failed to open/create %s: %s\n",
246 pidfile_path, strerror(errno));
249 cleanup_pidfile = pidfile_path;
251 signal(SIGALRM, terminate_intr);
254 fl.l_whence = SEEK_SET;
258 while (fcntl(fd_pidfile, F_SETLKW, &fl) < 0) {
259 if ((errno == EAGAIN) || (errno == EINTR))
262 fprintf(stderr, "Failed to lock %s: %s\n",
263 pidfile_path, strerror(errno));
266 ret = call_daemon(socket_path, 0, reply_buf, sizeof(reply_buf), 0, 0);
269 printf(_("uuidd daemon already running at pid %s\n"),
275 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
277 fprintf(stderr, _("Couldn't create unix stream "
278 "socket: %s"), strerror(errno));
283 * Make sure the socket isn't using fd numbers 0-2 to avoid it
284 * getting closed by create_daemon()
286 while (!debug && s <= 2) {
295 * Create the address we will be binding to.
297 my_addr.sun_family = AF_UNIX;
298 strncpy(my_addr.sun_path, socket_path, sizeof(my_addr.sun_path));
299 my_addr.sun_path[sizeof(my_addr.sun_path)-1] = '\0';
300 (void) unlink(socket_path);
301 save_umask = umask(0);
302 if (bind(s, (const struct sockaddr *) &my_addr,
303 sizeof(struct sockaddr_un)) < 0) {
306 _("Couldn't bind unix socket %s: %s\n"),
307 socket_path, strerror(errno));
310 (void) umask(save_umask);
312 if (listen(s, 5) < 0) {
314 fprintf(stderr, _("Couldn't listen on unix "
315 "socket %s: %s\n"), socket_path,
320 cleanup_socket = socket_path;
323 signal(SIGHUP, terminate_intr);
324 signal(SIGINT, terminate_intr);
325 signal(SIGTERM, terminate_intr);
326 signal(SIGALRM, terminate_intr);
327 signal(SIGPIPE, SIG_IGN);
329 sprintf(reply_buf, "%8d\n", getpid());
330 if (ftruncate(fd_pidfile, 0)) {} /* Silence warn_unused_result */
331 write_all(fd_pidfile, reply_buf, strlen(reply_buf));
333 close(fd_pidfile); /* Unlock the pid file */
336 fromlen = sizeof(from_addr);
339 ns = accept(s, (struct sockaddr *) &from_addr, &fromlen);
342 if ((errno == EAGAIN) || (errno == EINTR))
347 len = read(ns, &op, 1);
352 printf(_("Error reading from client, "
354 goto shutdown_socket;
356 if ((op == 4) || (op == 5)) {
357 if (read_all(ns, (char *) &num, sizeof(num)) != 4)
358 goto shutdown_socket;
360 printf(_("operation %d, incoming num = %d\n"),
363 printf("operation %d\n", op);
366 case UUIDD_OP_GETPID:
367 sprintf(reply_buf, "%d", getpid());
368 reply_len = strlen(reply_buf)+1;
370 case UUIDD_OP_GET_MAXOP:
371 sprintf(reply_buf, "%d", UUIDD_MAX_OP);
372 reply_len = strlen(reply_buf)+1;
374 case UUIDD_OP_TIME_UUID:
376 uuid__generate_time(uu, &num);
378 uuid_unparse(uu, str);
379 printf(_("Generated time UUID: %s\n"), str);
381 memcpy(reply_buf, uu, sizeof(uu));
382 reply_len = sizeof(uu);
384 case UUIDD_OP_RANDOM_UUID:
386 uuid__generate_random(uu, &num);
388 uuid_unparse(uu, str);
389 printf(_("Generated random UUID: %s\n"), str);
391 memcpy(reply_buf, uu, sizeof(uu));
392 reply_len = sizeof(uu);
394 case UUIDD_OP_BULK_TIME_UUID:
395 uuid__generate_time(uu, &num);
397 uuid_unparse(uu, str);
398 printf(_("Generated time UUID %s and %d "
399 "following\n"), str, num);
401 memcpy(reply_buf, uu, sizeof(uu));
402 reply_len = sizeof(uu);
403 memcpy(reply_buf+reply_len, &num, sizeof(num));
404 reply_len += sizeof(num);
406 case UUIDD_OP_BULK_RANDOM_UUID:
411 if (num*16 > (int) (sizeof(reply_buf)-sizeof(num)))
412 num = (sizeof(reply_buf)-sizeof(num)) / 16;
413 uuid__generate_random((unsigned char *) reply_buf +
416 printf(_("Generated %d UUID's:\n"), num);
417 for (i=0, cp=reply_buf+sizeof(num);
418 i < num; i++, cp+=16) {
419 uuid_unparse((unsigned char *)cp, str);
420 printf("\t%s\n", str);
423 reply_len = (num*16) + sizeof(num);
424 memcpy(reply_buf, &num, sizeof(num));
428 printf(_("Invalid operation %d\n"), op);
429 goto shutdown_socket;
431 write_all(ns, (char *) &reply_len, sizeof(reply_len));
432 write_all(ns, reply_buf, reply_len);
438 int main(int argc, char **argv)
440 const char *socket_path = UUIDD_SOCKET_PATH;
441 const char *pidfile_path = UUIDD_PIDFILE_PATH;
442 const char *err_context;
449 int debug = 0, do_type = 0, do_kill = 0, num = 0;
450 int timeout = 0, quiet = 0, drop_privs = 0;
453 setlocale(LC_MESSAGES, "");
454 setlocale(LC_CTYPE, "");
455 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
456 textdomain(NLS_CAT_NAME);
459 while ((c = getopt (argc, argv, "dkn:qp:s:tT:r")) != EOF) {
470 num = strtol(optarg, &tmp, 0);
471 if ((num < 0) || *tmp) {
472 fprintf(stderr, _("Bad number: %s\n"), optarg);
476 pidfile_path = optarg;
483 socket_path = optarg;
487 do_type = UUIDD_OP_TIME_UUID;
491 timeout = strtol(optarg, &tmp, 0);
492 if ((timeout < 0) || *tmp) {
493 fprintf(stderr, _("Bad number: %s\n"), optarg);
498 do_type = UUIDD_OP_RANDOM_UUID;
506 if (uid && drop_privs) {
508 #ifdef HAVE_SETRESGID
509 if (setresgid(gid, gid, gid) < 0)
512 if (setregid(gid, gid) < 0)
516 #ifdef HAVE_SETRESUID
517 if (setresuid(uid, uid, uid) < 0)
520 if (setreuid(uid, uid) < 0)
524 if (num && do_type) {
525 ret = call_daemon(socket_path, do_type+2, buf,
526 sizeof(buf), &num, &err_context);
528 printf(_("Error calling uuidd daemon (%s): %s\n"),
529 err_context, strerror(errno));
532 if (do_type == UUIDD_OP_TIME_UUID) {
533 if (ret != sizeof(uu) + sizeof(num))
534 goto unexpected_size;
536 uuid_unparse((unsigned char *) buf, str);
538 printf(_("%s and subsequent %d UUID's\n"), str, num);
540 printf(_("List of UUID's:\n"));
542 if (ret != (int) (sizeof(num) + num*sizeof(uu)))
543 goto unexpected_size;
544 for (i=0; i < num; i++, cp+=16) {
545 uuid_unparse((unsigned char *) cp, str);
546 printf("\t%s\n", str);
552 ret = call_daemon(socket_path, do_type, (char *) &uu,
553 sizeof(uu), 0, &err_context);
555 printf(_("Error calling uuidd daemon (%s): %s\n"),
556 err_context, strerror(errno));
559 if (ret != sizeof(uu)) {
561 printf(_("Unexpected reply length from server %d\n"),
565 uuid_unparse(uu, str);
572 ret = call_daemon(socket_path, 0, buf, sizeof(buf), 0, 0);
573 if ((ret > 0) && ((do_kill = atoi((char *) buf)) > 0)) {
574 ret = kill(do_kill, SIGTERM);
578 _("Couldn't kill uuidd running "
579 "at pid %d: %s\n"), do_kill,
584 printf(_("Killed uuidd running at pid %d\n"),
590 server_loop(socket_path, pidfile_path, debug, timeout, quiet);