From b7e06668878af06fc6e74842861e725b982b142f Mon Sep 17 00:00:00 2001 From: James Maki Date: Fri, 7 May 2010 10:12:55 -0500 Subject: add device lock --- src/utils.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 9149319..a794f9b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -30,12 +30,23 @@ #include #include #include +#include #include #include #include "utils.h" #include "log.h" +char *basename(const char *path) +{ + const char *cp = strrchr(path, '/'); + if(cp) { + return (char *) cp + 1; + } else { + return (char *) path; + } +} + int tokcmp(const char *cmd, const char *pattern) { int len = strlen(cmd); @@ -234,3 +245,33 @@ char *shell_path_expand(const char *path) return strdup(line); } + +char *device_lock(const char *path) +{ + int tmp; + char *name = basename(path); + int fd; + char str[32]; + char *lock; + + tmp = asprintf(&lock, "/var/lock/LCK..%s", name); + if (tmp < 0) { + log_error("out of memory"); + return NULL; + } + + fd = open(lock, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644); + if (fd < 0) { + log_error("failed to create %s: %m", lock); + free(lock); + return NULL; + } + + snprintf(str, sizeof(str), "%10d\n", getpid()); + + full_write(fd, str, strlen(str)); + + close(fd); + + return lock; +} -- cgit v1.2.3