Rizin
unix-like reverse engineering framework and cli tools
lock.c File Reference
#include <rz_userconf.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <rz_util/rz_sys.h>
#include "sdb.h"

Go to the source code of this file.

Functions

RZ_API const char * sdb_lock_file (const char *f)
 
RZ_API bool sdb_lock (const char *s)
 
RZ_API int sdb_lock_wait (const char *s)
 
RZ_API void sdb_unlock (const char *s)
 

Function Documentation

◆ sdb_lock()

RZ_API bool sdb_lock ( const char *  s)

Definition at line 29 of file lock.c.

29  {
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 }
static static fork write
Definition: sflib.h:33
static static fork const void static count close
Definition: sflib.h:33
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 RzSocket * s
Definition: rtr.c:28
RZ_API int rz_sys_getpid(void)
Definition: sys.c:1164
#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
static const z80_opcode fd[]
Definition: z80_tab.h:997

References close, fd, O_CREAT, O_EXCL, O_TRUNC, O_WRONLY, pid, rz_sys_getpid(), s, sdb_itoa(), SDB_MODE, and write.

Referenced by sdb_file(), sdb_lock_wait(), and sdb_new().

◆ sdb_lock_file()

RZ_API const char* sdb_lock_file ( const char *  f)

Definition at line 14 of file lock.c.

14  {
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 }
size_t len
Definition: 6502dis.c:15
#define NULL
Definition: cris-opc.c:27
voidpf void * buf
Definition: ioapi.h:138
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define f(i)
Definition: sha256.c:46

References f, len, memcpy(), and NULL.

Referenced by sdb_file(), sdb_fini(), and sdb_new().

◆ sdb_lock_wait()

RZ_API int sdb_lock_wait ( const char *  s)

Definition at line 50 of file lock.c.

50  {
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 }
RZ_API bool sdb_lock(const char *s)
Definition: lock.c:29
RZ_API int rz_sys_sleep(int secs)
Sleep for secs seconds.
Definition: sys.c:300

References rz_sys_sleep(), s, and sdb_lock().

Referenced by sdb_new().

◆ sdb_unlock()

RZ_API void sdb_unlock ( const char *  s)

Definition at line 60 of file lock.c.

60  {
61  // flock (fd, LOCK_UN);
62  unlink(s);
63 }
static static fork const void static count static fd const char static mode unlink
Definition: sflib.h:41

References s, and unlink.

Referenced by sdb_file(), and sdb_fini().