diff options
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 41 |
1 files changed, 41 insertions, 0 deletions
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 <ctype.h> #include <sys/types.h> #include <sys/stat.h> +#include <fcntl.h> #include <string.h> #include <linux/limits.h> #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; +} |