summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/libunix/lock.c
blob: 1be6fbc7ba06c1300be56e9a8fb5263fea2f3c69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* lock.c
   Lock and unlock a file name.

   Copyright (C) 1991, 1992, 1993, 1995 Ian Lance Taylor

   This file is part of the Taylor UUCP package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
   */

#include "uucp.h"

#if USE_RCS_ID
const char lock_rcsid[] = "$Id: lock.c,v 1.20 1995/06/21 19:19:38 ian Rel $";
#endif

#include "uudefs.h"
#include "sysdep.h"
#include "system.h"

#include <errno.h>
#include <ctype.h>

#if HAVE_FCNTL_H
#include <fcntl.h>
#else
#if HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#endif

#if TM_IN_SYS_TIME
#include <sys/time.h>
#else
#include <time.h>
#endif

#if HAVE_QNX_LOCKFILES
#include <sys/kernel.h>
#include <sys/psinfo.h>
#include <sys/seginfo.h>
#include <sys/vc.h>
#endif

#ifndef O_RDONLY
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#endif

#ifndef O_NOCTTY
#define O_NOCTTY 0
#endif

#ifndef SEEK_SET
#define SEEK_SET 0
#endif

#ifndef localtime
extern struct tm *localtime ();
#endif

#if HAVE_QNX_LOCKFILES
static boolean fsqnx_stale P((unsigned long ipid, unsigned long inme,
			     unsigned long inid, boolean *pferr));
#endif

/* Lock something.  If the fspooldir argument is TRUE, the argument is
   a file name relative to the spool directory; otherwise the argument
   is a simple file name which should be created in the system lock
   directory (under HDB this is /etc/locks).  */

