]> mj.ucw.cz Git - umpf.git/blob - lock.c
rename to umpf, guess default mailbox
[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         for (i = 0; i < LOCK_MAX_TRIES; i++){
68                 if ((fd = open(lockfile, O_WRONLY | O_EXCL | O_CREAT, 0)) 
69                         >= 0) {
70                         close(fd);
71                         res = 0;
72                         break;
73                 }
74
75                 if (errno != EEXIST)
76                         break;
77
78                 /* FIXME: deal with old locks */
79
80                 random_sleep(1, 1);
81         }
82
83         free(lockfile);
84         return res;
85 }
86
87 static void
88 dot_unlock(char* path)
89 {
90         char* lockfile = cat(path, ".lock");
91
92         raise_gid();
93         unlink(lockfile);
94         drop_gid();
95         free(lockfile);
96 }
97
98 static int
99 do_open_mailbox(char* path, int is_default_mailbox)
100 {
101         int fd;
102
103         /* attempt to dot_lock first */
104         if (dot_lock(path))
105                 return -1;
106
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)
110         */
111         int perms = S_IRUSR | S_IWUSR;
112         if (is_default_mailbox)
113                 perms |=  (S_IRGRP | S_IWGRP);
114
115         mode_t oldmask = umask(0);
116         fd = open(path, O_RDWR | O_CREAT, perms);
117         umask(oldmask);
118         if (fd < 0){
119                 dot_unlock(path);
120                 return -1;
121         }       
122
123         /* fcntl then */
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;
128
129         if (fcntl(fd, F_GETLK, &mb_lock) < 0){
130                 close(fd);
131                 dot_unlock(path);
132                 return -1;
133         }
134
135         return fd; 
136 }
137
138 static void
139 do_close_mailbox(int fd, char* path)
140 {
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);
145         close(fd);
146         dot_unlock(path);
147 }
148
149 void
150 close_mailbox(int fd, char* path, int is_default_mailbox)
151 {
152         if (is_default_mailbox)
153                 raise_gid();
154         do_close_mailbox(fd, path);
155         if (is_default_mailbox)
156                 drop_gid();
157 }
158
159 int
160 open_mailbox(char* path, int is_default_mailbox)
161 {
162         int fd;
163
164         if (is_default_mailbox)
165                 raise_gid();
166         fd = do_open_mailbox(path, is_default_mailbox);
167         if (is_default_mailbox)
168                 drop_gid();
169
170         return fd;
171 }