14 #define LOCK_MAX_TRIES 3
15 gid_t rgid, egid, sgid;
20 getresgid(&rgid, &egid, &sgid);
26 setresgid(rgid, rgid, egid);
32 setresgid(rgid, sgid, sgid);
36 random_sleep(unsigned int about, unsigned int offset)
40 if (about == 0 || offset > about)
41 die("random sleep: Bad input data: %d +- %d", about, offset);
43 myrand = random() % offset * (random()%2?1:-1);
45 usleep(about * 1000000 + myrand * 500000);
51 char* res = xmalloc(strlen(l) + strlen (r) + 1);
58 /* FIXME: what about privileges? */
65 char* lockfile = cat(path, ".lock");
68 for (i = 0; i < LOCK_MAX_TRIES; i++){
69 if ((fd = open(lockfile, O_WRONLY | O_EXCL | O_CREAT, 0))
79 /* FIXME: deal with old locks */
89 dot_unlock(char* path)
91 char* lockfile = cat(path, ".lock");
100 do_open_mailbox(char* path, int is_default_mailbox)
104 /* attempt to dot_lock first */
108 /* if OK, try to open the file:
109 either we are saving to default mailbox (no problem here)
110 or we are saving somewhere else (no need to be privileged)
112 int perms = S_IRUSR | S_IWUSR;
113 if (is_default_mailbox)
114 perms |= (S_IRGRP | S_IWGRP);
116 mode_t oldmask = umask(0);
117 fd = open(path, O_RDWR | O_CREAT, perms);
125 struct flock mb_lock;
126 memset(&mb_lock, 0, sizeof(struct flock));
127 mb_lock.l_type = F_WRLCK;
128 mb_lock.l_whence = SEEK_SET;
130 if (fcntl(fd, F_GETLK, &mb_lock) < 0){
140 do_close_mailbox(int fd, char* path)
142 struct flock mb_lock;
143 memset(&mb_lock, 0, sizeof(struct flock));
144 mb_lock.l_type = F_UNLCK;
145 fcntl(fd, F_SETLK, &mb_lock);
151 close_mailbox(int fd, char* path, int is_default_mailbox)
153 if (is_default_mailbox)
155 do_close_mailbox(fd, path);
156 if (is_default_mailbox)
161 open_mailbox(char* path, int is_default_mailbox)
165 if (is_default_mailbox)
167 fd = do_open_mailbox(path, is_default_mailbox);
168 if (is_default_mailbox)