summaryrefslogtreecommitdiffstats
path: root/misc-utils
diff options
context:
space:
mode:
authorPetr Uzel2012-05-03 21:01:49 +0200
committerKarel Zak2012-05-04 15:13:34 +0200
commitc453635572d6e5c4c7a735c6339e92721080afbd (patch)
tree88c8168c36769029d1cc2c9c2b2f9a81a6a67517 /misc-utils
parentuuidd: remove useless initialization of cleanup_socket (diff)
downloadkernel-qcow2-util-linux-c453635572d6e5c4c7a735c6339e92721080afbd.tar.gz
kernel-qcow2-util-linux-c453635572d6e5c4c7a735c6339e92721080afbd.tar.xz
kernel-qcow2-util-linux-c453635572d6e5c4c7a735c6339e92721080afbd.zip
uuidd: factor out pidfile creation into separate function
Introduce create_pidfile() function. Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
Diffstat (limited to 'misc-utils')
-rw-r--r--misc-utils/uuidd.c48
1 files changed, 34 insertions, 14 deletions
diff --git a/misc-utils/uuidd.c b/misc-utils/uuidd.c
index 0d844fbb6..704c9e968 100644
--- a/misc-utils/uuidd.c
+++ b/misc-utils/uuidd.c
@@ -207,19 +207,18 @@ static int call_daemon(const char *socket_path, int op, char *buf,
return ret;
}
-static void server_loop(const char *socket_path, const char *pidfile_path,
- int debug, int timeout, int quiet)
+/*
+ * Exclusively create and open a pid file with path @pidfile_path
+ *
+ * Set cleanup_pidfile global variable for the cleanup
+ * handler. @pidfile_path must not be NULL.
+ *
+ * Return file descriptor of the created pid_file.
+ */
+static int create_pidfile(const char *pidfile_path, int quiet)
{
- struct sockaddr_un my_addr, from_addr;
- struct flock fl;
- socklen_t fromlen;
- int32_t reply_len = 0;
- uuid_t uu;
- mode_t save_umask;
- char reply_buf[1024], *cp;
- char op, str[UUID_STR_LEN];
- int i, s, ns, len, num;
- int fd_pidfile, ret;
+ int fd_pidfile;
+ struct flock fl;
fd_pidfile = open(pidfile_path, O_CREAT | O_RDWR, 0664);
if (fd_pidfile < 0) {
@@ -229,8 +228,7 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
exit(EXIT_FAILURE);
}
cleanup_pidfile = pidfile_path;
- signal(SIGALRM, terminate_intr);
- alarm(30);
+
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
@@ -243,6 +241,28 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
fprintf(stderr, _("Failed to lock %s: %m\n"), pidfile_path);
exit(EXIT_FAILURE);
}
+
+ return fd_pidfile;
+}
+
+static void server_loop(const char *socket_path, const char *pidfile_path,
+ int debug, int timeout, int quiet)
+{
+ struct sockaddr_un my_addr, from_addr;
+ socklen_t fromlen;
+ int32_t reply_len = 0;
+ uuid_t uu;
+ mode_t save_umask;
+ char reply_buf[1024], *cp;
+ char op, str[UUID_STR_LEN];
+ int i, s, ns, len, num;
+ int fd_pidfile, ret;
+
+ signal(SIGALRM, terminate_intr);
+ alarm(30);
+
+ fd_pidfile = create_pidfile(pidfile_path, quiet);
+
ret = call_daemon(socket_path, UUIDD_OP_GETPID, reply_buf, sizeof(reply_buf), 0, NULL);
if (ret > 0) {
if (!quiet)