1*41fbaed0Stron /* $NetBSD: open_lock.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $ */
2*41fbaed0Stron
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /* open_lock 3
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /* open or create file and lock it for exclusive access
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /* #include <open_lock.h>
10*41fbaed0Stron /*
11*41fbaed0Stron /* VSTREAM *open_lock(path, flags, mode, why)
12*41fbaed0Stron /* const char *path;
13*41fbaed0Stron /* int flags;
14*41fbaed0Stron /* mode_t mode;
15*41fbaed0Stron /* VSTRING *why;
16*41fbaed0Stron /* DESCRIPTION
17*41fbaed0Stron /* This module opens or creates the named file and attempts to
18*41fbaed0Stron /* acquire an exclusive lock. The lock is lost when the last
19*41fbaed0Stron /* process closes the file.
20*41fbaed0Stron /*
21*41fbaed0Stron /* Arguments:
22*41fbaed0Stron /* .IP "path, flags, mode"
23*41fbaed0Stron /* These are passed on to safe_open().
24*41fbaed0Stron /* .IP why
25*41fbaed0Stron /* storage for diagnostics.
26*41fbaed0Stron /* SEE ALSO
27*41fbaed0Stron /* safe_open(3) carefully open or create file
28*41fbaed0Stron /* myflock(3) get exclusive lock on file
29*41fbaed0Stron /* DIAGNOSTICS
30*41fbaed0Stron /* In case of problems the result is a null pointer and a problem
31*41fbaed0Stron /* description is returned via the global \fIerrno\fR variable.
32*41fbaed0Stron /* LICENSE
33*41fbaed0Stron /* .ad
34*41fbaed0Stron /* .fi
35*41fbaed0Stron /* The Secure Mailer license must be distributed with this software.
36*41fbaed0Stron /* AUTHOR(S)
37*41fbaed0Stron /* Wietse Venema
38*41fbaed0Stron /* IBM T.J. Watson Research
39*41fbaed0Stron /* P.O. Box 704
40*41fbaed0Stron /* Yorktown Heights, NY 10598, USA
41*41fbaed0Stron /*--*/
42*41fbaed0Stron
43*41fbaed0Stron /* System library. */
44*41fbaed0Stron
45*41fbaed0Stron #include <sys_defs.h>
46*41fbaed0Stron #include <unistd.h>
47*41fbaed0Stron #include <fcntl.h>
48*41fbaed0Stron
49*41fbaed0Stron /* Utility library. */
50*41fbaed0Stron
51*41fbaed0Stron #include <msg.h>
52*41fbaed0Stron #include <vstream.h>
53*41fbaed0Stron #include <vstring.h>
54*41fbaed0Stron #include <safe_open.h>
55*41fbaed0Stron #include <myflock.h>
56*41fbaed0Stron #include <open_lock.h>
57*41fbaed0Stron
58*41fbaed0Stron /* open_lock - open file and lock it for exclusive access */
59*41fbaed0Stron
open_lock(const char * path,int flags,mode_t mode,VSTRING * why)60*41fbaed0Stron VSTREAM *open_lock(const char *path, int flags, mode_t mode, VSTRING *why)
61*41fbaed0Stron {
62*41fbaed0Stron VSTREAM *fp;
63*41fbaed0Stron
64*41fbaed0Stron /*
65*41fbaed0Stron * Carefully create or open the file, and lock it down. Some systems
66*41fbaed0Stron * don't have the O_LOCK open() flag, or the flag does not do what we
67*41fbaed0Stron * want, so we roll our own lock.
68*41fbaed0Stron */
69*41fbaed0Stron if ((fp = safe_open(path, flags, mode, (struct stat *) 0, -1, -1, why)) == 0)
70*41fbaed0Stron return (0);
71*41fbaed0Stron if (myflock(vstream_fileno(fp), INTERNAL_LOCK,
72*41fbaed0Stron MYFLOCK_OP_EXCLUSIVE | MYFLOCK_OP_NOWAIT) < 0) {
73*41fbaed0Stron vstring_sprintf(why, "unable to set exclusive lock: %m");
74*41fbaed0Stron vstream_fclose(fp);
75*41fbaed0Stron return (0);
76*41fbaed0Stron }
77*41fbaed0Stron return (fp);
78*41fbaed0Stron }
79