boolean
fsdo_lock (zlock, fspooldir, pferr)
     const char *zlock;
     boolean fspooldir;
     boolean *pferr;
{
  char *zfree;
  const char *zpath, *zslash;
  size_t cslash;
  pid_t ime;
  char *ztempfile;
  char abtempfile[sizeof "TMP12345678901234567890"];
  int o;
#if HAVE_QNX_LOCKFILES
  nid_t inme;
  char ab[23];
  char *zend;
#else
#if HAVE_V2_LOCKFILES
  int i;
#else
  char ab[12];
#endif
#endif
  int cwrote;
  const char *zerr;
  boolean fret;

  if (pferr != NULL)
    *pferr = TRUE;

  if (fspooldir)
    {
      zfree = NULL;
      zpath = zlock;
    }
  else
    {
      zfree = zsysdep_in_dir (zSlockdir, zlock);
      zpath = zfree;
    }

  ime = getpid ();
#if HAVE_QNX_LOCKFILES
  inme = getnid ();
#endif

  /* We do the actual lock by creating a file and then linking it to
     the final file name we want.  This avoids race conditions due to
     one process checking the file before we have finished writing it,
     and also works even if we are somehow running as root.

     First, create the file in the right directory (we must create the
     file in the same directory since otherwise we might attempt a
     cross-device link).  */
  zslash = strrchr (zpath, '/');
  if (zslash == NULL)
    cslash = 0;
  else
    cslash = zslash - zpath + 1;

#if HAVE_QNX_LOCKFILES
  sprintf (abtempfile, "TMP%010lx%010lx", (unsigned long) ime,
	   (unsigned long) inme);
#else
  sprintf (abtempfile, "TMP%010lx", (unsigned long) ime);
#endif
  ztempfile = zbufalc (cslash + sizeof abtempfile);
  memcpy (ztempfile, zpath, cslash);
  memcpy (ztempfile + cslash, abtempfile, sizeof abtempfile);

  o = creat (ztempfile, IPUBLIC_FILE_MODE);
  if (o < 0)
    {
      if (errno == ENOENT)
	{
	  if (! fsysdep_make_dirs (ztempfile, FALSE))
	    {
	      ubuffree (zfree);
	      ubuffree (ztempfile);
	      return FALSE;
	    }
	  o = creat (ztempfile, IPUBLIC_FILE_MODE);
	}
      if (o < 0)
	{
	  ulog (LOG_ERROR, "creat (%s): %s", ztempfile, strerror (errno));
	  ubuffree (zfree);
	  ubuffree (ztempfile);
	  return FALSE;
	}
    }

#if HAVE_QNX_LOCKFILES
  sprintf (ab, "%10ld %10ld\n", (long) ime, (long) inme);
  cwrote = write (o, ab, strlen (ab));
#else
#if HAVE_V2_LOCKFILES
  i = (int) ime;
  cwrote = write (o, &i, sizeof i);
#else
  sprintf (ab, "%10ld\n", (long) ime);
  cwrote = write (o, ab, strlen (ab));
#endif
#endif

  zerr = NULL;
  if (cwrote < 0)
    zerr = "write";
  if (close (o) < 0)
    zerr = "close";
  if (zerr != NULL)
    {
      ulog (LOG_ERROR, "%s (%s): %s", zerr, ztempfile, strerror (errno));
      (void) remove (ztempfile);
      ubuffree (zfree);
      ubuffree (ztempfile);
      return FALSE;
    }

  /* Now try to link the file we just created to the lock file that we
     want.  If it fails, try reading the existing file to make sure
     the process that created it still exists.  We do this in a loop
     to make it easy to retry if the old locking process no longer
     exists.  */
  fret = TRUE;
  if (pferr != NULL)
    *pferr = FALSE;
  o = -1;
  zerr = NULL;

  while (link (ztempfile, zpath) != 0)
    {
      int cgot;
      pid_t ipid;
      boolean freadonly;
      struct stat st;
      char abtime[sizeof "1991-12-31 12:00:00"];
#if HAVE_QNX_LOCKFILES
      nid_t inid;
#endif

      fret = FALSE;

      if (errno != EEXIST)
	{
	  ulog (LOG_ERROR, "link (%s, %s): %s", ztempfile, zpath,
		strerror (errno));
	  if (pferr != NULL)
	    *pferr = TRUE;
	  break;
	}

      freadonly = FALSE;
      o = open ((char *) zpath, O_RDWR | O_NOCTTY, 0);
      if (o < 0)
	{
	  if (errno == EACCES)
	    {
	      freadonly = TRUE;
	      o = open ((char *) zpath, O_RDONLY, 0);
	    }
	  if (o < 0)
	    {
	      if (errno == ENOENT)
		{
		  /* The file was presumably removed between the link
		     and the open.  Try the link again.  */
		  fret = TRUE;
		  continue;
		}
	      zerr = "open";
	      break;
	    }
	}

      /* The race starts here.  See below for a discussion.  */

#if HAVE_V2_LOCKFILES
      cgot = read (o, &i, sizeof i);
#else
      cgot = read (o, ab, sizeof ab - 1);
#endif

      if (cgot < 0)
	{
	  zerr = "read";
	  break;
	}

#if DEBUG > 0
#if HAVE_V2_LOCKFILES
      {
	char ab[10];

	if (read (o, ab, sizeof ab) > 4
	    && isdigit (BUCHAR (ab[0])))
	  ulog (LOG_ERROR,
		"Lock file %s may be HDB format; check LOCKFILES in policy.h",
		zpath);
      }
#else
      if (cgot == 4)
	ulog (LOG_ERROR,
	      "Lock file %s may be V2 format; check LOCKFILES in policy.h",
	      zpath);
#endif
#endif /* DEBUG > 0 */

#if HAVE_QNX_LOCKFILES
      ab[cgot] = '\0';
      ipid = (pid_t) strtol (ab, &zend, 10);
      inid = (nid_t) strtol (zend, (char **) NULL, 10);
#else
#if HAVE_V2_LOCKFILES
      ipid = (pid_t) i;
#else
      ab[cgot] = '\0';
      ipid = (pid_t) strtol (ab, (char **) NULL, 10);
#endif
#endif

      /* On NFS, the link might have actually succeeded even though we
	 got a failure return.  This can happen if the original
	 acknowledgement was lost or delayed and the operation was
	 retried.  In this case the pid will be our own.  This
	 introduces a rather improbable race condition: if a stale
	 lock was left with our process ID in it, and another process
	 just did the kill, below, but has not yet changed the lock
	 file to hold its own process ID, we could start up and make
	 it all the way to here and think we have the lock.  I'm not
	 going to worry about this possibility.  */
      if (ipid == ime)
	{
#if HAVE_QNX_LOCKFILES
	  if (inid == inme)
#endif
	    {
	      fret = TRUE;
	      break;
	    }
	}

      /* If the lock file is empty (cgot == 0), we assume that it is
         stale.  This can happen if the system crashed after the lock
         file was created but before the process ID was written out.  */
      if (cgot > 0)
	{
#if HAVE_QNX_LOCKFILES
	  if (! fsqnx_stale ((unsigned long) ipid, (unsigned long) inme,
			     (unsigned long) inid, pferr))
	    break;
#else
	  /* If the process still exists, we will get EPERM rather
	     than ESRCH.  We then return FALSE to indicate that we
	     cannot make the lock.  */
	  if (kill (ipid, 0) == 0 || errno == EPERM)
	    break;
#endif
	}

      if (fstat (o, &st) < 0)
	strcpy (abtime, "unknown");
      else
	{
	  time_t itm;
	  struct tm *q;

	  itm = (time_t) st.st_mtime;
	  q = localtime (&itm);
	  sprintf (abtime, "%04d-%02d-%02d %02d:%02d:%02d",
		   q->tm_year + 1900, q->tm_mon + 1, q->tm_mday, q->tm_hour,
		   q->tm_min, q->tm_sec);
	}

#if HAVE_QNX_LOCKFILES
      ulog (LOG_ERROR,
	    "Stale lock %s held by process %ld on node %ld created %s",
	    zpath, (long) ipid, (long) inid, abtime);
#else
      ulog (LOG_ERROR, "Stale lock %s held by process %ld created %s",
	    zpath, (long) ipid, abtime);
#endif

      /* This is a stale lock, created by a process that no longer
	 exists.

	 Now we could remove the file (and, if the file mode disallows
	 writing, that's what we have to do), but we try to avoid
	 doing so since it causes a race condition.  If we remove the
	 file, and are interrupted any time after we do the read until
	 we do the remove, another process could get in, open the
	 file, find that it was a stale lock, remove the file and
	 create a new one.  When we regained control we would remove
	 the file the other process just created.

	 These files are being generated partially for the benefit of
	 cu, and it would be nice to avoid the race however cu avoids
	 it, so that the programs remain compatible.  Unfortunately,
	 nobody seems to know how cu avoids the race, or even if it
	 tries to avoid it at all.

	 There are a few ways to avoid the race.  We could use kernel
	 locking primitives, but they may not be available.  We could
	 link to a special file name, but if that file were left lying
	 around then no stale lock could ever be broken (Henry Spencer
	 would think this was a good thing).

	 Instead I've implemented the following procedure: seek to the
	 start of the file, write our pid into it, sleep for five
	 seconds, and then make sure our pid is still there.  Anybody
	 who checks the file while we're asleep will find our pid
	 there and fail the lock.  The only race will come from
	 another process which has done the read by the time we do our
	 write.  That process will then have five seconds to do its
	 own write.  When we wake up, we'll notice that our pid is no
	 longer in the file, and retry the lock from the beginning.

	 This relies on the atomicity of write(2).  If it possible for
	 the writes of two processes to be interleaved, the two
	 processes could livelock.  POSIX unfortunately leaves this
	 case explicitly undefined; however, given that the write is
	 of less than a disk block, it's difficult to imagine an
	 interleave occurring.

	 Note that this is still a race.  If it takes the second
	 process more than five seconds to do the kill, the lseek, and
	 the write, both processes will think they have the lock.
	 Perhaps the length of time to sleep should be configurable.
	 Even better, perhaps I should add a configuration option to
	 use a permanent lock file, which eliminates any race and
	 forces the installer to be aware of the existence of the
	 permanent lock file.

	 We stat the file after the sleep, to make sure some other
	 program hasn't deleted it for us.  */
      if (freadonly)
	{
	  (void) close (o);
	  o = -1;
	  (void) remove (zpath);
	  fret = TRUE;
	  continue;
	}

      if (lseek (o, (off_t) 0, SEEK_SET) != 0)
	{
	  zerr = "lseek";
	  break;
	}

#if HAVE_QNX_LOCKFILES
      sprintf (ab, "%10ld %10ld\n", (long) ime, (long) inme);
      cwrote = write (o, ab, strlen (ab));
#else
#if HAVE_V2_LOCKFILES
      i = (int) ime;
      cwrote = write (o, &i, sizeof i);
#else
      sprintf (ab, "%10ld\n", (long) ime);
      cwrote = write (o, ab, strlen (ab));
#endif
#endif

      if (cwrote < 0)
	{
	  zerr = "write";
	  break;
	}

      (void) sleep (5);

      if (lseek (o, (off_t) 0, SEEK_SET) != 0)
	{
	  zerr = "lseek";
	  break;
	}

#if HAVE_V2_LOCKFILES
      cgot = read (o, &i, sizeof i);
#else
      cgot = read (o, ab, sizeof ab - 1);
#endif

      if (cgot < 0)
	{
	  zerr = "read";
	  break;
	}

#if HAVE_QNX_LOCKFILES
      ab[cgot] = '\0';
      ipid = (pid_t) strtol (ab, &zend, 10);
      inid = (nid_t) strtol (zend, (char **) NULL, 10);
#else
#if HAVE_V2_LOCKFILES
      ipid = (pid_t) i;
#else
      ab[cgot] = '\0';
      ipid = (pid_t) strtol (ab, (char **) NULL, 10);
#endif
#endif

      if (ipid == ime)
	{
#if HAVE_QNX_LOCKFILES
	  if (inid == inme)
#endif
	    {
	      struct stat sfile, sdescriptor;

	      /* It looks like we have the lock.  Do the final stat
		 check.  */
	      if (stat ((char *) zpath, &sfile) < 0)
		{
		  if (errno != ENOENT)
		    {
		      zerr = "stat";
		      break;
		    }
		  /* Loop around and try again.  */
		}
	      else
		{
		  if (fstat (o, &sdescriptor) < 0)
		    {
		      zerr = "fstat";
		      break;
		    }

		  if (sfile.st_ino == sdescriptor.st_ino
		      && sfile.st_dev == sdescriptor.st_dev)
		    {
		      /* Close the file before assuming we've
			 succeeded to pick up any trailing errors.  */
		      if (close (o) < 0)
			{
			  zerr = "close";
			  break;
			}

		      o = -1;

		      /* We have the lock.  */
		      fret = TRUE;
		      break;
		    }
		}
	    }
	}

      /* Loop around and try the lock again.  We keep doing this until
	 the lock file holds a pid that exists.  */
      (void) close (o);
      o = -1;
      fret = TRUE;
    }

  if (zerr != NULL)
    {
      ulog (LOG_ERROR, "%s (%s): %s", zerr, zpath, strerror (errno));
      if (pferr != NULL)
	*pferr = TRUE;
    }

  if (o >= 0)
    (void) close (o);

  ubuffree (zfree);

  /* It would be nice if we could leave the temporary file around for
     future calls, but considering that we create lock files in
     various different directories it's probably more trouble than
     it's worth.  */
  if (remove (ztempfile) != 0)
    ulog (LOG_ERROR, "remove (%s): %s", ztempfile, strerror (errno));

  ubuffree (ztempfile);

  return fret;
}

