comparison lib-src/movemail.c @ 6690:a1a42eefcc61

(main): Fix up MAIL_USE_MMDF conditional where the indesc open was moved inside a fork. Handle MAIL_USE_LOCKF. (MAIL_USE_SYSTEM_LOCK): New conditional macro. Define it if either MAIL_USE_FLOCK or MAIL_USE_LOCKF.
author Richard M. Stallman <rms@gnu.org>
date Tue, 05 Apr 1994 23:04:36 +0000
parents 9c86c71e879d
children 76f93e487956
comparison
equal deleted inserted replaced
6689:2efd8c4eb9be 6690:a1a42eefcc61
16 16
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to 18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ 19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 20
21 /* Important notice: defining MAIL_USE_FLOCK *will cause loss of mail* 21 /* Important notice: defining MAIL_USE_FLOCK or MAIL_USE_LOCKF *will
22 if you do it on a system that does not normally use flock as its way of 22 cause loss of mail* if you do it on a system that does not normally
23 interlocking access to inbox files. The setting of MAIL_USE_FLOCK 23 use flock as its way of interlocking access to inbox files. The
24 *must agree* with the system's own conventions. 24 setting of MAIL_USE_FLOCK and MAIL_USE_LOCKF *must agree* with the
25 It is not a choice that is up to you. 25 system's own conventions. It is not a choice that is up to you.
26 26
27 So, if your system uses lock files rather than flock, then the only way 27 So, if your system uses lock files rather than flock, then the only way
28 you can get proper operation is to enable movemail to write lockfiles there. 28 you can get proper operation is to enable movemail to write lockfiles there.
29 This means you must either give that directory access modes 29 This means you must either give that directory access modes
30 that permit everyone to write lockfiles in it, or you must make movemail 30 that permit everyone to write lockfiles in it, or you must make movemail
73 73
74 #ifdef XENIX 74 #ifdef XENIX
75 #include <sys/locking.h> 75 #include <sys/locking.h>
76 #endif 76 #endif
77 77
78 #ifdef MAIL_USE_LOCKF
79 #define MAIL_USE_SYSTEM_LOCK
80 #endif
81
82 #ifdef MAIL_USE_FLOCK
83 #define MAIL_USE_SYSTEM_LOCK
84 #endif
85
78 #ifdef MAIL_USE_MMDF 86 #ifdef MAIL_USE_MMDF
79 extern int lk_open (), lk_close (); 87 extern int lk_open (), lk_close ();
80 #endif 88 #endif
81 89
82 /* Cancel substitutions made by config.h for Emacs. */ 90 /* Cancel substitutions made by config.h for Emacs. */
101 char *inname, *outname; 109 char *inname, *outname;
102 int indesc, outdesc; 110 int indesc, outdesc;
103 int nread; 111 int nread;
104 WAITTYPE status; 112 WAITTYPE status;
105 113
106 #ifndef MAIL_USE_FLOCK 114 #ifndef MAIL_USE_SYSTEM_LOCK
107 struct stat st; 115 struct stat st;
108 long now; 116 long now;
109 int tem; 117 int tem;
110 char *lockname, *p; 118 char *lockname, *p;
111 char *tempname; 119 char *tempname;
112 int desc; 120 int desc;
113 #endif /* not MAIL_USE_FLOCK */ 121 #endif /* not MAIL_USE_SYSTEM_LOCK */
114 122
115 delete_lockname = 0; 123 delete_lockname = 0;
116 124
117 if (argc < 3) 125 if (argc < 3)
118 fatal ("two arguments required"); 126 fatal ("two arguments required");
162 /* Check access to input file. */ 170 /* Check access to input file. */
163 if (access (inname, R_OK | W_OK) != 0) 171 if (access (inname, R_OK | W_OK) != 0)
164 pfatal_with_name (inname); 172 pfatal_with_name (inname);
165 173
166 #ifndef MAIL_USE_MMDF 174 #ifndef MAIL_USE_MMDF
167 #ifndef MAIL_USE_FLOCK 175 #ifndef MAIL_USE_SYSTEM_LOCK
168 /* Use a lock file named /usr/spool/mail/$USER.lock: 176 /* Use a lock file named /usr/spool/mail/$USER.lock:
169 If it exists, the mail file is locked. */ 177 If it exists, the mail file is locked. */
170 /* Note: this locking mechanism is *required* by the mailer 178 /* Note: this locking mechanism is *required* by the mailer
171 (on systems which use it) to prevent loss of mail. 179 (on systems which use it) to prevent loss of mail.
172 180
181 189
182 You might also wish to verify that your system is one 190 You might also wish to verify that your system is one
183 which uses lock files for this purpose. Some systems use other methods. 191 which uses lock files for this purpose. Some systems use other methods.
184 192
185 If your system uses the `flock' system call for mail locking, 193 If your system uses the `flock' system call for mail locking,
186 define MAIL_USE_FLOCK in config.h or the s-*.h file 194 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
187 and recompile movemail. If the s- file for your system 195 and recompile movemail. If the s- file for your system
188 should define MAIL_USE_FLOCK but does not, send a bug report 196 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
189 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */ 197 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
190 198
191 lockname = concat (inname, ".lock", ""); 199 lockname = concat (inname, ".lock", "");
192 tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1); 200 tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
193 strcpy (tempname, inname); 201 strcpy (tempname, inname);
222 unlink (lockname); 230 unlink (lockname);
223 } 231 }
224 } 232 }
225 233
226 delete_lockname = lockname; 234 delete_lockname = lockname;
227 #endif /* not MAIL_USE_FLOCK */ 235 #endif /* not MAIL_USE_SYSTEM_LOCK */
236 #endif /* not MAIL_USE_MMDF */
228 237
229 if (fork () == 0) 238 if (fork () == 0)
230 { 239 {
231 seteuid (getuid ()); 240 seteuid (getuid ());
232 241
233 #ifdef MAIL_USE_FLOCK 242 #ifndef MAIL_USE_MMDF
243 #ifdef MAIL_USE_SYSTEM_LOCK
234 indesc = open (inname, O_RDWR); 244 indesc = open (inname, O_RDWR);
235 #else /* if not MAIL_USE_FLOCK */ 245 #else /* if not MAIL_USE_SYSTEM_LOCK */
236 indesc = open (inname, O_RDONLY); 246 indesc = open (inname, O_RDONLY);
237 #endif /* not MAIL_USE_FLOCK */ 247 #endif /* not MAIL_USE_SYSTEM_LOCK */
238 #else /* MAIL_USE_MMDF */ 248 #else /* MAIL_USE_MMDF */
239 indesc = lk_open (inname, O_RDONLY, 0, 0, 10); 249 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
240 #endif /* MAIL_USE_MMDF */ 250 #endif /* MAIL_USE_MMDF */
241 251
242 if (indesc < 0) 252 if (indesc < 0)
250 umask (umask (0) & 0333); 260 umask (umask (0) & 0333);
251 #endif /* BSD or Xenix */ 261 #endif /* BSD or Xenix */
252 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666); 262 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
253 if (outdesc < 0) 263 if (outdesc < 0)
254 pfatal_with_name (outname); 264 pfatal_with_name (outname);
255 #ifdef MAIL_USE_FLOCK 265 #ifdef MAIL_USE_SYSTEM_LOCK
266 #ifdef MAIL_USE_LOCKF
267 if (lockf (indesc, F_LOCK, 0) < 0) pfatal_with_name (inname);
268 #else /* not MAIL_USE_LOCKF */
256 #ifdef XENIX 269 #ifdef XENIX
257 if (locking (indesc, LK_RLCK, 0L) < 0) pfatal_with_name (inname); 270 if (locking (indesc, LK_RLCK, 0L) < 0) pfatal_with_name (inname);
258 #else 271 #else
259 if (flock (indesc, LOCK_EX) < 0) pfatal_with_name (inname); 272 if (flock (indesc, LOCK_EX) < 0) pfatal_with_name (inname);
260 #endif 273 #endif
261 #endif /* MAIL_USE_FLOCK */ 274 #endif /* not MAIL_USE_LOCKF */
275 #endif /* MAIL_USE_SYSTEM_LOCK */
262 276
263 { 277 {
264 char buf[1024]; 278 char buf[1024];
265 279
266 while (1) 280 while (1)
285 299
286 /* Check to make sure no errors before we zap the inbox. */ 300 /* Check to make sure no errors before we zap the inbox. */
287 if (close (outdesc) != 0) 301 if (close (outdesc) != 0)
288 pfatal_and_delete (outname); 302 pfatal_and_delete (outname);
289 303
290 #ifdef MAIL_USE_FLOCK 304 #ifdef MAIL_USE_SYSTEM_LOCK
291 #if defined (STRIDE) || defined (XENIX) 305 #if defined (STRIDE) || defined (XENIX)
292 /* Stride, xenix have file locking, but no ftruncate. This mess will do. */ 306 /* Stride, xenix have file locking, but no ftruncate. This mess will do. */
293 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666)); 307 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
294 #else 308 #else
295 ftruncate (indesc, 0L); 309 ftruncate (indesc, 0L);
296 #endif /* STRIDE or XENIX */ 310 #endif /* STRIDE or XENIX */
297 #endif /* MAIL_USE_FLOCK */ 311 #endif /* MAIL_USE_SYSTEM_LOCK */
298 312
299 #ifdef MAIL_USE_MMDF 313 #ifdef MAIL_USE_MMDF
300 lk_close (indesc, 0, 0, 0); 314 lk_close (indesc, 0, 0, 0);
301 #else 315 #else
302 close (indesc); 316 close (indesc);
303 #endif 317 #endif
304 318
305 #ifndef MAIL_USE_FLOCK 319 #ifndef MAIL_USE_SYSTEM_LOCK
306 /* Delete the input file; if we can't, at least get rid of its 320 /* Delete the input file; if we can't, at least get rid of its
307 contents. */ 321 contents. */
308 #ifdef MAIL_UNLINK_SPOOL 322 #ifdef MAIL_UNLINK_SPOOL
309 /* This is generally bad to do, because it destroys the permissions 323 /* This is generally bad to do, because it destroys the permissions
310 that were set on the file. Better to just empty the file. */ 324 that were set on the file. Better to just empty the file. */
311 if (unlink (inname) < 0 && errno != ENOENT) 325 if (unlink (inname) < 0 && errno != ENOENT)
312 #endif /* MAIL_UNLINK_SPOOL */ 326 #endif /* MAIL_UNLINK_SPOOL */
313 creat (inname, 0600); 327 creat (inname, 0600);
314 #endif /* not MAIL_USE_FLOCK */ 328 #endif /* not MAIL_USE_SYSTEM_LOCK */
315 329
316 exit (0); 330 exit (0);
317 } 331 }
318 332
319 wait (&status); 333 wait (&status);
320 if (!WIFEXITED (status)) 334 if (!WIFEXITED (status))
321 exit (1); 335 exit (1);
322 else if (WRETCODE (status) != 0) 336 else if (WRETCODE (status) != 0)
323 exit (WRETCODE (status)); 337 exit (WRETCODE (status));
324 338
325 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_FLOCK) 339 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
326 unlink (lockname); 340 unlink (lockname);
327 #endif /* not MAIL_USE_MMDF and not MAIL_USE_FLOCK */ 341 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
328 exit (0); 342 exit (0);
329 } 343 }
330 344
331 /* Print error message and exit. */ 345 /* Print error message and exit. */
332 346