rev |
line source |
yuuji@0
|
1 /* ========================================================================
|
yuuji@0
|
2 * Copyright 1988-2006 University of Washington
|
yuuji@0
|
3 *
|
yuuji@0
|
4 * Licensed under the Apache License, Version 2.0 (the "License");
|
yuuji@0
|
5 * you may not use this file except in compliance with the License.
|
yuuji@0
|
6 * You may obtain a copy of the License at
|
yuuji@0
|
7 *
|
yuuji@0
|
8 * http://www.apache.org/licenses/LICENSE-2.0
|
yuuji@0
|
9 *
|
yuuji@0
|
10 *
|
yuuji@0
|
11 * ========================================================================
|
yuuji@0
|
12 */
|
yuuji@0
|
13
|
yuuji@0
|
14 /*
|
yuuji@0
|
15 * Program: Exclusive create of a file, NFS kludge version
|
yuuji@0
|
16 *
|
yuuji@0
|
17 * Author: Mark Crispin
|
yuuji@0
|
18 * Networks and Distributed Computing
|
yuuji@0
|
19 * Computing & Communications
|
yuuji@0
|
20 * University of Washington
|
yuuji@0
|
21 * Administration Building, AG-44
|
yuuji@0
|
22 * Seattle, WA 98195
|
yuuji@0
|
23 * Internet: MRC@CAC.Washington.EDU
|
yuuji@0
|
24 *
|
yuuji@0
|
25 * Date: 17 December 1999
|
yuuji@0
|
26 * Last Edited: 30 August 2006
|
yuuji@0
|
27 */
|
yuuji@0
|
28
|
yuuji@0
|
29
|
yuuji@0
|
30 /* SUN-OS had an NFS, As kludgy as an albatross;
|
yuuji@0
|
31 * And everywhere that it was installed, It was a total loss.
|
yuuji@0
|
32 * -- MRC 9/25/91
|
yuuji@0
|
33 */
|
yuuji@0
|
34
|
yuuji@0
|
35 /* Exclusive create of a file
|
yuuji@0
|
36 * Accepts: file name
|
yuuji@0
|
37 * Return: T if success, NIL if failed, -1 if retry
|
yuuji@0
|
38 */
|
yuuji@0
|
39
|
yuuji@0
|
40 long crexcl (char *name)
|
yuuji@0
|
41 {
|
yuuji@0
|
42 long ret = -1;
|
yuuji@0
|
43 int i;
|
yuuji@0
|
44 char hitch[MAILTMPLEN];
|
yuuji@0
|
45 struct stat sb;
|
yuuji@0
|
46 int mask = umask (0);
|
yuuji@0
|
47 /* build hitching post file name */
|
yuuji@0
|
48 sprintf (hitch,"%s.%lu.%d.",name,(unsigned long) time (0),getpid ());
|
yuuji@0
|
49 i = strlen (hitch); /* append local host name */
|
yuuji@0
|
50 gethostname (hitch + i,(MAILTMPLEN - i) - 1);
|
yuuji@0
|
51 /* try to get hitching-post file */
|
yuuji@0
|
52 if ((i = open (hitch,O_WRONLY|O_CREAT|O_EXCL,(int) shlock_mode)) >= 0) {
|
yuuji@0
|
53 close (i); /* close the hitching-post */
|
yuuji@0
|
54 /* Note: link() may return an error even if it actually succeeded. So we
|
yuuji@0
|
55 * always check for success via the link count, and ignore the error if
|
yuuji@0
|
56 * the link count is right.
|
yuuji@0
|
57 */
|
yuuji@0
|
58 /* tie hitching-post to lock */
|
yuuji@0
|
59 i = link (hitch,name) ? errno : 0;
|
yuuji@0
|
60 /* success if link count now 2 */
|
yuuji@0
|
61 if (!stat (hitch,&sb) && (sb.st_nlink == 2)) ret = LONGT;
|
yuuji@0
|
62 else if (i == EPERM) { /* links not allowed? */
|
yuuji@0
|
63 /* Probably a FAT filesystem on Linux. It can't be NFS, so try creating
|
yuuji@0
|
64 * the lock file directly.
|
yuuji@0
|
65 */
|
yuuji@0
|
66 if ((i = open (name,O_WRONLY|O_CREAT|O_EXCL,(int) shlock_mode)) >= 0){
|
yuuji@0
|
67 close (i); /* close the file */
|
yuuji@0
|
68 ret = LONGT; /* success */
|
yuuji@0
|
69 }
|
yuuji@0
|
70 /* fail unless error is EEXIST */
|
yuuji@0
|
71 else if (errno != EEXIST) ret = NIL;
|
yuuji@0
|
72 }
|
yuuji@0
|
73 unlink (hitch); /* flush hitching post */
|
yuuji@0
|
74 }
|
yuuji@0
|
75 /* fail unless error is EEXIST */
|
yuuji@0
|
76 else if (errno != EEXIST) ret = NIL;
|
yuuji@0
|
77 umask (mask); /* restore previous mask */
|
yuuji@0
|
78 return ret;
|
yuuji@0
|
79 }
|