moved some variables around
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19457 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+40
-48
@@ -91,11 +91,6 @@ setenv(const char *var, const char *value, bool overwrite)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* reapchild. Child process is out there, let's catch its termination.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* spawn_shell(): spawn child process, create pty master/slave device and
|
* spawn_shell(): spawn child process, create pty master/slave device and
|
||||||
* execute SHELL program.
|
* execute SHELL program.
|
||||||
@@ -119,43 +114,26 @@ typedef struct
|
|||||||
#define PTY_NG 1 /* pty open or set termios NG */
|
#define PTY_NG 1 /* pty open or set termios NG */
|
||||||
#define PTY_WS 2 /* pty need WINSIZE (row and col ) */
|
#define PTY_WS 2 /* pty need WINSIZE (row and col ) */
|
||||||
|
|
||||||
/* global varriables */
|
/* global variables */
|
||||||
|
|
||||||
pid_t sh_pid;
|
pid_t sh_pid;
|
||||||
|
|
||||||
int
|
int
|
||||||
spawn_shell(int row, int col, const char *command, const char *coding)
|
spawn_shell(int row, int col, const char *command, const char *coding)
|
||||||
{
|
{
|
||||||
int done = 0;
|
|
||||||
pid_t pgrp;
|
|
||||||
|
|
||||||
int master = -1;
|
|
||||||
int slave;
|
|
||||||
struct termios tio;
|
|
||||||
struct winsize ws;
|
struct winsize ws;
|
||||||
|
|
||||||
handshake_t handshake;
|
handshake_t handshake;
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
time_t now_time_t;
|
|
||||||
struct tm *now_time;
|
|
||||||
int now_hour;
|
|
||||||
|
|
||||||
char *args[16];
|
|
||||||
char com_line[256];
|
|
||||||
char *ptr;
|
|
||||||
|
|
||||||
char err_msg[256];
|
|
||||||
|
|
||||||
signal(SIGTTOU, SIG_IGN);
|
signal(SIGTTOU, SIG_IGN);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get a psuedo-tty. We do this by cycling through files in the
|
* Get a psuedo-tty. We do this by cycling through files in the
|
||||||
* directory. The oparationg system will not allow us to open a master
|
* directory. The oparationg system will not allow us to open a master
|
||||||
* which is already in use, so we simply go until the open succeeds.
|
* which is already in use, so we simply go until the open succeeds.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char tty_name[B_PATH_NAME_LENGTH];
|
char tty_name[B_PATH_NAME_LENGTH];
|
||||||
|
int master = -1;
|
||||||
DIR *dir = opendir("/dev/pt/");
|
DIR *dir = opendir("/dev/pt/");
|
||||||
if (dir != NULL) {
|
if (dir != NULL) {
|
||||||
struct dirent *dirEntry;
|
struct dirent *dirEntry;
|
||||||
@@ -181,7 +159,6 @@ spawn_shell(int row, int col, const char *command, const char *coding)
|
|||||||
closedir(dir);
|
closedir(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If master is still < 0 then we haven't found a tty we can use
|
|
||||||
if (master < 0) {
|
if (master < 0) {
|
||||||
printf("didn't find any available pesudo ttys.");
|
printf("didn't find any available pesudo ttys.");
|
||||||
return -1;
|
return -1;
|
||||||
@@ -217,18 +194,19 @@ spawn_shell(int row, int col, const char *command, const char *coding)
|
|||||||
close(pc_pipe[1]);
|
close(pc_pipe[1]);
|
||||||
|
|
||||||
/* Set process session leader */
|
/* Set process session leader */
|
||||||
if ((pgrp = setsid()) < 0) {
|
if (setsid() < 0) {
|
||||||
handshake.status = PTY_NG;
|
handshake.status = PTY_NG;
|
||||||
sprintf(handshake.msg, "could not set session leader.");
|
sprintf(handshake.msg, "could not set session leader.");
|
||||||
write(cp_pipe[1], (char *)&handshake, sizeof (handshake));
|
write(cp_pipe[1], (char *)&handshake, sizeof (handshake));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* change pty owner and asscess mode. */
|
/* change pty owner and access mode. */
|
||||||
chown(tty_name, getuid(), getgid());
|
chown(tty_name, getuid(), getgid());
|
||||||
chmod(tty_name, S_IRUSR | S_IWUSR);
|
chmod(tty_name, S_IRUSR | S_IWUSR);
|
||||||
|
|
||||||
/* open slave pty */
|
/* open slave pty */
|
||||||
|
int slave = -1;
|
||||||
if ((slave = open(tty_name, O_RDWR)) < 0) {
|
if ((slave = open(tty_name, O_RDWR)) < 0) {
|
||||||
handshake.status = PTY_NG;
|
handshake.status = PTY_NG;
|
||||||
sprintf(handshake.msg, "can't open tty (%s).", tty_name);
|
sprintf(handshake.msg, "can't open tty (%s).", tty_name);
|
||||||
@@ -236,7 +214,11 @@ spawn_shell(int row, int col, const char *command, const char *coding)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get tty termios (don't necessary). */
|
struct termios tio;
|
||||||
|
|
||||||
|
/* get tty termios (not necessary).
|
||||||
|
* TODO: so why are we doing it ?
|
||||||
|
*/
|
||||||
tcgetattr(slave, &tio);
|
tcgetattr(slave, &tio);
|
||||||
|
|
||||||
/* set signal default */
|
/* set signal default */
|
||||||
@@ -281,7 +263,7 @@ spawn_shell(int row, int col, const char *command, const char *coding)
|
|||||||
tio.c_lflag |= ISIG|ICANON|ECHO|ECHOE|ECHONL;
|
tio.c_lflag |= ISIG|ICANON|ECHO|ECHOE|ECHONL;
|
||||||
tio.c_lflag &= ~(ECHOK | IEXTEN);
|
tio.c_lflag &= ~(ECHOK | IEXTEN);
|
||||||
|
|
||||||
/* set control charactors. */
|
/* set control characters. */
|
||||||
tio.c_cc[VINTR] = 'C' & 0x1f; /* '^C' */
|
tio.c_cc[VINTR] = 'C' & 0x1f; /* '^C' */
|
||||||
tio.c_cc[VQUIT] = CQUIT; /* '^\' */
|
tio.c_cc[VQUIT] = CQUIT; /* '^\' */
|
||||||
tio.c_cc[VERASE] = 0x08; /* '^H' */
|
tio.c_cc[VERASE] = 0x08; /* '^H' */
|
||||||
@@ -343,13 +325,18 @@ spawn_shell(int row, int col, const char *command, const char *coding)
|
|||||||
* to this process group ID (equal process ID).
|
* to this process group ID (equal process ID).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pgrp = getpid();
|
pid_t processGroup = getpid();
|
||||||
setpgid(pgrp, pgrp);
|
if (setpgid(processGroup, processGroup) < 0) {
|
||||||
tcsetpgrp(0, pgrp);
|
handshake.status = PTY_NG;
|
||||||
|
sprintf(handshake.msg, "can't set process group id.");
|
||||||
|
write(cp_pipe[1], (char *)&handshake, sizeof(handshake));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
tcsetpgrp(0, processGroup);
|
||||||
|
|
||||||
/* mark the pipes as close on exec */
|
/* mark the pipes as close on exec */
|
||||||
fcntl(cp_pipe[1], F_SETFD, 1);
|
fcntl(cp_pipe[1], F_SETFD, FD_CLOEXEC);
|
||||||
fcntl(pc_pipe[0], F_SETFD, 1);
|
fcntl(pc_pipe[0], F_SETFD, FD_CLOEXEC);
|
||||||
|
|
||||||
/* pty open and set termios successful. */
|
/* pty open and set termios successful. */
|
||||||
handshake.status = PTY_OK;
|
handshake.status = PTY_OK;
|
||||||
@@ -368,13 +355,14 @@ spawn_shell(int row, int col, const char *command, const char *coding)
|
|||||||
if (command == NULL)
|
if (command == NULL)
|
||||||
command = SHELL_COMMAND;
|
command = SHELL_COMMAND;
|
||||||
|
|
||||||
memcpy (com_line, command, 256);
|
|
||||||
ptr = com_line;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* split up the arguments in the command into an artv-like structure.
|
* split up the arguments in the command into an artv-like structure.
|
||||||
*/
|
*/
|
||||||
|
char commandLine[256];
|
||||||
|
memcpy(commandLine, command, 256);
|
||||||
|
char *ptr = commandLine;
|
||||||
|
char *args[16];
|
||||||
|
int i = 0;
|
||||||
while (*ptr) {
|
while (*ptr) {
|
||||||
/* Skip white space */
|
/* Skip white space */
|
||||||
while ((*ptr == ' ') || (*ptr == '\t'))
|
while ((*ptr == ' ') || (*ptr == '\t'))
|
||||||
@@ -390,13 +378,15 @@ spawn_shell(int row, int col, const char *command, const char *coding)
|
|||||||
|
|
||||||
setenv("SHELL", *args, true);
|
setenv("SHELL", *args, true);
|
||||||
|
|
||||||
|
#if 0
|
||||||
/*
|
/*
|
||||||
* Print Welcome Message.
|
* Print Welcome Message.
|
||||||
* (But, Only print message when MuTerminal coding is UTF8.)
|
* (But, Only print message when MuTerminal coding is UTF8.)
|
||||||
*/
|
*/
|
||||||
now_time_t = time(NULL);
|
|
||||||
now_time = localtime (&now_time_t);
|
|
||||||
|
|
||||||
|
time_t now_time_t = time(NULL);
|
||||||
|
struct tm *now_time = localtime (&now_time_t);
|
||||||
|
int now_hour = 0;
|
||||||
if (now_time->tm_hour >= 5 && now_time->tm_hour < 11) {
|
if (now_time->tm_hour >= 5 && now_time->tm_hour < 11) {
|
||||||
now_hour = 0;
|
now_hour = 0;
|
||||||
} else if (now_time->tm_hour >= 11 && now_time->tm_hour <= 18 ) {
|
} else if (now_time->tm_hour >= 11 && now_time->tm_hour <= 18 ) {
|
||||||
@@ -404,16 +394,17 @@ spawn_shell(int row, int col, const char *command, const char *coding)
|
|||||||
} else {
|
} else {
|
||||||
now_hour = 2;
|
now_hour = 2;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
execve(*args, args, environ);
|
execve(*args, args, environ);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Exec failed.
|
* Exec failed.
|
||||||
*/
|
*/
|
||||||
sleep(1);
|
sleep(1);
|
||||||
sprintf(err_msg, kSpawnAlertMessage, com_line, strerror(errno));
|
char errorMessage[256];
|
||||||
|
snprintf(errorMessage, sizeof(errorMessage), kSpawnAlertMessage, commandLine, strerror(errno));
|
||||||
|
|
||||||
if (system(err_msg) == 0)
|
if (system(errorMessage) == 0)
|
||||||
execl("/bin/sh", "/bin/sh", "-login", NULL);
|
execl("/bin/sh", "/bin/sh", "-login", NULL);
|
||||||
|
|
||||||
exit(1);
|
exit(1);
|
||||||
@@ -432,6 +423,7 @@ spawn_shell(int row, int col, const char *command, const char *coding)
|
|||||||
* close parent control tty.
|
* close parent control tty.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
int done = 0;
|
||||||
while (!done) {
|
while (!done) {
|
||||||
read (cp_pipe[0], (char *)&handshake, sizeof (handshake));
|
read (cp_pipe[0], (char *)&handshake, sizeof (handshake));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user