summaryrefslogtreecommitdiffstats
path: root/loader/kon2/tools/newvc.c
blob: 3b60e7e5f5998554af0232872226ee63f0e96d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Newvc - run a program in another virtual console */

/* Copyright (C) 1992 by MAEDA Atusi (mad@math.keio.ac.jp) */
/* Version 0.1 92/1/11 */
/* Version 0.2 92/1/19 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <pwd.h>
#include <utmp.h>
#include <time.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/kd.h>
#include <sys/vt.h>
#include <sys/wait.h>

#define MAXTTYLEN 12

#define MAXCMDLEN 4096

char cmdBuf[MAXCMDLEN];

char *progName;
char newTtyName[MAXTTYLEN];

void error(const char *message, const char *perrorMessage) {
    fprintf(stderr, "%s: %s\n", progName, message);
    if (perrorMessage) {
	perror(perrorMessage);
    }
    exit(EXIT_FAILURE);
}

struct passwd *pw;
struct utmp ut;

void setUtmpEntry(int pid) {

    pw = getpwuid(getuid());
    setutent();				 /* open utmp */
    strncpy(ut.ut_id, newTtyName + strlen("/dev/tty"), sizeof(ut.ut_id));
    ut.ut_type = DEAD_PROCESS;
    getutid(&ut);			 /* set position */
    /* Set up the new entry. */
    ut.ut_type = USER_PROCESS;
    strncpy(ut.ut_line, newTtyName + strlen("/dev/"), sizeof(ut.ut_line));
    strncpy(ut.ut_user, (pw && pw->pw_name) ? pw->pw_name : "????",
	    sizeof(ut.ut_user));
/*
    gethostname(ut.ut_host, sizeof(ut.ut_host));
*/
    ut.ut_pid = pid;
    ut.ut_time = time(NULL);
    pututline(&ut);
    endutent();				 /* close utmp */
}

void restoreUtmpEntry(int pid) {
    struct utmp *utp;

    pw = getpwuid(getuid());
    setutent();				 /* open utmp */
    strncpy(ut.ut_id, newTtyName + strlen("/dev/tty"), sizeof(ut.ut_id));
    ut.ut_type = USER_PROCESS;
    utp = getutid(&ut);			 /* search entry */
    /* Set up the new entry. */
    if (utp && utp->ut_pid == pid) {
	ut.ut_type = DEAD_PROCESS;
	ut.ut_time = time(NULL);
	pututline(&ut);
    }
    endutent();				 /* close utmp */
}

int main(int argc, char* argv[]) {
    int	curVcNum, newVcNum;
    int consoleFd = 0;
    int newVcFd;
    int childPid;
    struct vt_stat vts;

    progName = argv[0];

    if ((consoleFd = open("/dev/console", 0)) < 0) {
	    error("can't open console", "/dev/console");
    }

    ioctl(consoleFd, VT_GETSTATE, &vts);
    curVcNum = vts.v_active;

    ioctl(consoleFd, VT_OPENQRY, &newVcNum);
    if (newVcNum < 0) {
	error("can't find unused virtual console", NULL);
    }
    sprintf(newTtyName, "/dev/tty%d", newVcNum);

    setsid();
    
    if ((newVcFd = open(newTtyName, O_RDWR)) < 0) {
	error("can't open virtual console", newTtyName);
    }
    if (ioctl(consoleFd, VT_ACTIVATE, newVcNum) != 0) {
	error("can't switch virtual console", "ioctl VT_ACTIVATE");
    }

    dup2(newVcFd, 0);
    dup2(newVcFd, 1);
    dup2(newVcFd, 2);

    if ((childPid = fork()) < 0) {
	error("fork failed", "fork");
    }
    if (childPid) {
	/* Parent process. */
	int status;

	setUtmpEntry(childPid);

	wait(&status);

	restoreUtmpEntry(childPid);

	if (ioctl(0, VT_ACTIVATE, curVcNum) != 0) {
	    error("couldn't restore original console", "ioctl(0, VT_ACTIVATE)");
	}
	return WEXITSTATUS(status);
    } else {
	/* Child process. */
	char *shell, *command;
	char *newArgv[] = {"/bin/sh", "-c", cmdBuf, NULL};

	setuid(getuid());
	setgid(getgid());

	if ((shell = getenv("SHELL")) == NULL) {
	    shell = "/bin/sh";
	}
	if (argc == 1) {
	    /* No command specified.  Run shell as default. */
	    if ((command = rindex(shell, '/')) == NULL) {
		command = shell;
	    } else {
		command++;
	    }
	    newArgv[1] = NULL;
	} else {
	    int i;

	    command = argv[1];
	    for (i = 1; i < argc; i++) {
		strncat(cmdBuf, argv[i], MAXCMDLEN);
		strncat(cmdBuf, " ", MAXCMDLEN);
	    }
	}
	newArgv[0] = command;

	execv(shell, newArgv);

	error("can't exec", shell);

	return EXIT_FAILURE;
    }
}