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() */
22 #include <sys/types.h>
24 #include <sys/socket.h>
32 extern int getopt(int argc, char * const argv[], const char *optstring);
36 #include "uuid/uuid.h"
37 #include "uuid/uuidd.h"
38 #include "support/nls-enable.h"
39 #include "ext2fs/ext2fs.h"
42 #define CODE_ATTR(x) __attribute__(x)
47 static void usage(const char *progname)
49 fprintf(stderr, _("Usage: %s [-d] [-p pidfile] [-s socketpath] "
50 "[-T timeout]\n"), progname);
51 fprintf(stderr, _(" %s [-r|t] [-n num] [-s socketpath]\n"),
53 fprintf(stderr, _(" %s -k\n"), progname);
57 static void die(const char *msg)
63 static void create_daemon(void)
72 } else if (pid != 0) {
79 open("/dev/null", O_RDWR);
80 open("/dev/null", O_RDWR);
81 open("/dev/null", O_RDWR);
83 if (chdir("/")) {} /* Silence warn_unused_result warning */
86 if (setreuid(euid, euid) < 0)
90 static ssize_t read_all(int fd, char *buf, size_t count)
96 memset(buf, 0, count);
98 ret = read(fd, buf, count);
100 if ((errno == EAGAIN || errno == EINTR || ret == 0) &&
114 static int write_all(int fd, char *buf, size_t count)
120 ret = write(fd, buf, count);
122 if ((errno == EAGAIN) || (errno == EINTR))
133 static const char *cleanup_pidfile, *cleanup_socket;
135 static void terminate_intr(int signo CODE_ATTR((unused)))
137 (void) unlink(cleanup_pidfile);
139 (void) unlink(cleanup_socket);
143 static int call_daemon(const char *socket_path, int op, char *buf,
144 int buflen, int *num, const char **err_context)
150 int32_t reply_len = 0;
151 struct sockaddr_un srv_addr;
153 if (((op == 4) || (op == 5)) && !num) {
155 *err_context = _("bad arguments");
160 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
162 *err_context = _("socket");
166 srv_addr.sun_family = AF_UNIX;
167 strncpy(srv_addr.sun_path, socket_path, sizeof(srv_addr.sun_path));
168 srv_addr.sun_path[sizeof(srv_addr.sun_path)-1] = '\0';
170 if (connect(s, (const struct sockaddr *) &srv_addr,
171 sizeof(struct sockaddr_un)) < 0) {
173 *err_context = _("connect");
179 if ((*num)*16 > buflen-4)
180 *num = (buflen-4) / 16;
184 if ((op == 4) || (op == 5)) {
185 memcpy(op_buf+1, num, sizeof(int));
186 op_len += sizeof(int);
189 ret = write_all(s, op_buf, op_len);
192 *err_context = _("write");
197 ret = read_all(s, (char *) &reply_len, sizeof(reply_len));
200 *err_context = _("read count");
204 if (reply_len < 0 || reply_len > buflen) {
206 *err_context = _("bad response length");
210 ret = read_all(s, (char *) buf, reply_len);
212 if ((ret > 0) && (op == 4)) {
213 if (reply_len >= (int) (16+sizeof(int)))
214 memcpy(buf+16, num, sizeof(int));
218 if ((ret > 0) && (op == 5)) {
219 if (*num >= (int) sizeof(int))
220 memcpy(buf, num, sizeof(int));
230 static void server_loop(const char *socket_path, const char *pidfile_path,
231 int debug, int timeout, int quiet)
233 struct sockaddr_un my_addr, from_addr;
236 int32_t reply_len = 0;
239 char reply_buf[1024], *cp;
240 char op, str[UUID_STR_SIZE];
241 int i, s, ns, len, num;
244 fd_pidfile = open(pidfile_path, O_CREAT | O_RDWR, 0664);
245 if (fd_pidfile < 0) {
247 fprintf(stderr, "Failed to open/create %s: %s\n",
248 pidfile_path, strerror(errno));
251 cleanup_pidfile = pidfile_path;
253 signal(SIGALRM, terminate_intr);
256 fl.l_whence = SEEK_SET;
260 while (fcntl(fd_pidfile, F_SETLKW, &fl) < 0) {
261 if ((errno == EAGAIN) || (errno == EINTR))
264 fprintf(stderr, "Failed to lock %s: %s\n",
265 pidfile_path, strerror(errno));
268 ret = call_daemon(socket_path, 0, reply_buf, sizeof(reply_buf), 0, 0);
271 printf(_("uuidd daemon already running at pid %s\n"),
277 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
279 fprintf(stderr, _("Couldn't create unix stream "
280 "socket: %s"), strerror(errno));
285 * Make sure the socket isn't using fd numbers 0-2 to avoid it
286 * getting closed by create_daemon()
288 while (!debug && s <= 2) {
297 * Create the address we will be binding to.
299 my_addr.sun_family = AF_UNIX;
300 strncpy(my_addr.sun_path, socket_path, sizeof(my_addr.sun_path));
301 my_addr.sun_path[sizeof(my_addr.sun_path)-1] = '\0';
302 (void) unlink(socket_path);
303 save_umask = umask(0);
304 if (bind(s, (const struct sockaddr *) &my_addr,
305 sizeof(struct sockaddr_un)) < 0) {
308 _("Couldn't bind unix socket %s: %s\n"),
309 socket_path, strerror(errno));
312 (void) umask(save_umask);
314 if (listen(s, 5) < 0) {
316 fprintf(stderr, _("Couldn't listen on unix "
317 "socket %s: %s\n"), socket_path,
322 cleanup_socket = socket_path;
325 signal(SIGHUP, terminate_intr);
326 signal(SIGINT, terminate_intr);
327 signal(SIGTERM, terminate_intr);
328 signal(SIGALRM, terminate_intr);
329 signal(SIGPIPE, SIG_IGN);
331 sprintf(reply_buf, "%8d\n", getpid());
332 if (ftruncate(fd_pidfile, 0)) {} /* Silence warn_unused_result */
333 write_all(fd_pidfile, reply_buf, strlen(reply_buf));
335 close(fd_pidfile); /* Unlock the pid file */
338 fromlen = sizeof(from_addr);
341 ns = accept(s, (struct sockaddr *) &from_addr, &fromlen);
344 if ((errno == EAGAIN) || (errno == EINTR))
349 len = read(ns, &op, 1);
354 printf(_("Error reading from client, "
356 goto shutdown_socket;
358 if ((op == 4) || (op == 5)) {
359 if (read_all(ns, (char *) &num, sizeof(num)) != 4)
360 goto shutdown_socket;
362 printf(_("operation %d, incoming num = %d\n"),
365 printf("operation %d\n", op);
368 case UUIDD_OP_GETPID:
369 sprintf(reply_buf, "%d", getpid());
370 reply_len = strlen(reply_buf)+1;
372 case UUIDD_OP_GET_MAXOP:
373 sprintf(reply_buf, "%d", UUIDD_MAX_OP);
374 reply_len = strlen(reply_buf)+1;
376 case UUIDD_OP_TIME_UUID:
378 uuid__generate_time(uu, &num);
380 uuid_unparse(uu, str);
381 printf(_("Generated time UUID: %s\n"), str);
383 memcpy(reply_buf, uu, sizeof(uu));
384 reply_len = sizeof(uu);
386 case UUIDD_OP_RANDOM_UUID:
388 uuid__generate_random(uu, &num);
390 uuid_unparse(uu, str);
391 printf(_("Generated random UUID: %s\n"), str);
393 memcpy(reply_buf, uu, sizeof(uu));
394 reply_len = sizeof(uu);
396 case UUIDD_OP_BULK_TIME_UUID:
397 uuid__generate_time(uu, &num);
399 uuid_unparse(uu, str);
400 printf(P_("Generated time UUID %s and "
402 "Generated time UUID %s and %d "
403 "subsequent UUIDs\n", num),
406 memcpy(reply_buf, uu, sizeof(uu));
407 reply_len = sizeof(uu);
408 memcpy(reply_buf+reply_len, &num, sizeof(num));
409 reply_len += sizeof(num);
411 case UUIDD_OP_BULK_RANDOM_UUID:
416 if (num*16 > (int) (sizeof(reply_buf)-sizeof(num)))
417 num = (sizeof(reply_buf)-sizeof(num)) / 16;
418 uuid__generate_random((unsigned char *) reply_buf +
421 printf(_("Generated %d UUID's:\n"), num);
422 for (i=0, cp=reply_buf+sizeof(num);
423 i < num; i++, cp+=16) {
424 uuid_unparse((unsigned char *)cp, str);
425 printf("\t%s\n", str);
428 reply_len = (num*16) + sizeof(num);
429 memcpy(reply_buf, &num, sizeof(num));
433 printf(_("Invalid operation %d\n"), op);
434 goto shutdown_socket;
436 write_all(ns, (char *) &reply_len, sizeof(reply_len));
437 write_all(ns, reply_buf, reply_len);
443 int main(int argc, char **argv)
445 const char *socket_path = UUIDD_SOCKET_PATH;
446 const char *pidfile_path = UUIDD_PIDFILE_PATH;
447 const char *err_context;
454 int debug = 0, do_type = 0, do_kill = 0, num = 0;
455 int timeout = 0, quiet = 0, drop_privs = 0;
458 setlocale(LC_MESSAGES, "");
459 setlocale(LC_CTYPE, "");
460 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
461 textdomain(NLS_CAT_NAME);
464 while ((c = getopt (argc, argv, "dkn:qp:s:tT:r")) != EOF) {
475 num = strtol(optarg, &tmp, 0);
476 if ((num < 0) || *tmp) {
477 fprintf(stderr, _("Bad number: %s\n"), optarg);
482 pidfile_path = optarg;
489 socket_path = optarg;
493 do_type = UUIDD_OP_TIME_UUID;
497 timeout = strtol(optarg, &tmp, 0);
498 if ((timeout < 0) || *tmp) {
499 fprintf(stderr, _("Bad number: %s\n"), optarg);
504 do_type = UUIDD_OP_RANDOM_UUID;
512 if (uid && drop_privs) {
514 #ifdef HAVE_SETRESGID
515 if (setresgid(gid, gid, gid) < 0)
518 if (setregid(gid, gid) < 0)
522 #ifdef HAVE_SETRESUID
523 if (setresuid(uid, uid, uid) < 0)
526 if (setreuid(uid, uid) < 0)
530 if (num && do_type) {
531 ret = call_daemon(socket_path, do_type+2, buf,
532 sizeof(buf), &num, &err_context);
534 printf(_("Error calling uuidd daemon (%s): %s\n"),
535 err_context, strerror(errno));
538 if (do_type == UUIDD_OP_TIME_UUID) {
539 if (ret != sizeof(uu) + sizeof(num))
540 goto unexpected_size;
542 uuid_unparse((unsigned char *) buf, str);
544 printf(P_("%s and subsequent UUID\n",
545 "%s and subsequent %d UUIDs\n", num),
548 printf("%s", _("List of UUID's:\n"));
550 if (ret != (int) (sizeof(num) + num*sizeof(uu)))
551 goto unexpected_size;
552 for (i=0; i < num; i++, cp+=16) {
553 uuid_unparse((unsigned char *) cp, str);
554 printf("\t%s\n", str);
560 ret = call_daemon(socket_path, do_type, (char *) &uu,
561 sizeof(uu), 0, &err_context);
563 printf(_("Error calling uuidd daemon (%s): %s\n"),
564 err_context, strerror(errno));
567 if (ret != sizeof(uu)) {
569 printf(_("Unexpected reply length from server %d\n"),
573 uuid_unparse(uu, str);
580 ret = call_daemon(socket_path, 0, buf, sizeof(buf), 0, 0);
581 if ((ret > 0) && ((do_kill = atoi((char *) buf)) > 0)) {
582 ret = kill(do_kill, SIGTERM);
586 _("Couldn't kill uuidd running "
587 "at pid %d: %s\n"), do_kill,
592 printf(_("Killed uuidd running at pid %d\n"),
598 server_loop(socket_path, pidfile_path, debug, timeout, quiet);