xref: /illumos-gate/usr/src/cmd/ttymon/tmlock.c (revision 0ccfe5834c3b867c1b6c273cae02ec8922bc0fd2)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 #include	<unistd.h>
31 #include	<string.h>
32 #include	<sys/termios.h>
33 #include	"tmextern.h"
34 
35 /* -------------------------------------------------------- */
36 /* the follwing are here so we can use routines in ulockf.c */
37 int	Debug = 0;
38 char	*Bnptr;
39 /* dummies for using uucp .o routines */
40 /*VARARGS*/
41 /*ARGSUSED*/
42 void
43 assert(char *s1, char *s2, int i1, char *s3, int i2)
44 {
45 }
46 
47 void
48 cleanup(void)
49 {
50 }
51 
52 void
53 logent(char *s1 __unused, char *s2 __unused)
54 {
55 	/* so we can load ulockf() */
56 }
57 
58 /*
59  *	lastname	- If the path name starts with "/dev/",
60  *			  return the rest of the string.
61  *			- Otherwise, return the last token of the path name
62  */
63 char *
64 lastname(char *name)
65 {
66 	char	*sp, *p;
67 
68 	sp = name;
69 	if (strncmp(sp, "/dev/", 5) == 0)
70 		sp += 5;
71 	else
72 		while ((p = (char *)strchr(sp, '/')) != NULL) {
73 			sp = ++p;
74 		}
75 	return (sp);
76 }
77 
78 /*
79  *	tm_lock(fd)	- set advisory lock on the device
80  */
81 int
82 tm_lock(int fd)
83 {
84 	return (fd_mklock(fd));
85 }
86 
87 /*
88  *	tm_checklock	- check if advisory lock is on
89  */
90 int
91 tm_checklock(int fd)
92 {
93 	return (fd_cklock(fd));
94 }
95 
96 /*
97  * check_session(fd) - check if a session established on fd
98  *		       return 1 if session exists, otherwise, return 0.
99  *
100  */
101 int
102 check_session(int fd)
103 {
104 	pid_t	sid;
105 
106 	if (ioctl(fd, TIOCGSID, &sid) == -1)
107 		return (0);
108 	else if (sid == 0)
109 		return (0);
110 	else
111 		return (1);
112 }
113