1/* $Xorg: lock.c,v 1.4 2001/02/09 02:05:59 xorgcvs Exp $ */ 2/****************************************************************************** 3 4Copyright 1994, 1998 The Open Group 5 6Permission to use, copy, modify, distribute, and sell this software and its 7documentation for any purpose is hereby granted without fee, provided that 8the above copyright notice appear in all copies and that both that 9copyright notice and this permission notice appear in supporting 10documentation. 11 12The above copyright notice and this permission notice shall be included in 13all copies or substantial portions of the Software. 14 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22Except as contained in this notice, the name of The Open Group shall not be 23used in advertising or otherwise to promote the sale, use or other dealings 24in this Software without prior written authorization from The Open Group. 25******************************************************************************/ 26/* $XFree86: xc/programs/xsm/lock.c,v 3.4 2001/12/14 20:02:25 dawes Exp $ */ 27 28#include "xsm.h" 29#include "lock.h" 30#include "choose.h" 31#include <sys/types.h> 32 33 34static const char * 35GetPath(void) 36{ 37 const char *path = getenv ("SM_SAVE_DIR"); 38 39 if (!path) 40 { 41 path = getenv ("HOME"); 42 if (!path) 43 path = "."; 44 } 45 46 return (path); 47} 48 49 50Status 51LockSession(const char *session_name, Bool write_id) 52{ 53 const char *path; 54 char lock_file[PATH_MAX]; 55 char temp_lock_file[PATH_MAX]; 56 Status status; 57 int fd; 58 59 path = GetPath (); 60 61 snprintf (lock_file, sizeof(lock_file), "%s/.XSMlock-%s", 62 path, session_name); 63 snprintf (temp_lock_file, sizeof(temp_lock_file), "%s/.XSMtlock-%s", 64 path, session_name); 65 66 if ((fd = creat (temp_lock_file, 0444)) < 0) 67 return (0); 68 69 if ((write_id && 70 (write (fd, networkIds, strlen (networkIds)) != strlen (networkIds))) || 71 (write (fd, "\n", 1) != 1)) 72 { 73 close (fd); 74 return (0); 75 } 76 77 close (fd); 78 79 status = 1; 80 81 if (link (temp_lock_file, lock_file) < 0) 82 status = 0; 83 84 if (remove (temp_lock_file) < 0) 85 status = 0; 86 87 return (status); 88} 89 90 91void 92UnlockSession(const char *session_name) 93{ 94 const char *path; 95 char lock_file[PATH_MAX]; 96 97 path = GetPath (); 98 99 snprintf (lock_file, sizeof(lock_file), "%s/.XSMlock-%s", 100 path, session_name); 101 102 remove (lock_file); 103} 104 105 106char * 107GetLockId(const char *session_name) 108{ 109 const char *path; 110 FILE *fp; 111 char lock_file[PATH_MAX]; 112 char buf[256]; 113 char *ret; 114 115 path = GetPath (); 116 117 snprintf (lock_file, sizeof(lock_file), "%s/.XSMlock-%s", 118 path, session_name); 119 120 if ((fp = fopen (lock_file, "r")) == NULL) 121 { 122 return (NULL); 123 } 124 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC); 125 126 buf[0] = '\0'; 127 fscanf (fp, "%255s\n", buf); 128 ret = XtNewString (buf); 129 130 fclose (fp); 131 132 return (ret); 133} 134 135 136Bool 137CheckSessionLocked(const char *session_name, Bool get_id, char **id_ret) 138{ 139 if (get_id) 140 *id_ret = GetLockId (session_name); 141 142 if (!LockSession (session_name, False)) 143 return (1); 144 145 UnlockSession (session_name); 146 return (0); 147} 148 149 150void 151UnableToLockSession(const char *session_name) 152{ 153 /* 154 * We should popup a dialog here giving error. 155 */ 156 157#ifdef XKB 158 XkbStdBell(XtDisplay(topLevel),XtWindow(topLevel),0,XkbBI_Failure); 159#else 160 XBell (XtDisplay (topLevel), 0); 161#endif 162 sleep (2); 163 164 ChooseSession (); 165} 166