diff options
author | James Maki <jmaki@multitech.com> | 2010-05-07 10:12:55 -0500 |
---|---|---|
committer | James Maki <jmaki@multitech.com> | 2010-05-07 10:12:55 -0500 |
commit | b7e06668878af06fc6e74842861e725b982b142f (patch) | |
tree | 4709270926ad7fc804fd71d455503b6945224c0f /src/utils.c | |
parent | 2b85ae17d6c73c619d17f8987069c0e6aaf390a2 (diff) | |
download | sms-utils-b7e06668878af06fc6e74842861e725b982b142f.tar.gz sms-utils-b7e06668878af06fc6e74842861e725b982b142f.tar.bz2 sms-utils-b7e06668878af06fc6e74842861e725b982b142f.zip |
add device lock
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; +} |