13 #define LOCK_MAX_TRIES 30
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");
67 for (i = 0; i < LOCK_MAX_TRIES; i++){
68 if ((fd = open(lockfile, O_WRONLY | O_EXCL | O_CREAT, 0))
78 /* FIXME: deal with old locks */
88 dot_unlock(char* path)
90 char* lockfile = cat(path, ".lock");
99 do_open_mailbox(char* path, int is_default_mailbox)
103 /* attempt to dot_lock first */
107 /* if OK, try to open the file:
108 either we are saving to default mailbox (no problem here)
109 or we are saving somewhere else (no need to be privileged)
111 int perms = S_IRUSR | S_IWUSR;
112 if (is_default_mailbox)
113 perms |= (S_IRGRP | S_IWGRP);
115 mode_t oldmask = umask(0);
116 fd = open(path, O_RDWR | O_CREAT, perms);
124 struct flock mb_lock;
125 memset(&mb_lock, 0, sizeof(struct flock));
126 mb_lock.l_type = F_WRLCK;
127 mb_lock.l_whence = SEEK_SET;
129 if (fcntl(fd, F_GETLK, &mb_lock) < 0){
139 do_close_mailbox(int fd, char* path)
141 struct flock mb_lock;
142 memset(&mb_lock, 0, sizeof(struct flock));
143 mb_lock.l_type = F_UNLCK;
144 fcntl(fd, F_SETLK, &mb_lock);
150 close_mailbox(int fd, char* path, int is_default_mailbox)
152 if (is_default_mailbox)
154 do_close_mailbox(fd, path);
155 if (is_default_mailbox)
160 open_mailbox(char* path, int is_default_mailbox)
164 if (is_default_mailbox)
166 fd = do_open_mailbox(path, is_default_mailbox);
167 if (is_default_mailbox)
174 main(int argc, char** argv)
176 /* FIXME: move somewhere */
181 int is_default_mailbox = atoi(argv[2]);
184 fd = open_mailbox(mb, is_default_mailbox);
189 close_mailbox(fd, mb, is_default_mailbox);