Show More
@@ -33,16 +33,13 b' struct cmdserveropts {' | |||
|
33 | 33 | char sockname[UNIX_PATH_MAX]; |
|
34 | 34 | char initsockname[UNIX_PATH_MAX]; |
|
35 | 35 | char redirectsockname[UNIX_PATH_MAX]; |
|
36 | char lockfile[UNIX_PATH_MAX]; | |
|
37 | 36 | size_t argsize; |
|
38 | 37 | const char **args; |
|
39 | int lockfd; | |
|
40 | 38 | int sockdirfd; |
|
41 | 39 | }; |
|
42 | 40 | |
|
43 | 41 | static void initcmdserveropts(struct cmdserveropts *opts) { |
|
44 | 42 | memset(opts, 0, sizeof(struct cmdserveropts)); |
|
45 | opts->lockfd = -1; | |
|
46 | 43 | opts->sockdirfd = -1; |
|
47 | 44 | } |
|
48 | 45 | |
@@ -50,7 +47,6 b' static void freecmdserveropts(struct cmd' | |||
|
50 | 47 | free(opts->args); |
|
51 | 48 | opts->args = NULL; |
|
52 | 49 | opts->argsize = 0; |
|
53 | assert(opts->lockfd == -1 && "should be closed by unlockcmdserver()"); | |
|
54 | 50 | if (opts->sockdirfd >= 0) { |
|
55 | 51 | close(opts->sockdirfd); |
|
56 | 52 | opts->sockdirfd = -1; |
@@ -157,52 +153,15 b' static void setcmdserveropts(struct cmds' | |||
|
157 | 153 | |
|
158 | 154 | const char *basename = (envsockname) ? envsockname : sockdir; |
|
159 | 155 | const char *sockfmt = (envsockname) ? "%s" : "%s/server"; |
|
160 | const char *lockfmt = (envsockname) ? "%s.lock" : "%s/lock"; | |
|
161 | 156 | r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename); |
|
162 | 157 | if (r < 0 || (size_t)r >= sizeof(opts->sockname)) |
|
163 | 158 | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); |
|
164 | r = snprintf(opts->lockfile, sizeof(opts->lockfile), lockfmt, basename); | |
|
165 | if (r < 0 || (size_t)r >= sizeof(opts->lockfile)) | |
|
166 | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); | |
|
167 | 159 | r = snprintf(opts->initsockname, sizeof(opts->initsockname), |
|
168 | 160 | "%s.%u", opts->sockname, (unsigned)getpid()); |
|
169 | 161 | if (r < 0 || (size_t)r >= sizeof(opts->initsockname)) |
|
170 | 162 | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); |
|
171 | 163 | } |
|
172 | 164 | |
|
173 | /* | |
|
174 | * Acquire a file lock that indicates a client is trying to start and connect | |
|
175 | * to a server, before executing a command. The lock is released upon exit or | |
|
176 | * explicit unlock. Will block if the lock is held by another process. | |
|
177 | */ | |
|
178 | static void lockcmdserver(struct cmdserveropts *opts) | |
|
179 | { | |
|
180 | if (opts->lockfd == -1) { | |
|
181 | opts->lockfd = open(opts->lockfile, | |
|
182 | O_RDWR | O_CREAT | O_NOFOLLOW, 0600); | |
|
183 | if (opts->lockfd == -1) | |
|
184 | abortmsgerrno("cannot create lock file %s", | |
|
185 | opts->lockfile); | |
|
186 | fsetcloexec(opts->lockfd); | |
|
187 | } | |
|
188 | int r = flock(opts->lockfd, LOCK_EX); | |
|
189 | if (r == -1) | |
|
190 | abortmsgerrno("cannot acquire lock"); | |
|
191 | } | |
|
192 | ||
|
193 | /* | |
|
194 | * Release the file lock held by calling lockcmdserver. Will do nothing if | |
|
195 | * lockcmdserver is not called. | |
|
196 | */ | |
|
197 | static void unlockcmdserver(struct cmdserveropts *opts) | |
|
198 | { | |
|
199 | if (opts->lockfd == -1) | |
|
200 | return; | |
|
201 | flock(opts->lockfd, LOCK_UN); | |
|
202 | close(opts->lockfd); | |
|
203 | opts->lockfd = -1; | |
|
204 | } | |
|
205 | ||
|
206 | 165 | static const char *gethgcmd(void) |
|
207 | 166 | { |
|
208 | 167 | static const char *hgcmd = NULL; |
@@ -308,14 +267,6 b' static hgclient_t *connectcmdserver(stru' | |||
|
308 | 267 | if (hgc) |
|
309 | 268 | return hgc; |
|
310 | 269 | |
|
311 | lockcmdserver(opts); | |
|
312 | hgc = hgc_open(sockname); | |
|
313 | if (hgc) { | |
|
314 | unlockcmdserver(opts); | |
|
315 | debugmsg("cmdserver is started by another process"); | |
|
316 | return hgc; | |
|
317 | } | |
|
318 | ||
|
319 | 270 | /* prevent us from being connected to an outdated server: we were |
|
320 | 271 | * told by a server to redirect to opts->redirectsockname and that |
|
321 | 272 | * address does not work. we do not want to connect to the server |
@@ -334,7 +285,6 b' static hgclient_t *connectcmdserver(stru' | |||
|
334 | 285 | hgc = retryconnectcmdserver(opts, pid); |
|
335 | 286 | } |
|
336 | 287 | |
|
337 | unlockcmdserver(opts); | |
|
338 | 288 | return hgc; |
|
339 | 289 | } |
|
340 | 290 |
General Comments 0
You need to be logged in to leave comments.
Login now