add logname, getlogin()

This commit is contained in:
K. Lange 2018-10-09 20:35:53 +09:00
parent c35b51ad14
commit f3c800b791
7 changed files with 77 additions and 8 deletions

View File

@ -21,6 +21,7 @@
#include <pwd.h>
#include <sys/wait.h>
#include <sys/utsname.h>
#include <sys/ioctl.h>
#include <toaru/auth.h>
@ -142,6 +143,7 @@ do_fork:
pid = getpid();
f = fork();
if (getpid() != pid) {
ioctl(STDIN_FILENO, IOCTLTTYLOGIN, &uid);
setuid(uid);
toaru_auth_set_vars();
char * args[] = {

22
apps/logname.c Normal file
View File

@ -0,0 +1,22 @@
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2018 K. Lange
*
* logname - Effectively the same as whoami, but for compliance
* with POSIX, this uses getlogin().
*/
#include <unistd.h>
#include <stdio.h>
int main(int argc, char ** argv) {
char * name = getlogin();
if (!name) {
fprintf(stderr, "%s: failed to determine login name\n", argv[0]);
return 1;
}
fprintf(stdout, "%s\n", name);
return 0;
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <stdio.h>
#include <sys/types.h>
struct passwd {

View File

@ -8,7 +8,8 @@
#define IOCTL_DTYPE_FILE 1
#define IOCTL_DTYPE_TTY 2
#define IOCTLTTYNAME 0x4F01
#define IOCTLTTYNAME 0x4F01
#define IOCTLTTYLOGIN 0x4F02
#define IOCTL_PACKETFS_QUEUED 0x5050

View File

@ -67,6 +67,7 @@ extern char * ttyname(int fd);
extern int utime(const char *filename, const struct utimbuf *times);
extern int rmdir(const char *pathname); /* TODO rm probably just works */
extern int chown(const char * pathname, uid_t owner, gid_t group);
extern char * getlogin(void);
#define STDIN_FILENO 0
#define STDOUT_FILENO 1

View File

@ -198,42 +198,50 @@ int pty_ioctl(pty_t * pty, int request, void * argp) {
*/
return IOCTL_DTYPE_TTY;
case IOCTLTTYNAME:
if (!argp) return -1;
if (!argp) return -EINVAL;
validate(argp);
((char*)argp)[0] = '\0';
sprintf((char*)argp, "/dev/pts/%d", pty->name);
return 0;
case IOCTLTTYLOGIN:
/* Set the user id of the login user */
if (current_process->user != 0) return -EPERM;
if (!argp) return -EINVAL;
validate(argp);
pty->slave->uid = *(int*)argp;
pty->master->uid = *(int*)argp;
return 0;
case TIOCSWINSZ:
if (!argp) return -1;
if (!argp) return -EINVAL;
validate(argp);
memcpy(&pty->size, argp, sizeof(struct winsize));
/* TODO send sigwinch to fg_prog */
return 0;
case TIOCGWINSZ:
if (!argp) return -1;
if (!argp) return -EINVAL;
validate(argp);
memcpy(argp, &pty->size, sizeof(struct winsize));
return 0;
case TCGETS:
if (!argp) return -1;
if (!argp) return -EINVAL;
validate(argp);
memcpy(argp, &pty->tios, sizeof(struct termios));
return 0;
case TIOCSPGRP:
if (!argp) return -1;
if (!argp) return -EINVAL;
validate(argp);
pty->fg_proc = *(pid_t *)argp;
debug_print(NOTICE, "Setting PTY group to %d", pty->fg_proc);
return 0;
case TIOCGPGRP:
if (!argp) return -1;
if (!argp) return -EINVAL;
validate(argp);
*(pid_t *)argp = pty->fg_proc;
return 0;
case TCSETS:
case TCSETSW:
case TCSETSF:
if (!argp) return -1;
if (!argp) return -EINVAL;
validate(argp);
if (!(((struct termios *)argp)->c_lflag & ICANON) && (pty->tios.c_lflag & ICANON)) {
/* Switch out of canonical mode, the dump the input buffer */

34
libc/unistd/getlogin.c Normal file
View File

@ -0,0 +1,34 @@
#include <unistd.h>
#include <pwd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
static char _name[64]; /* NAME_MAX ? */
char * getlogin(void) {
int tty = STDIN_FILENO;
if (!isatty(tty)) {
tty = STDOUT_FILENO;
if (!isatty(tty)) {
tty = STDERR_FILENO;
if (!isatty(tty)) {
errno = ENOTTY;
return NULL;
}
}
}
/* Get the owner */
struct stat statbuf;
fstat(tty, &statbuf);
struct passwd * passwd = getpwuid(statbuf.st_uid);
if (!passwd) return NULL;
if (!passwd->pw_name) return NULL;
memcpy(_name, passwd->pw_name, strlen(passwd->pw_name));
return _name;
}