xref: /onnv-gate/usr/src/lib/libc/port/gen/ttyslot.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216737Sraf 
220Sstevel@tonic-gate /*
236737Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*6812Sraf 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Return the number of the slot in the utmp file
340Sstevel@tonic-gate  * corresponding to the current user: try for file 0, 1, 2.
350Sstevel@tonic-gate  * Returns -1 if slot not found.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
38*6812Sraf #include "lint.h"
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <utmpx.h>
450Sstevel@tonic-gate #include <stdlib.h>
466737Sraf #include <pthread.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #ifndef TRUE
490Sstevel@tonic-gate #define	TRUE 1
500Sstevel@tonic-gate #define	FALSE 0
510Sstevel@tonic-gate #endif
520Sstevel@tonic-gate 
530Sstevel@tonic-gate int
ttyslot(void)540Sstevel@tonic-gate ttyslot(void)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	struct futmpx ubuf;
576737Sraf 	char *p;
580Sstevel@tonic-gate 	int s;
596737Sraf 	int ret = -1;
606737Sraf 	int console = FALSE;
610Sstevel@tonic-gate 	char ttynm[128];
626737Sraf 	FILE *fp;
636737Sraf 	int cancel_state;
640Sstevel@tonic-gate 
656737Sraf 	/*
666737Sraf 	 * The UNIX98 Posix conformance test suite requires
676737Sraf 	 * ttyslot() to not be a cancellation point.
686737Sraf 	 */
696737Sraf 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
700Sstevel@tonic-gate 
716737Sraf 	if ((p = ttyname_r(0, ttynm, 128)) != NULL ||
726737Sraf 	    (p = ttyname_r(1, ttynm, 128)) != NULL ||
736737Sraf 	    (p = ttyname_r(2, ttynm, 128)) != NULL) {
746737Sraf 		if (strncmp(p, "/dev/", 5) == 0)
756737Sraf 			p += 5;
766737Sraf 		if (strcmp(p, "console") == 0)
776737Sraf 			console = TRUE;
786737Sraf 		s = 0;
796737Sraf 		if ((fp = fopen(UTMPX_FILE, "rF")) != NULL) {
806737Sraf 			while ((fread(&ubuf, sizeof (ubuf), 1, fp)) == 1) {
816737Sraf 				if ((ubuf.ut_type == INIT_PROCESS ||
826737Sraf 				    ubuf.ut_type == LOGIN_PROCESS ||
836737Sraf 				    ubuf.ut_type == USER_PROCESS) &&
846737Sraf 				    strncmp(p, ubuf.ut_line,
856737Sraf 				    sizeof (ubuf.ut_line)) == 0) {
866737Sraf 					ret = s;
876737Sraf 					if (!console ||
886737Sraf 					    strncmp(ubuf.ut_host, ":0", 2) == 0)
896737Sraf 						break;
906737Sraf 				}
916737Sraf 				s++;
926737Sraf 			}
936737Sraf 			(void) fclose(fp);
946737Sraf 		}
956737Sraf 	}
960Sstevel@tonic-gate 
976737Sraf 	(void) pthread_setcancelstate(cancel_state, NULL);
980Sstevel@tonic-gate 	return (ret);
990Sstevel@tonic-gate }
100