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 "nls-enable.h"
41 #define CODE_ATTR(x) __attribute__(x)
46 static void usage(const char *progname
)
48 fprintf(stderr
, _("Usage: %s [-d] [-p pidfile] [-s socketpath] "
49 "[-T timeout]\n"), progname
);
50 fprintf(stderr
, _(" %s [-r|t] [-n num] [-s socketpath]\n"),
52 fprintf(stderr
, _(" %s -k\n"), progname
);
56 static void die(const char *msg
)
62 static void create_daemon(void)
71 } else if (pid
!= 0) {
78 open("/dev/null", O_RDWR
);
79 open("/dev/null", O_RDWR
);
80 open("/dev/null", O_RDWR
);
82 if (chdir("/")) {} /* Silence warn_unused_result warning */
85 if (setreuid(euid
, euid
) < 0)
89 static ssize_t
read_all(int fd
, char *buf
, size_t count
)
95 memset(buf
, 0, count
);
97 ret
= read(fd
, buf
, count
);
99 if ((errno
== EAGAIN
|| errno
== EINTR
|| ret
== 0) &&
113 static int write_all(int fd
, char *buf
, size_t count
)
119 ret
= write(fd
, buf
, count
);
121 if ((errno
== EAGAIN
) || (errno
== EINTR
))
132 static const char *cleanup_pidfile
, *cleanup_socket
;
134 static void terminate_intr(int signo
CODE_ATTR((unused
)))
136 (void) unlink(cleanup_pidfile
);
138 (void) unlink(cleanup_socket
);
142 static int call_daemon(const char *socket_path
, int op
, char *buf
,
143 int buflen
, int *num
, const char **err_context
)
149 int32_t reply_len
= 0;
150 struct sockaddr_un srv_addr
;
152 if (((op
== 4) || (op
== 5)) && !num
) {
154 *err_context
= _("bad arguments");
159 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
161 *err_context
= _("socket");
165 srv_addr
.sun_family
= AF_UNIX
;
166 strncpy(srv_addr
.sun_path
, socket_path
, sizeof(srv_addr
.sun_path
));
167 srv_addr
.sun_path
[sizeof(srv_addr
.sun_path
)-1] = '\0';
169 if (connect(s
, (const struct sockaddr
*) &srv_addr
,
170 sizeof(struct sockaddr_un
)) < 0) {
172 *err_context
= _("connect");
178 if ((*num
)*16 > buflen
-4)
179 *num
= (buflen
-4) / 16;
183 if ((op
== 4) || (op
== 5)) {
184 memcpy(op_buf
+1, num
, sizeof(int));
185 op_len
+= sizeof(int);
188 ret
= write_all(s
, op_buf
, op_len
);
191 *err_context
= _("write");
196 ret
= read_all(s
, (char *) &reply_len
, sizeof(reply_len
));
199 *err_context
= _("read count");
203 if (reply_len
< 0 || reply_len
> buflen
) {
205 *err_context
= _("bad response length");
209 ret
= read_all(s
, (char *) buf
, reply_len
);
211 if ((ret
> 0) && (op
== 4)) {
212 if (reply_len
>= (int) (16+sizeof(int)))
213 memcpy(buf
+16, num
, sizeof(int));
217 if ((ret
> 0) && (op
== 5)) {
218 if (*num
>= (int) sizeof(int))
219 memcpy(buf
, num
, sizeof(int));
229 static void server_loop(const char *socket_path
, const char *pidfile_path
,
230 int debug
, int timeout
, int quiet
)
232 struct sockaddr_un my_addr
, from_addr
;
235 int32_t reply_len
= 0;
238 char reply_buf
[1024], *cp
;
240 int i
, s
, ns
, len
, num
;
243 fd_pidfile
= open(pidfile_path
, O_CREAT
| O_RDWR
, 0664);
244 if (fd_pidfile
< 0) {
246 fprintf(stderr
, "Failed to open/create %s: %s\n",
247 pidfile_path
, strerror(errno
));
250 cleanup_pidfile
= pidfile_path
;
252 signal(SIGALRM
, terminate_intr
);
255 fl
.l_whence
= SEEK_SET
;
259 while (fcntl(fd_pidfile
, F_SETLKW
, &fl
) < 0) {
260 if ((errno
== EAGAIN
) || (errno
== EINTR
))
263 fprintf(stderr
, "Failed to lock %s: %s\n",
264 pidfile_path
, strerror(errno
));
267 ret
= call_daemon(socket_path
, 0, reply_buf
, sizeof(reply_buf
), 0, 0);
270 printf(_("uuidd daemon already running at pid %s\n"),
276 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
278 fprintf(stderr
, _("Couldn't create unix stream "
279 "socket: %s"), strerror(errno
));
284 * Make sure the socket isn't using fd numbers 0-2 to avoid it
285 * getting closed by create_daemon()
287 while (!debug
&& s
<= 2) {
296 * Create the address we will be binding to.
298 my_addr
.sun_family
= AF_UNIX
;
299 strncpy(my_addr
.sun_path
, socket_path
, sizeof(my_addr
.sun_path
));
300 my_addr
.sun_path
[sizeof(my_addr
.sun_path
)-1] = '\0';
301 (void) unlink(socket_path
);
302 save_umask
= umask(0);
303 if (bind(s
, (const struct sockaddr
*) &my_addr
,
304 sizeof(struct sockaddr_un
)) < 0) {
307 _("Couldn't bind unix socket %s: %s\n"),
308 socket_path
, strerror(errno
));
311 (void) umask(save_umask
);
313 if (listen(s
, 5) < 0) {
315 fprintf(stderr
, _("Couldn't listen on unix "
316 "socket %s: %s\n"), socket_path
,
321 cleanup_socket
= socket_path
;
324 signal(SIGHUP
, terminate_intr
);
325 signal(SIGINT
, terminate_intr
);
326 signal(SIGTERM
, terminate_intr
);
327 signal(SIGALRM
, terminate_intr
);
328 signal(SIGPIPE
, SIG_IGN
);
330 sprintf(reply_buf
, "%8d\n", getpid());
331 if (ftruncate(fd_pidfile
, 0)) {} /* Silence warn_unused_result */
332 write_all(fd_pidfile
, reply_buf
, strlen(reply_buf
));
334 close(fd_pidfile
); /* Unlock the pid file */
337 fromlen
= sizeof(from_addr
);
340 ns
= accept(s
, (struct sockaddr
*) &from_addr
, &fromlen
);
343 if ((errno
== EAGAIN
) || (errno
== EINTR
))
348 len
= read(ns
, &op
, 1);
353 printf(_("Error reading from client, "
355 goto shutdown_socket
;
357 if ((op
== 4) || (op
== 5)) {
358 if (read_all(ns
, (char *) &num
, sizeof(num
)) != 4)
359 goto shutdown_socket
;
361 printf(_("operation %d, incoming num = %d\n"),
364 printf("operation %d\n", op
);
367 case UUIDD_OP_GETPID
:
368 sprintf(reply_buf
, "%d", getpid());
369 reply_len
= strlen(reply_buf
)+1;
371 case UUIDD_OP_GET_MAXOP
:
372 sprintf(reply_buf
, "%d", UUIDD_MAX_OP
);
373 reply_len
= strlen(reply_buf
)+1;
375 case UUIDD_OP_TIME_UUID
:
377 uuid__generate_time(uu
, &num
);
379 uuid_unparse(uu
, str
);
380 printf(_("Generated time UUID: %s\n"), str
);
382 memcpy(reply_buf
, uu
, sizeof(uu
));
383 reply_len
= sizeof(uu
);
385 case UUIDD_OP_RANDOM_UUID
:
387 uuid__generate_random(uu
, &num
);
389 uuid_unparse(uu
, str
);
390 printf(_("Generated random UUID: %s\n"), str
);
392 memcpy(reply_buf
, uu
, sizeof(uu
));
393 reply_len
= sizeof(uu
);
395 case UUIDD_OP_BULK_TIME_UUID
:
396 uuid__generate_time(uu
, &num
);
398 uuid_unparse(uu
, str
);
399 printf(P_("Generated time UUID %s and "
401 "Generated time UUID %s and %d "
402 "subsequent UUIDs\n", num
),
405 memcpy(reply_buf
, uu
, sizeof(uu
));
406 reply_len
= sizeof(uu
);
407 memcpy(reply_buf
+reply_len
, &num
, sizeof(num
));
408 reply_len
+= sizeof(num
);
410 case UUIDD_OP_BULK_RANDOM_UUID
:
415 if (num
*16 > (int) (sizeof(reply_buf
)-sizeof(num
)))
416 num
= (sizeof(reply_buf
)-sizeof(num
)) / 16;
417 uuid__generate_random((unsigned char *) reply_buf
+
420 printf(_("Generated %d UUID's:\n"), num
);
421 for (i
=0, cp
=reply_buf
+sizeof(num
);
422 i
< num
; i
++, cp
+=16) {
423 uuid_unparse((unsigned char *)cp
, str
);
424 printf("\t%s\n", str
);
427 reply_len
= (num
*16) + sizeof(num
);
428 memcpy(reply_buf
, &num
, sizeof(num
));
432 printf(_("Invalid operation %d\n"), op
);
433 goto shutdown_socket
;
435 write_all(ns
, (char *) &reply_len
, sizeof(reply_len
));
436 write_all(ns
, reply_buf
, reply_len
);
442 int main(int argc
, char **argv
)
444 const char *socket_path
= UUIDD_SOCKET_PATH
;
445 const char *pidfile_path
= UUIDD_PIDFILE_PATH
;
446 const char *err_context
;
453 int debug
= 0, do_type
= 0, do_kill
= 0, num
= 0;
454 int timeout
= 0, quiet
= 0, drop_privs
= 0;
457 setlocale(LC_MESSAGES
, "");
458 setlocale(LC_CTYPE
, "");
459 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
460 textdomain(NLS_CAT_NAME
);
463 while ((c
= getopt (argc
, argv
, "dkn:qp:s:tT:r")) != EOF
) {
474 num
= strtol(optarg
, &tmp
, 0);
475 if ((num
< 0) || *tmp
) {
476 fprintf(stderr
, _("Bad number: %s\n"), optarg
);
481 pidfile_path
= optarg
;
488 socket_path
= optarg
;
492 do_type
= UUIDD_OP_TIME_UUID
;
496 timeout
= strtol(optarg
, &tmp
, 0);
497 if ((timeout
< 0) || *tmp
) {
498 fprintf(stderr
, _("Bad number: %s\n"), optarg
);
503 do_type
= UUIDD_OP_RANDOM_UUID
;
511 if (uid
&& drop_privs
) {
513 #ifdef HAVE_SETRESGID
514 if (setresgid(gid
, gid
, gid
) < 0)
517 if (setregid(gid
, gid
) < 0)
521 #ifdef HAVE_SETRESUID
522 if (setresuid(uid
, uid
, uid
) < 0)
525 if (setreuid(uid
, uid
) < 0)
529 if (num
&& do_type
) {
530 ret
= call_daemon(socket_path
, do_type
+2, buf
,
531 sizeof(buf
), &num
, &err_context
);
533 printf(_("Error calling uuidd daemon (%s): %s\n"),
534 err_context
, strerror(errno
));
537 if (do_type
== UUIDD_OP_TIME_UUID
) {
538 if (ret
!= sizeof(uu
) + sizeof(num
))
539 goto unexpected_size
;
541 uuid_unparse((unsigned char *) buf
, str
);
543 printf(P_("%s and subsequent UUID\n",
544 "%s and subsequent %d UUIDs\n", num
),
547 printf("%s", _("List of UUID's:\n"));
549 if (ret
!= (int) (sizeof(num
) + num
*sizeof(uu
)))
550 goto unexpected_size
;
551 for (i
=0; i
< num
; i
++, cp
+=16) {
552 uuid_unparse((unsigned char *) cp
, str
);
553 printf("\t%s\n", str
);
559 ret
= call_daemon(socket_path
, do_type
, (char *) &uu
,
560 sizeof(uu
), 0, &err_context
);
562 printf(_("Error calling uuidd daemon (%s): %s\n"),
563 err_context
, strerror(errno
));
566 if (ret
!= sizeof(uu
)) {
568 printf(_("Unexpected reply length from server %d\n"),
572 uuid_unparse(uu
, str
);
579 ret
= call_daemon(socket_path
, 0, buf
, sizeof(buf
), 0, 0);
580 if ((ret
> 0) && ((do_kill
= atoi((char *) buf
)) > 0)) {
581 ret
= kill(do_kill
, SIGTERM
);
585 _("Couldn't kill uuidd running "
586 "at pid %d: %s\n"), do_kill
,
591 printf(_("Killed uuidd running at pid %d\n"),
597 server_loop(socket_path
, pidfile_path
, debug
, timeout
, quiet
);