xref: /titanic_44/usr/src/lib/libc/port/gen/ttyslot.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7257d1b4Sraf 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Return the number of the slot in the utmp file
347c478bd9Sstevel@tonic-gate  * corresponding to the current user: try for file 0, 1, 2.
357c478bd9Sstevel@tonic-gate  * Returns -1 if slot not found.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
38*7257d1b4Sraf #include "lint.h"
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <utmpx.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
46ec6bbcf8Sraf #include <pthread.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #ifndef TRUE
497c478bd9Sstevel@tonic-gate #define	TRUE 1
507c478bd9Sstevel@tonic-gate #define	FALSE 0
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate int
ttyslot(void)547c478bd9Sstevel@tonic-gate ttyslot(void)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	struct futmpx ubuf;
57ec6bbcf8Sraf 	char *p;
587c478bd9Sstevel@tonic-gate 	int s;
59ec6bbcf8Sraf 	int ret = -1;
60ec6bbcf8Sraf 	int console = FALSE;
617c478bd9Sstevel@tonic-gate 	char ttynm[128];
627c478bd9Sstevel@tonic-gate 	FILE *fp;
63ec6bbcf8Sraf 	int cancel_state;
647c478bd9Sstevel@tonic-gate 
65ec6bbcf8Sraf 	/*
66ec6bbcf8Sraf 	 * The UNIX98 Posix conformance test suite requires
67ec6bbcf8Sraf 	 * ttyslot() to not be a cancellation point.
68ec6bbcf8Sraf 	 */
69ec6bbcf8Sraf 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
707c478bd9Sstevel@tonic-gate 
71ec6bbcf8Sraf 	if ((p = ttyname_r(0, ttynm, 128)) != NULL ||
72ec6bbcf8Sraf 	    (p = ttyname_r(1, ttynm, 128)) != NULL ||
73ec6bbcf8Sraf 	    (p = ttyname_r(2, ttynm, 128)) != NULL) {
74ec6bbcf8Sraf 		if (strncmp(p, "/dev/", 5) == 0)
757c478bd9Sstevel@tonic-gate 			p += 5;
767c478bd9Sstevel@tonic-gate 		if (strcmp(p, "console") == 0)
777c478bd9Sstevel@tonic-gate 			console = TRUE;
787c478bd9Sstevel@tonic-gate 		s = 0;
79ec6bbcf8Sraf 		if ((fp = fopen(UTMPX_FILE, "rF")) != NULL) {
807c478bd9Sstevel@tonic-gate 			while ((fread(&ubuf, sizeof (ubuf), 1, fp)) == 1) {
817c478bd9Sstevel@tonic-gate 				if ((ubuf.ut_type == INIT_PROCESS ||
827c478bd9Sstevel@tonic-gate 				    ubuf.ut_type == LOGIN_PROCESS ||
837c478bd9Sstevel@tonic-gate 				    ubuf.ut_type == USER_PROCESS) &&
84ec6bbcf8Sraf 				    strncmp(p, ubuf.ut_line,
85ec6bbcf8Sraf 				    sizeof (ubuf.ut_line)) == 0) {
867c478bd9Sstevel@tonic-gate 					ret = s;
87ec6bbcf8Sraf 					if (!console ||
88ec6bbcf8Sraf 					    strncmp(ubuf.ut_host, ":0", 2) == 0)
897c478bd9Sstevel@tonic-gate 						break;
907c478bd9Sstevel@tonic-gate 				}
917c478bd9Sstevel@tonic-gate 				s++;
927c478bd9Sstevel@tonic-gate 			}
937c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
94ec6bbcf8Sraf 		}
95ec6bbcf8Sraf 	}
96ec6bbcf8Sraf 
97ec6bbcf8Sraf 	(void) pthread_setcancelstate(cancel_state, NULL);
987c478bd9Sstevel@tonic-gate 	return (ret);
997c478bd9Sstevel@tonic-gate }
100