]> mj.ucw.cz Git - umpf.git/blob - lock.c
attempt to cope with big emails
[umpf.git] / lock.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #include <sys/stat.h>
11
12 #include "umpf.h"
13
14 #define LOCK_MAX_TRIES 3
15 gid_t rgid, egid, sgid;
16
17 void
18 save_gids(void)
19 {
20         getresgid(&rgid, &egid, &sgid); 
21 }
22
23 static void
24 drop_gid(void)
25 {
26         setresgid(rgid, rgid, egid);
27 }
28
29 static void
30 raise_gid(void)
31 {
32         setresgid(rgid, sgid, sgid);
33 }
34
35 static void
36 random_sleep(unsigned int about, unsigned int offset)
37 {
38         int myrand;
39
40         if (about == 0 || offset > about)
41                 die("random sleep: Bad input data: %d +- %d", about, offset);
42
43         myrand = random() % offset * (random()%2?1:-1);
44
45         usleep(about * 1000000 + myrand * 500000);
46 }
47
48 char*
49 cat(char* l, char* r)
50 {
51         char* res = xmalloc(strlen(l) + strlen (r) + 1);
52         strcpy(res, l);
53         strcat(res, r);
54         
55         return res;     
56 }
57
58 /* FIXME: what about privileges? */
59 static int
60 dot_lock(char* path)
61 {
62         int i;
63         int fd;
64         int res = -1;
65         char* lockfile = cat(path, ".lock");
66
67         raise_gid();
68         for (i = 0; i < LOCK_MAX_TRIES; i++){
69                 if ((fd = open(lockfile, O_WRONLY | O_EXCL | O_CREAT, 0)) 
70                         >= 0) {
71                         close(fd);
72                         res = 0;
73                         break;
74                 }
75
76                 if (errno != EEXIST)
77                         break;
78
79                 /* FIXME: deal with old locks */
80                 random_sleep(1, 1);
81         }
82         drop_gid();
83
84         free(lockfile);
85         return res;
86 }
87
88 static void
89 dot_unlock(char* path)
90 {
91         char* lockfile = cat(path, ".lock");
92
93         raise_gid();
94         unlink(lockfile);
95         drop_gid();
96         free(lockfile);
97 }
98
99 static int
100 do_open_mailbox(char* path, int is_default_mailbox)
101 {
102         int fd;
103
104         /* attempt to dot_lock first */
105         if (dot_lock(path))
106                 return -1;
107
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)
111         */
112         int perms = S_IRUSR | S_IWUSR;
113         if (is_default_mailbox)
114                 perms |=  (S_IRGRP | S_IWGRP);
115
116         mode_t oldmask = umask(0);
117         fd = open(path, O_RDWR | O_CREAT, perms);
118         umask(oldmask);
119         if (fd < 0){
120                 dot_unlock(path);
121                 return -1;
122         }       
123
124         /* fcntl then */
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;
129
130         if (fcntl(fd, F_GETLK, &mb_lock) < 0){
131                 close(fd);
132                 dot_unlock(path);
133                 return -1;
134         }
135
136         return fd; 
137 }
138
139 static void
140 do_close_mailbox(int fd, char* path)
141 {
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);
146         close(fd);
147         dot_unlock(path);
148 }
149
150 void
151 close_mailbox(int fd, char* path, int is_default_mailbox)
152 {
153         if (is_default_mailbox)
154                 raise_gid();
155         do_close_mailbox(fd, path);
156         if (is_default_mailbox)
157                 drop_gid();
158 }
159
160 int
161 open_mailbox(char* path, int is_default_mailbox)
162 {
163         int fd;
164
165         if (is_default_mailbox)
166                 raise_gid();
167         fd = do_open_mailbox(path, is_default_mailbox);
168         if (is_default_mailbox)
169                 drop_gid();
170
171         return fd;
172 }