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
|
/*
* SCCS: @(#)lockfile.c 1.2 (97/07/21)
*
* UniSoft Ltd., London, England
*
* (C) Copyright 1996 X/Open Company Limited
*
* All rights reserved. No part of this source code may be reproduced,
* stored in a retrieval system, or transmitted, in any form or by any
* means, electronic, mechanical, photocopying, recording or otherwise,
* except as stated in the end-user licence agreement, without the prior
* permission of the copyright owners.
* A copy of the end-user licence agreement is contained in the file
* Licence which accompanies this distribution.
*
* X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
* the UK and other countries.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/************************************************************************
SCCS: @(#)lockfile.c 1.2 97/07/21 TETware release 3.3
NAME: lockfile.c
PRODUCT: TETware
AUTHOR: Andrew Dingwall, UniSoft Ltd.
DATE CREATED: October 1996
DESCRIPTION:
tcc action function - create an exclusive lock
this function moved from tccd/stcc.c to here
MODIFICATIONS:
Aaron Plattner, April 2010
Fixed warnings when compiled with GCC's -Wall option.
************************************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <errno.h>
# include <unistd.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "error.h"
#include "dtetlib.h"
#include "tcclib.h"
/* lock file mode */
#define MODEANY \
((mode_t) (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH))
/*
** tcf_lockfile() - create a lock file (exclusive lock)
**
** return ER_OK if successful or other ER_* code on error
**
** a zero timeout means return immediately if the operation fails
** a -ve timeout means try indefinately until a lock is obtained or an
** error occurs
*/
int tcf_lockfile(fname, timeout)
char *fname;
int timeout;
{
register int fd, errsave, rc;
register time_t start;
ASSERT(fname && *fname);
/* create the lock file, sleeping a bit if it fails */
start = time((time_t *) 0);
while ((fd = OPEN(fname, O_RDONLY | O_CREAT | O_EXCL, MODEANY)) < 0) {
if ((errsave = errno) != EEXIST || !timeout)
break;
if (timeout > 0 && time((time_t *) 0) > start + timeout) {
errno = 0;
return(ER_TIMEDOUT);
}
(void) SLEEP(2);
}
/* handle unexpected errors */
if (fd < 0) {
if ((rc = tet_maperrno(errsave)) == ER_ERR)
error(errsave, "can't create", fname);
errno = errsave;
return(rc);
}
/* all ok so close the file and return success */
(void) CLOSE(fd);
return(ER_OK);
}
|