/* Unlock something.  The fspooldir argument is as in fsdo_lock.  */

boolean
fsdo_unlock (zlock, fspooldir)
     const char *zlock;
     boolean fspooldir;
{
  char *zfree;
  const char *zpath;

  if (fspooldir)
    {
      zfree = NULL;
      zpath = zlock;
    }
  else
    {
      zfree = zsysdep_in_dir (zSlockdir, zlock);
      zpath = zfree;
    }

  if (remove (zpath) == 0
      || errno == ENOENT)
    {
      ubuffree (zfree);
      return TRUE;
    }
  else
    {
      ulog (LOG_ERROR, "remove (%s): %s", zpath, strerror (errno));
      ubuffree (zfree);
      return FALSE;
    }
}

#if HAVE_QNX_LOCKFILES

/* Return TRUE if the lock is stale.  */

static boolean
fsqnx_stale (ipid, inme, inid, pferr)
     unsigned long ipid;
     unsigned long inme;
     unsigned long inid;
     boolean *pferr;
{
  /* A virtual process ID.  This virtual process ID, which will exist
     on the local node, will represent the process ID of the process
     manager process (Proc) on the remote node. */
  pid_t ivid;
  /* The return value of the qnx_psinfo function.  This is either a
     process ID which might or might not be the same as the process
     being looked for, or -1 to indicate no process found. */
  pid_t ifound_pid;
  /* This holds the actual result of qnx_psinfo.  We will ignore
     almost all the fields since we're just checking for existence. */
  struct _psinfo spsdata;

  /* Establish connection with a remote process manager if necessary. */
  if (inid != inme)
    {
      ivid = qnx_vc_attach (inid /* remote node ID */,
			    PROC_PID /* pid of process manager */,
			    1000 /* initial buffer size */,
			    0	/* flags */);
      if (ivid < 0)
	{
	  ulog (LOG_ERROR, "qnx_vc_attach (%lu, PROC_PID): %s",
		inid, strerror (errno));
	  if (pferr != NULL)
	    *pferr = TRUE;
	  return FALSE;
	}
    }
  else
    {
      /* Use the local pid of the local process manager. */
      ivid = PROC_PID;
    }
        
  /* Request the process information. */
  ifound_pid = qnx_psinfo (ivid /* process manager handling request */,
			   ipid /* get info on this process */,
			   &spsdata /* put info in this struct */,
			   0	/* unused */,
			   (struct _seginfo *) NULL /* unused */);

  /* Deallocate the virtual connection before continuing. */
  {
    int isaved_errno = errno;
    if (qnx_vc_detach (ivid) < 0)
      ulog (LOG_ERROR, "qnx_vd_detach (%ld): %s", (long) ivid,
	    strerror (errno));
    errno = isaved_errno;
  }
          
  /* If the returned pid matches then the process still holds the lock. */
  if ((ifound_pid == ipid) && (spsdata.pid == ipid))
    return FALSE;

  /* If the returned pid is positive and doesn't match, then the
     process doesn't exist and the lock is stale.  Continue. */

  /* If the returned pid is negative (-1) and errno is EINVAL (or ESRCH
     in older versions of QNX), then the process doesn't exist and the
     lock is stale.  Continue. */

  /* Check for impossible errors. */
  if ((ifound_pid < 0) && (errno != ESRCH) && (errno != EINVAL))
    {
      ulog (LOG_ERROR, "qnx_psinfo (%ld, %ld): %s", (long) ivid,
	    (long) ipid, strerror (errno));
      /* Since we don't know what the hell this means, and we don't
	 want our system to freeze, we treat this case as a stale
	 lock.  Continue on. */
    }

  return TRUE;
}

#endif /* HAVE_QNX_LOCKFILES */
OpenPOWER on IntegriCloud