15 #define LOCK_MAX_TRIES 3
16 gid_t rgid, egid, sgid;
21 getresgid(&rgid, &egid, &sgid);
27 setresgid(rgid, rgid, egid);
33 setresgid(rgid, sgid, sgid);
37 random_sleep(unsigned int about, unsigned int offset)
41 if (about == 0 || offset > about)
42 die("random sleep: Bad input data: %d +- %d", about, offset);
44 myrand = random() % offset * (random()%2?1:-1);
46 usleep(about * 1000000 + myrand * 500000);
52 char* res = xmalloc(strlen(l) + strlen (r) + 1);
65 char* lockfile = cat(path, ".lock");
68 for (i = 0; i < LOCK_MAX_TRIES; i++){
71 if ((fd = open(lockfile, O_WRONLY | O_EXCL | O_CREAT, 0))
80 /* deal with old locks */
82 if ((time(NULL) - buf.st_mtime) > 3600)
93 dot_unlock(char* path)
95 char* lockfile = cat(path, ".lock");
104 do_open_mailbox(char* path, int is_default_mailbox)
108 /* attempt to dot_lock first */
112 /* if OK, try to open the file:
113 either we are saving to default mailbox (no problem here)
114 or we are saving somewhere else (no need to be privileged)
116 int perms = S_IRUSR | S_IWUSR;
117 if (is_default_mailbox)
118 perms |= (S_IRGRP | S_IWGRP);
120 mode_t oldmask = umask(0);
121 fd = open(path, O_RDWR | O_CREAT, perms);
129 struct flock mb_lock;
130 memset(&mb_lock, 0, sizeof(struct flock));
131 mb_lock.l_type = F_WRLCK;
132 mb_lock.l_whence = SEEK_SET;
134 if (fcntl(fd, F_GETLK, &mb_lock) < 0){
144 do_close_mailbox(int fd, char* path)
146 struct flock mb_lock;
147 memset(&mb_lock, 0, sizeof(struct flock));
148 mb_lock.l_type = F_UNLCK;
149 fcntl(fd, F_SETLK, &mb_lock);
155 close_mailbox(int fd, char* path, int is_default_mailbox)
157 if (is_default_mailbox)
159 do_close_mailbox(fd, path);
160 if (is_default_mailbox)
165 open_mailbox(char* path, int is_default_mailbox)
169 if (is_default_mailbox)
171 fd = do_open_mailbox(path, is_default_mailbox);
172 if (is_default_mailbox)