X-Git-Url: https://git.jankratochvil.net/?p=gnokii.git;a=blobdiff_plain;f=common%2Fmisc.c;h=46f8958c48dff02620039d1af1c11356fefe5f50;hp=b17a93e71ff36959593d64fe50c761cd82bc4152;hb=2a2206f128cdd3ef136166e586026c774da5c490;hpb=2656067281b2441a179b8492b95ee8992aa0225c diff --git a/common/misc.c b/common/misc.c index b17a93e..46f8958 100644 --- a/common/misc.c +++ b/common/misc.c @@ -8,9 +8,9 @@ */ -#include -#include -#include +#include +#include +#include #ifndef WIN32 #include @@ -20,10 +20,10 @@ #include #include #include -#endif - -#include "misc.h" -#include "gsm-common.h" +#endif + +#include "misc.h" +#include "gsm-common.h" #ifndef HAVE_TIMEOPS @@ -153,88 +153,3 @@ void txhexdump(u16 MessageLength, u8 *MessageBuffer) fflush(stdout); } #endif - -#ifndef WIN32 - -#define max_buf_len 128 -#define lock_path "/var/lock/LCK.." - -/* Lock the device. Return allocated string with a lock name */ -char *lock_device(const char* port) -{ - char *lock_file = NULL; - char buffer[max_buf_len]; - char *aux = rindex(port, '/'); - int fd, len = strlen(aux) + strlen(lock_path); - - memset(buffer, 0, sizeof(buffer)); - lock_file = calloc(len + 1, 1); - if (!lock_file) { - fprintf(stderr, _("Cannot lock device\n")); - return NULL; - } - /* I think we don't need to use strncpy, as we should have enough - * buffer due to strlen results - */ - strcpy(lock_file, lock_path); - strcat(lock_file, aux); - - /* Check for the stale lockfile. - * The code taken from minicom by Miquel van Smoorenburg */ - if ((fd = open(lock_file, O_RDONLY)) >= 0) { - char buf[max_buf_len]; - int pid, n = 0; - - n = read(fd, buf, sizeof(buf) - 1); - close(fd); - if (n > 0) { - pid = -1; - if (n == 4) - /* Kermit-style lockfile. */ - pid = *(int *)buf; - else { - /* Ascii lockfile. */ - buf[n] = 0; - sscanf(buf, "%d", &pid); - } - if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) { - fprintf(stderr, _("Lockfile is stale. Overriding it..\n")); - sleep(1); - unlink(lock_file); - } else - n = 0; - } - if (n == 0) { - free(lock_file); - fprintf(stderr, _("Device is already locked.\n")); - return NULL; - } - } - - /* Try to create a new file, with 0644 mode */ - fd = open(lock_file, O_CREAT | O_EXCL, 0644); - if (fd == -1) { - free(lock_file); - fprintf(stderr, _("Cannot lock device\n")); - return NULL; - } - sprintf(buffer, "%10ld gnokii\n", (long)getpid()); - write(fd, buffer, strlen(buffer)); - close(fd); - return lock_file; -} - -/* Removes lock and frees memory */ -bool unlock_device(char *lock_file) -{ - int err; - - if (!lock_file) { - fprintf(stderr, _("Cannot unlock device\n")); - return false; - } - err = unlink(lock_file); - free(lock_file); - return (err + 1); -} -#endif /* WIN32 */