2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Return the number of the slot in the utmp file
2N/A * corresponding to the current user: try for file 0, 1, 2.
2N/A * Returns -1 if slot not found.
2N/A */
2N/A
2N/A#include "lint.h"
2N/A#include <string.h>
2N/A#include <stdio.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <unistd.h>
2N/A#include <utmpx.h>
2N/A#include <stdlib.h>
2N/A#include <pthread.h>
2N/A
2N/A#ifndef TRUE
2N/A#define TRUE 1
2N/A#define FALSE 0
2N/A#endif
2N/A
2N/Aint
2N/Attyslot(void)
2N/A{
2N/A struct futmpx ubuf;
2N/A char *p;
2N/A int s;
2N/A int ret = -1;
2N/A int console = FALSE;
2N/A char ttynm[128];
2N/A FILE *fp;
2N/A int cancel_state;
2N/A
2N/A /*
2N/A * The UNIX98 Posix conformance test suite requires
2N/A * ttyslot() to not be a cancellation point.
2N/A */
2N/A (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
2N/A
2N/A if ((p = ttyname_r(0, ttynm, 128)) != NULL ||
2N/A (p = ttyname_r(1, ttynm, 128)) != NULL ||
2N/A (p = ttyname_r(2, ttynm, 128)) != NULL) {
2N/A if (strncmp(p, "/dev/", 5) == 0)
2N/A p += 5;
2N/A if (strcmp(p, "console") == 0)
2N/A console = TRUE;
2N/A s = 0;
2N/A if ((fp = fopen(UTMPX_FILE, "rF")) != NULL) {
2N/A while ((fread(&ubuf, sizeof (ubuf), 1, fp)) == 1) {
2N/A if ((ubuf.ut_type == INIT_PROCESS ||
2N/A ubuf.ut_type == LOGIN_PROCESS ||
2N/A ubuf.ut_type == USER_PROCESS) &&
2N/A strncmp(p, ubuf.ut_line,
2N/A sizeof (ubuf.ut_line)) == 0) {
2N/A ret = s;
2N/A if (!console ||
2N/A strncmp(ubuf.ut_host, ":0", 2) == 0)
2N/A break;
2N/A }
2N/A s++;
2N/A }
2N/A (void) fclose(fp);
2N/A }
2N/A }
2N/A
2N/A (void) pthread_setcancelstate(cancel_state, NULL);
2N/A return (ret);
2N/A}