17c478bd9Sstevel@tonic-gate /* 2*8fd04b83SRoger A. Faulkner * CDDL HEADER START 3*8fd04b83SRoger A. Faulkner * 4*8fd04b83SRoger A. Faulkner * The contents of this file are subject to the terms of the 5*8fd04b83SRoger A. Faulkner * Common Development and Distribution License (the "License"). 6*8fd04b83SRoger A. Faulkner * You may not use this file except in compliance with the License. 7*8fd04b83SRoger A. Faulkner * 8*8fd04b83SRoger A. Faulkner * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*8fd04b83SRoger A. Faulkner * or http://www.opensolaris.org/os/licensing. 10*8fd04b83SRoger A. Faulkner * See the License for the specific language governing permissions 11*8fd04b83SRoger A. Faulkner * and limitations under the License. 12*8fd04b83SRoger A. Faulkner * 13*8fd04b83SRoger A. Faulkner * When distributing Covered Code, include this CDDL HEADER in each 14*8fd04b83SRoger A. Faulkner * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*8fd04b83SRoger A. Faulkner * If applicable, add the following below this CDDL HEADER, with the 16*8fd04b83SRoger A. Faulkner * fields enclosed by brackets "[]" replaced with your own identifying 17*8fd04b83SRoger A. Faulkner * information: Portions Copyright [yyyy] [name of copyright owner] 18*8fd04b83SRoger A. Faulkner * 19*8fd04b83SRoger A. Faulkner * CDDL HEADER END 20*8fd04b83SRoger A. Faulkner */ 21*8fd04b83SRoger A. Faulkner 22*8fd04b83SRoger A. Faulkner /* 23*8fd04b83SRoger A. Faulkner * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * Copyright (c) 1984 Regents of the University of California. 297c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 307c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* 347c478bd9Sstevel@tonic-gate * Return the number of the slot in the utmp file 357c478bd9Sstevel@tonic-gate * corresponding to the current user: try for file 0, 1, 2. 367c478bd9Sstevel@tonic-gate * To mimic the behavior of getttyent, we loop through utmp 377c478bd9Sstevel@tonic-gate * and try to find an entry with a matching line number. 387c478bd9Sstevel@tonic-gate * If we don't find one we return the index of the end of 397c478bd9Sstevel@tonic-gate * the file, so that the record can be added to the end of 407c478bd9Sstevel@tonic-gate * the file. 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate #include "../../sys/common/compat.h" 437c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 447c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 457c478bd9Sstevel@tonic-gate #include <stdio.h> 465d54f3d8Smuffin #include <unistd.h> 475d54f3d8Smuffin #include <strings.h> 487c478bd9Sstevel@tonic-gate 495d54f3d8Smuffin int 505d54f3d8Smuffin ttyslot(void) 517c478bd9Sstevel@tonic-gate { 525d54f3d8Smuffin char *tp, *p; 535d54f3d8Smuffin int s; 547c478bd9Sstevel@tonic-gate int fd; 557c478bd9Sstevel@tonic-gate struct utmpx utx; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate if ((tp = ttyname(0)) == NULL && 597c478bd9Sstevel@tonic-gate (tp = ttyname(1)) == NULL && 607c478bd9Sstevel@tonic-gate (tp = ttyname(2)) == NULL) 617c478bd9Sstevel@tonic-gate return (0); 627c478bd9Sstevel@tonic-gate if ((p = rindex(tp, '/')) == NULL) 637c478bd9Sstevel@tonic-gate p = tp; 647c478bd9Sstevel@tonic-gate else 657c478bd9Sstevel@tonic-gate p++; 667c478bd9Sstevel@tonic-gate 67*8fd04b83SRoger A. Faulkner if ((fd = _syscall(SYS_openat, 68*8fd04b83SRoger A. Faulkner AT_FDCWD, "/etc/utmpx", O_RDONLY)) == -1) { 697c478bd9Sstevel@tonic-gate perror("ttyslot: open of /etc/utmpx failed:"); 707c478bd9Sstevel@tonic-gate return (0); 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate s = 0; 747c478bd9Sstevel@tonic-gate while (_read(fd, &utx, sizeof (struct utmpx)) > 0) { 757c478bd9Sstevel@tonic-gate s++; 767c478bd9Sstevel@tonic-gate if (strncmp(utx.ut_line, p, sizeof (utx.ut_line)) == 0) { 777c478bd9Sstevel@tonic-gate _syscall(SYS_close, fd); 787c478bd9Sstevel@tonic-gate return (s); 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate _syscall(SYS_close, fd); 827c478bd9Sstevel@tonic-gate return (s); 837c478bd9Sstevel@tonic-gate } 84