xref: /illumos-gate/usr/src/lib/libc/port/gen/ttyslot.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ec6bbcf8Sraf 
227c478bd9Sstevel@tonic-gate /*
23ec6bbcf8Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Return the number of the slot in the utmp file
327c478bd9Sstevel@tonic-gate  * corresponding to the current user: try for file 0, 1, 2.
337c478bd9Sstevel@tonic-gate  * Returns -1 if slot not found.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
36*7257d1b4Sraf #include "lint.h"
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <utmpx.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
44ec6bbcf8Sraf #include <pthread.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #ifndef TRUE
477c478bd9Sstevel@tonic-gate #define	TRUE 1
487c478bd9Sstevel@tonic-gate #define	FALSE 0
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate int
ttyslot(void)527c478bd9Sstevel@tonic-gate ttyslot(void)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	struct futmpx ubuf;
55ec6bbcf8Sraf 	char *p;
567c478bd9Sstevel@tonic-gate 	int s;
57ec6bbcf8Sraf 	int ret = -1;
58ec6bbcf8Sraf 	int console = FALSE;
597c478bd9Sstevel@tonic-gate 	char ttynm[128];
60ec6bbcf8Sraf 	FILE *fp;
61ec6bbcf8Sraf 	int cancel_state;
627c478bd9Sstevel@tonic-gate 
63ec6bbcf8Sraf 	/*
64ec6bbcf8Sraf 	 * The UNIX98 Posix conformance test suite requires
65ec6bbcf8Sraf 	 * ttyslot() to not be a cancellation point.
66ec6bbcf8Sraf 	 */
67ec6bbcf8Sraf 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
687c478bd9Sstevel@tonic-gate 
69ec6bbcf8Sraf 	if ((p = ttyname_r(0, ttynm, 128)) != NULL ||
70ec6bbcf8Sraf 	    (p = ttyname_r(1, ttynm, 128)) != NULL ||
71ec6bbcf8Sraf 	    (p = ttyname_r(2, ttynm, 128)) != NULL) {
72ec6bbcf8Sraf 		if (strncmp(p, "/dev/", 5) == 0)
73ec6bbcf8Sraf 			p += 5;
74ec6bbcf8Sraf 		if (strcmp(p, "console") == 0)
75ec6bbcf8Sraf 			console = TRUE;
76ec6bbcf8Sraf 		s = 0;
77ec6bbcf8Sraf 		if ((fp = fopen(UTMPX_FILE, "rF")) != NULL) {
78ec6bbcf8Sraf 			while ((fread(&ubuf, sizeof (ubuf), 1, fp)) == 1) {
79ec6bbcf8Sraf 				if ((ubuf.ut_type == INIT_PROCESS ||
80ec6bbcf8Sraf 				    ubuf.ut_type == LOGIN_PROCESS ||
81ec6bbcf8Sraf 				    ubuf.ut_type == USER_PROCESS) &&
82ec6bbcf8Sraf 				    strncmp(p, ubuf.ut_line,
83ec6bbcf8Sraf 				    sizeof (ubuf.ut_line)) == 0) {
84ec6bbcf8Sraf 					ret = s;
85ec6bbcf8Sraf 					if (!console ||
86ec6bbcf8Sraf 					    strncmp(ubuf.ut_host, ":0", 2) == 0)
87ec6bbcf8Sraf 						break;
88ec6bbcf8Sraf 				}
89ec6bbcf8Sraf 				s++;
90ec6bbcf8Sraf 			}
91ec6bbcf8Sraf 			(void) fclose(fp);
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 	}
94ec6bbcf8Sraf 
95ec6bbcf8Sraf 	(void) pthread_setcancelstate(cancel_state, NULL);
967c478bd9Sstevel@tonic-gate 	return (ret);
977c478bd9Sstevel@tonic-gate }
98