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))
58 /* FIXME: deal with old locks */
68 dot_unlock(char* path)
70 char* lockfile = cat(path, ".lock");
78 open_mailbox(char* path, int use_default_mailbox)
82 /* attempt to dot_lock first */
86 /* if OK, try to open the file:
87 either we are saving to default mailbox (no problem here)
88 or we are saving somewhere else (no need to be privileged)
90 if (use_default_mailbox){
91 /* get our preciousss out of pocket */
92 gid_t ruid, euid, suid;
93 getresgid(&ruid, &euid, &suid);
94 setresgid(ruid, suid, suid);
96 /* we are not going to create default mailbox */
97 fd = open(path, O_RDWR);
104 setresgid(ruid, ruid, suid);
106 fd = open(path, O_RDWR);
107 if (fd < 0 && errno == ENOENT){
108 fd = open(path, O_RDWR | O_CREAT | O_EXCL,
122 struct flock mb_lock;
123 memset(&mb_lock, 0, sizeof(struct flock));
124 mb_lock.l_type = F_WRLCK;
125 mb_lock.l_whence = SEEK_SET;
127 if (fcntl(fd, F_GETLK, &mb_lock) < 0){
137 close_mailbox(int fd, char* path)
139 struct flock mb_lock;
140 memset(&mb_lock, 0, sizeof(struct flock));
141 mb_lock.l_type = F_UNLCK;
142 fcntl(fd, F_SETLK, &mb_lock);
148 main(int argc, char** argv)
150 /* FIXME: move somewhere */
152 gid_t ruid, euid, suid;
156 getresgid(&ruid, &euid, &suid);
157 setresgid(ruid, ruid, euid);
158 fd = open_mailbox(mb, 0);
163 close_mailbox(fd, mb);