xref: /illumos-gate/usr/src/cmd/ypcmd/yppasswd/yplckpwdf.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1996-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <stdio.h>
31 #include <signal.h>
32 #include <fcntl.h>
33 #include <unistd.h> /* alarm() */
34 
35 #define	S_WAITTIME	15
36 
37 static struct flock flock =	{
38 			0,	/* l_type */
39 			0,	/* l_whence */
40 			0,	/* l_start */
41 			0,	/* l_len */
42 			0,	/* l_sysid */
43 			0	/* l_pid */
44 			};
45 
46 /*
47  *	yplckpwdf() returns a 0 for a successful lock within W_WAITTIME
48  *	and -1 otherwise
49  */
50 
51 static int fildes = -1;
52 extern char lockfile[];
53 
54 /* ARGSUSED */
55 static void
56 almhdlr(int sig)
57 {
58 }
59 
60 int
61 yplckpwdf()
62 {
63 	int retval;
64 	if ((fildes = creat(lockfile, 0600)) == -1)
65 		return (-1);
66 
67 	flock.l_type = F_WRLCK;
68 	(void) sigset(SIGALRM, almhdlr);
69 	(void) alarm(S_WAITTIME);
70 	retval = fcntl(fildes, F_SETLKW, (int)&flock);
71 	(void) alarm(0);
72 	(void) sigset(SIGALRM, SIG_DFL);
73 	return (retval);
74 
75 }
76 
77 /*
78  *	ypulckpwdf() returns 0 for a successful unlock and -1 otherwise
79  */
80 int
81 ypulckpwdf()
82 {
83 	if (fildes == -1)
84 		return (-1);
85 
86 	flock.l_type = F_UNLCK;
87 	(void) fcntl(fildes, F_SETLK, (int)&flock);
88 	(void) close(fildes);
89 	fildes = -1;
90 	return (0);
91 
92 }
93