Signed-off-by: Jim Rees <rees@xxxxxxxxx> --- utils/blkmapd/device-discovery.c | 69 +++++++++++++++++++++---------------- 1 files changed, 39 insertions(+), 30 deletions(-) diff --git a/utils/blkmapd/device-discovery.c b/utils/blkmapd/device-discovery.c index c8bd39c..6b1f942 100644 --- a/utils/blkmapd/device-discovery.c +++ b/utils/blkmapd/device-discovery.c @@ -398,54 +398,63 @@ int bl_run_disk_inquiry_process(int fd) /* Daemon */ int main(int argc, char **argv) { - int fd, opt, fg = 0, ret = 1; + int fd = -1, opt, dflag = 0, fg = 0, ret = 1; struct stat statbuf; char pidbuf[64]; - while ((opt = getopt(argc, argv, "f")) != -1) { + while ((opt = getopt(argc, argv, "df")) != -1) { switch (opt) { + case 'd': + dflag = 1; + break; case 'f': fg = 1; break; } } - if (!stat(PID_FILE, &statbuf)) { - fprintf(stderr, "Pid file already existed\n"); - return -1; - } + if (fg) { + openlog("blkmapd", LOG_PERROR, 0); + } else { + if (!stat(PID_FILE, &statbuf)) { + fprintf(stderr, "Pid file already existed\n"); + return -1; + } - if (!fg && daemon(0, 0) != 0) { - fprintf(stderr, "Daemonize failed\n"); - return -1; - } + if (daemon(0, 0) != 0) { + fprintf(stderr, "Daemonize failed\n"); + return -1; + } - openlog("blkmapd", LOG_PID, 0); - fd = open(PID_FILE, O_WRONLY | O_CREAT, 0644); - if (fd < 0) { - BL_LOG_ERR("Create pid file failed\n"); - return -1; - } + openlog("blkmapd", LOG_PID, 0); + fd = open(PID_FILE, O_WRONLY | O_CREAT, 0644); + if (fd < 0) { + BL_LOG_ERR("Create pid file failed\n"); + return -1; + } - if (lockf(fd, F_TLOCK, 0) < 0) { - BL_LOG_ERR("Lock pid file failed\n"); - close(fd); - return -1; - } - ftruncate(fd, 0); - sprintf(pidbuf, "%d\n", getpid()); - write(fd, pidbuf, strlen(pidbuf)); - - /* open pipe file */ - fd = open(BL_PIPE_FILE, O_RDWR); - if (fd < 0) { - BL_LOG_ERR("open pipe file error\n"); - return -1; + if (lockf(fd, F_TLOCK, 0) < 0) { + BL_LOG_ERR("Lock pid file failed\n"); + close(fd); + return -1; + } + ftruncate(fd, 0); + sprintf(pidbuf, "%d\n", getpid()); + write(fd, pidbuf, strlen(pidbuf)); + + /* open pipe file */ + fd = open(BL_PIPE_FILE, O_RDWR); + if (fd < 0) { + BL_LOG_ERR("open pipe file error\n"); + return -1; + } } while (1) { /* discover device when needed */ bl_discover_devices(); + if (dflag) + break; ret = bl_run_disk_inquiry_process(fd); if (ret < 0) { -- 1.7.1 -- To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html