13 #define LOCK_MAX_TRIES 30
16 random_sleep(unsigned int about, unsigned int offset)
20 if (about == 0 || offset > about)
21 die("random sleep: Bad input data: %d +- %d", about, offset);
23 myrand = random() % offset * (random()%2?1:-1);
25 usleep(about * 1000000 + myrand * 500000);
31 char* res = xmalloc(strlen(l) + strlen (r) + 1);
38 /* FIXME: what about privileges? */
45 char* lockfile = cat(path, ".lock");
47 for (i = 0; i < LOCK_MAX_TRIES; i++){
48 if ((fd = open(lockfile, O_WRONLY | O_EXCL | O_CREAT, 0))
57 /* FIXME: deal with old locks */
67 dot_unlock(char* path)
69 char* lockfile = cat(path, ".lock");
77 open_mailbox(char* path, int use_default_mailbox)
81 /* attempt to dot_lock first */
85 /* if OK, try to open the file:
86 either we are saving to default mailbox (no problem here)
87 or we are saving somewhere else (no need to be privileged)
89 if (use_default_mailbox){
90 /* get our preciousss out of pocket */
91 gid_t ruid, euid, suid;
92 getresgid(&ruid, &euid, &suid);
93 setresgid(ruid, suid, suid);
95 /* we are not going to create default mailbox */
96 fd = open(path, O_RDWR);
101 setresgid(ruid, ruid, suid);
103 fd = open(path, O_RDWR);
104 if (fd < 0 && errno == ENOENT){
105 fd = open(path, O_RDWR | O_CREAT | O_EXCL,
115 struct flock mb_lock;
116 memset(&mb_lock, 0, sizeof(struct flock));
117 mb_lock.l_type = F_WRLCK;
118 mb_lock.l_whence = SEEK_SET;
120 if (fcntl(fd, F_GETLK, &mb_lock) < 0){
130 close_mailbox(int fd, char* path)
132 struct flock mb_lock;
133 memset(&mb_lock, 0, sizeof(struct flock));
134 mb_lock.l_type = F_UNLCK;
135 fcntl(fd, F_SETLK, &mb_lock);
141 main(int argc, char** argv)
143 /* FIXME: move somewhere */
145 gid_t ruid, euid, suid;
149 getresgid(&ruid, &euid, &suid);
150 setresgid(ruid, ruid, euid);
151 fd = open_mailbox(mb, 1);
156 close_mailbox(fd, mb);