From d89c57ee89ba09ea141654741e9cd95ee5726fd3 Mon Sep 17 00:00:00 2001 From: Jan Friesse Date: Mon, 14 Mar 2016 10:16:21 +0100 Subject: [PATCH] qnetd: Return lock file fd Signed-off-by: Jan Friesse --- qdevices/corosync-qnetd.c | 3 ++- qdevices/utils.c | 13 ++----------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/qdevices/corosync-qnetd.c b/qdevices/corosync-qnetd.c index bf6ebb22..089749b2 100644 --- a/qdevices/corosync-qnetd.c +++ b/qdevices/corosync-qnetd.c @@ -313,6 +313,7 @@ main(int argc, char *argv[]) int client_cert_required; size_t max_clients; PRIntn address_family; + int lock_file; cli_parse(argc, argv, &host_addr, &host_port, &foreground, &debug_log, &bump_log_priority, &tls_supported, &client_cert_required, &max_clients, &address_family); @@ -333,7 +334,7 @@ main(int argc, char *argv[]) utils_tty_detach(); } - if (utils_flock(QNETD_LOCK_FILE, getpid(), qnetd_log_printf) != 0) { + if ((lock_file = utils_flock(QNETD_LOCK_FILE, getpid(), qnetd_log_printf)) == -1) { exit(1); } diff --git a/qdevices/utils.c b/qdevices/utils.c index e5b27f41..00205260 100644 --- a/qdevices/utils.c +++ b/qdevices/utils.c @@ -76,9 +76,6 @@ utils_flock(const char *lockfile, pid_t pid, char pid_s[17]; int fd_flag; int lf; - int res; - - res = 0; lf = open(lockfile, O_WRONLY | O_CREAT, 0640); if (lf == -1) { @@ -100,12 +97,10 @@ retry_fcntl: case EAGAIN: case EACCES: log_printf(LOG_ERR, "Another instance is already running."); - res = -1; goto error_close; break; default: log_printf(LOG_ERR, "Cannot aquire lock. Error was %s", strerror(errno)); - res = -1; goto error_close; break; } @@ -113,7 +108,6 @@ retry_fcntl: if (ftruncate(lf, 0) == -1) { log_printf(LOG_ERR, "Cannot truncate lock file. Error was %s", strerror(errno)); - res = -1; goto error_close_unlink; } @@ -127,7 +121,6 @@ retry_write: } else { log_printf(LOG_ERR, "Cannot write pid to lock file. Error was %s", strerror(errno)); - res = -1; goto error_close_unlink; } } @@ -135,25 +128,23 @@ retry_write: if ((fd_flag = fcntl(lf, F_GETFD, 0)) == -1) { log_printf(LOG_ERR, "Cannot get close-on exec flag for lock file. Error was %s", strerror(errno)); - res = -1; goto error_close_unlink; } fd_flag |= FD_CLOEXEC; if (fcntl(lf, F_SETFD, fd_flag) == -1) { log_printf(LOG_ERR, "Cannot set close-on-exec flag for lock file. Error was %s", strerror(errno)); - res = -1; goto error_close_unlink; } - return (res); + return (lf); error_close_unlink: unlink(lockfile); error_close: close(lf); - return (res); + return (-1); } void -- 2.39.5