Rizin
unix-like reverse engineering framework and cli tools
lock.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2012-2016 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: MIT
3 
4 #include <rz_userconf.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <rz_util/rz_sys.h>
9 #include "sdb.h"
10 #if __WINDOWS__
11 #include <windows.h>
12 #endif
13 
14 RZ_API const char *sdb_lock_file(const char *f) {
15  static char buf[128];
16  size_t len;
17  if (!f || !*f) {
18  return NULL;
19  }
20  len = strlen(f);
21  if (len + 10 > sizeof buf) {
22  return NULL;
23  }
24  memcpy(buf, f, len);
25  strcpy(buf + len, ".lock");
26  return buf;
27 }
28 
29 RZ_API bool sdb_lock(const char *s) {
30  int fd;
31  char *pid, pidstr[64];
32  if (!s) {
33  return false;
34  }
35  fd = open(s, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL, SDB_MODE);
36  if (fd == -1) {
37  return false;
38  }
39  pid = sdb_itoa(rz_sys_getpid(), pidstr, 10);
40  if (pid) {
41  if ((write(fd, pid, strlen(pid)) < 0) || (write(fd, "\n", 1) < 0)) {
42  close(fd);
43  return false;
44  }
45  }
46  close(fd);
47  return true;
48 }
49 
50 RZ_API int sdb_lock_wait(const char *s) {
51  // TODO use flock() here
52  // wait forever here?
53  while (!sdb_lock(s)) {
54  // TODO: if waiting too much return 0
55  rz_sys_sleep(1);
56  }
57  return 1;
58 }
59 
60 RZ_API void sdb_unlock(const char *s) {
61  // flock (fd, LOCK_UN);
62  unlink(s);
63 }
size_t len
Definition: 6502dis.c:15
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static fork write
Definition: sflib.h:33
static static fork const void static count close
Definition: sflib.h:33
voidpf void * buf
Definition: ioapi.h:138
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
static static fork const void static count static fd const char static mode unlink
Definition: sflib.h:41
RZ_API void sdb_unlock(const char *s)
Definition: lock.c:60
RZ_API bool sdb_lock(const char *s)
Definition: lock.c:29
RZ_API int sdb_lock_wait(const char *s)
Definition: lock.c:50
RZ_API const char * sdb_lock_file(const char *f)
Definition: lock.c:14
static RzSocket * s
Definition: rtr.c:28
RZ_API int rz_sys_getpid(void)
Definition: sys.c:1164
RZ_API int rz_sys_sleep(int secs)
Sleep for secs seconds.
Definition: sys.c:300
#define SDB_MODE
Definition: sdb.h:42
RZ_API char * sdb_itoa(ut64 n, char *s, int base)
Definition: util.c:38
#define O_WRONLY
Definition: sftypes.h:487
#define O_CREAT
Definition: sftypes.h:489
#define O_EXCL
Definition: sftypes.h:490
#define O_TRUNC
Definition: sftypes.h:492
#define f(i)
Definition: sha256.c:46
static const z80_opcode fd[]
Definition: z80_tab.h:997