]> git.sesse.net Git - rdpsrv/blob - Xserver/lib/Xau/AuLock.c
Support RDP5 logon packets.
[rdpsrv] / Xserver / lib / Xau / AuLock.c
1 /* $XConsortium: AuLock.c,v 1.15 94/04/17 20:15:43 rws Exp $ */
2 /* $XFree86: xc/lib/Xau/AuLock.c,v 3.0 1994/10/20 06:04:31 dawes Exp $ */
3
4 /*
5
6 Copyright (c) 1988  X Consortium
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
21 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 Except as contained in this notice, the name of the X Consortium shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings
27 in this Software without prior written authorization from the X Consortium.
28
29 */
30
31 #include <X11/Xauth.h>
32 #include <X11/Xos.h>
33 #include <sys/stat.h>
34 #include <errno.h>
35 #if defined(X_NOT_STDC_ENV)
36 extern int errno;
37 #define Time_t long
38 extern Time_t time ();
39 #else
40 #include <time.h>
41 #define Time_t time_t
42 #endif
43 #ifndef X_NOT_POSIX
44 #include <unistd.h>
45 #else
46 #ifndef WIN32
47 extern unsigned sleep ();
48 #else
49 #define link rename
50 #endif
51 #endif
52 #ifdef __EMX__
53 #define link rename
54 #endif
55
56 #if NeedFunctionPrototypes
57 int
58 XauLockAuth (
59 _Xconst char *file_name,
60 int     retries,
61 int     timeout,
62 long    dead)
63 #else
64 int
65 XauLockAuth (file_name, retries, timeout, dead)
66 char    *file_name;
67 int     retries;
68 int     timeout;
69 long    dead;
70 #endif
71 {
72     char        creat_name[1025], link_name[1025];
73     struct stat statb;
74     Time_t      now;
75     int         creat_fd = -1;
76
77     if (strlen (file_name) > 1022)
78         return LOCK_ERROR;
79     (void) strcpy (creat_name, file_name);
80     (void) strcat (creat_name, "-c");
81     (void) strcpy (link_name, file_name);
82     (void) strcat (link_name, "-l");
83     if (stat (creat_name, &statb) != -1) {
84         now = time ((Time_t *) 0);
85         /*
86          * NFS may cause ctime to be before now, special
87          * case a 0 deadtime to force lock removal
88          */
89         if (dead == 0 || now - statb.st_ctime > dead) {
90             (void) unlink (creat_name);
91             (void) unlink (link_name);
92         }
93     }
94     
95     while (retries > 0) {
96         if (creat_fd == -1) {
97             creat_fd = creat (creat_name, 0666);
98             if (creat_fd == -1) {
99                 if (errno != EACCES)
100                     return LOCK_ERROR;
101             } else
102                 (void) close (creat_fd);
103         }
104         if (creat_fd != -1) {
105             if (link (creat_name, link_name) != -1)
106                 return LOCK_SUCCESS;
107             if (errno == ENOENT) {
108                 creat_fd = -1;  /* force re-creat next time around */
109                 continue;
110             }
111             if (errno != EEXIST)
112                 return LOCK_ERROR;
113         }
114         (void) sleep ((unsigned) timeout);
115         --retries;
116     }
117     return LOCK_TIMEOUT;
118 }