-static int call_daemon(const char *socket_path, int op, char *buf,
- int buflen, int *num, const char **err_context)
-{
- char op_buf[8];
- int op_len;
- int s;
- ssize_t ret;
- int32_t reply_len = 0;
- struct sockaddr_un srv_addr;
-
- if (((op == 4) || (op == 5)) && !num) {
- if (err_context)
- *err_context = _("bad arguments");
- errno = EINVAL;
- return -1;
- }
-
- if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
- if (err_context)
- *err_context = _("socket");
- return -1;
- }
-
- srv_addr.sun_family = AF_UNIX;
- strcpy(srv_addr.sun_path, socket_path);
-
- if (connect(s, (const struct sockaddr *) &srv_addr,
- sizeof(struct sockaddr_un)) < 0) {
- if (err_context)
- *err_context = _("connect");
- close(s);
- return -1;
- }
-
- if (op == 5) {
- if ((*num)*16 > buflen-4)
- *num = (buflen-4) / 16;
- }
- op_buf[0] = op;
- op_len = 1;
- if ((op == 4) || (op == 5)) {
- memcpy(op_buf+1, num, sizeof(int));
- op_len += sizeof(int);
- }
-
- ret = write(s, op_buf, op_len);
- if (ret < op_len) {
- if (err_context)
- *err_context = _("write");
- close(s);
- return -1;
- }
-
- ret = read_all(s, (char *) &reply_len, sizeof(reply_len));
- if (ret < 0) {
- if (err_context)
- *err_context = _("read count");
- close(s);
- return -1;
- }
- if (reply_len < 0 || reply_len > buflen) {
- if (err_context)
- *err_context = _("bad response length");
- close(s);
- return -1;
- }
- ret = read_all(s, (char *) buf, reply_len);
-
- if ((ret > 0) && (op == 4)) {
- if (reply_len >= (int) (16+sizeof(int)))
- memcpy(buf+16, num, sizeof(int));
- else
- *num = -1;
- }
- if ((ret > 0) && (op == 5)) {
- if (*num >= (int) sizeof(int))
- memcpy(buf, num, sizeof(int));
- else
- *num = -1;
- }
-
- close(s);
-
- return ret;
-}
-
-static int create_pidfile(const char *socket_path, const char *pidfile_path,
- int quiet)
-{
- int fd, ret;
- char buf[20];
-
- fd = open(pidfile_path, O_CREAT | O_RDWR, 0664);
- if (fd < 0) {
- if (!quiet)
- fprintf(stderr, "Failed to open/create %s: %s\n",
- pidfile_path, strerror(errno));
- exit(1);
- }
- cleanup_pidfile = pidfile_path;
- cleanup_socket = 0;
- signal(SIGALRM, terminate_intr);
- alarm(30);
- if (lockf(fd, F_LOCK, 0) < 0) {
- if (!quiet)
- fprintf(stderr, "Failed to lock %s: %s\n",
- pidfile_path, strerror(errno));
- exit(1);
- }
- ret = call_daemon(socket_path, 0, buf, sizeof(buf), 0, 0);
- if (ret > 0) {
- if (!quiet)
- printf(_("uuidd daemon already running at pid %s\n"),
- buf);
- exit(1);
- }
- alarm(0);
-
- sprintf(buf, "%d\n", getpid());
- ftruncate(fd, 0);
- write(fd, buf, strlen(buf));
- return(fd);
-}
-