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