- rewrote the code that searches the for a free pty/tty - it is much cleaner now, as we talked about on the mailing list some time ago. \n- started to apply our styling guide \n- some changes not worth mentioning here and there ;)

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@9534 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
assimil8or
2004-10-27 15:54:47 +00:00
parent 7b5e243a4d
commit ead4f89ebb
2 changed files with 301 additions and 326 deletions
+44 -57
View File
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2003-4 Kian Duffy <[email protected]>
* Copyright (c) 2004 Daniel Furrer <[email protected]>
* Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
*
* Permission is hereby granted, free of charge, to any person obtaining
@@ -28,6 +29,7 @@
*
*/
#include <dirent.h>
#include <sys/param.h>
#include <sys/file.h>
#include <stdio.h>
@@ -56,7 +58,7 @@ extern char **environ;
char spawn_alert_msg [] = \
"alert --stop " \
"'MuTerminal Error!!\n\
"'Haiku Terminal Error!!\n\
Cannot execute shell [%s].\n\
Check \"Shell\" in preferance panel.' "\
"'Exec /bin/sh' 'Abort'";
@@ -118,15 +120,12 @@ typedef struct
/* global varriables */
pid_t sh_pid;
char tty_name[16];
int pfd_num = 0;
char tty_name[B_PATH_NAME_LENGTH];
int
spawn_shell (int row, int col, const char *command, const char *coding)
{
int done = 0;
char *s;
char *t = 0;
pid_t pgrp;
int master = 0;
@@ -136,8 +135,6 @@ spawn_shell (int row, int col, const char *command, const char *coding)
handshake_t handshake;
char pty_name[16];
int i = 0;
time_t now_time_t;
struct tm *now_time;
@@ -149,37 +146,39 @@ spawn_shell (int row, int col, const char *command, const char *coding)
char err_msg[256];
signal (SIGTTOU, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
/*
* Get a psuedo-tty. We do this by cycling through all the possible
* names. The oparationg system will not allow us to open a master
* which is already in use, so we simply go until the open successed.
* 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
* which is already in use, so we simply go until the open succeeds.
*/
for (s = PTYCHAR1; *s != 0; s++) {
for (t = PTYCHAR2; *t != 0; t++) {
sprintf (pty_name, PTYDEV, *s, *t);
if ((master = open (pty_name, O_RDWR)) >= 0)
goto out;
pfd_num++;
}
DIR *dir;
dir = opendir("/dev/pt/");
struct dirent *dir_entry;
char pty_name[B_PATH_NAME_LENGTH];
while(NULL != (dir_entry = readdir(dir)))
{
if (dir_entry->d_name[0] == '.') // skip . and ..
continue;
sprintf(pty_name, "/dev/pt/%s", dir_entry->d_name);
printf("%s\n", pty_name);
master = open(pty_name, O_RDWR);
if (master >= 0)
break;
}
out:
/*
* If s and t are NULL, we ran out of pseudo ttys before we found
* one we can use
*/
if ((*s == 0) && (*t == 0)) {
printf ("don't found pesudo ttys.");
// If master is still < 0 then we haven't found a tty we can use
if (master < 0)
{
printf("didn't find any available pesudo ttys.");
return -1;
}
/* Change "/dev/pt/XX (master) to "/dev/tt/XX" (slave). */
sprintf (pty_name,"%c%c", *s, *t);
sprintf (tty_name, TTYDEV, *s, *t);
// Set the tty that corresponds to the pty we found
sprintf (tty_name, "/dev/tt/%s", dir_entry->d_name);
/*
* Get the modes of the current terminal. We will duplicates these
@@ -192,17 +191,10 @@ spawn_shell (int row, int col, const char *command, const char *coding)
return -1;
}
/*
* If set prosess group ID is MuTerminal process ID, MuTerminal
* ignore SIGINT for exec Terminal.
*
* pgrp = getpid ();
* setpgid (0, pgrp);
*/
/* Fork a child process. */
if ((sh_pid=fork()) < 0) {
close (master);
if ((sh_pid = fork()) < 0) {
close(master);
return -1;
}
@@ -211,25 +203,20 @@ spawn_shell (int row, int col, const char *command, const char *coding)
* Now in child process.
*/
/* close master pty and control tty. */
// close (tty);
// for (i = 0; i <= 2; i++)
// close (i);
/*
* Make our controlling tty the pseudo tty. This hapens because
* we cleared our original controlling terminal above.
*/
close (cp_pipe[0]);
close (pc_pipe[1]);
close(cp_pipe[0]);
close(pc_pipe[1]);
/* Set process session leader */
if ((pgrp = setsid()) < 0) {
handshake.status = PTY_NG;
sprintf (handshake.msg, "could not set session leader.");
write (cp_pipe[1], (char *)&handshake, sizeof (handshake));
exit (1);
sprintf(handshake.msg, "could not set session leader.");
write(cp_pipe[1], (char *)&handshake, sizeof (handshake));
exit(1);
}
/* change pty owner and asscess mode. */
@@ -356,9 +343,9 @@ spawn_shell (int row, int col, const char *command, const char *coding)
* to this process group ID (equal process ID).
*/
pgrp = getpid ();
setpgid (pgrp, pgrp);
tcsetpgrp (0, pgrp);
pgrp = getpid();
setpgid(pgrp, pgrp);
tcsetpgrp(0, pgrp);
/* mark the pipes as close on exec */
fcntl( cp_pipe[1], F_SETFD, 1);
@@ -405,7 +392,7 @@ spawn_shell (int row, int col, const char *command, const char *coding)
Setenv ("SHELL=", *args);
/*
* Print Welcome MuTerm World! Message.
* Print Welcome Message.
* (But, Only print message when MuTerminal coding is UTF8.)
*/
now_time_t = time(NULL);
@@ -430,7 +417,7 @@ spawn_shell (int row, int col, const char *command, const char *coding)
if (system (err_msg) == 0)
execl ("/bin/sh", "/bin/sh", "-login", NULL);
exit (1);
exit(1);
}
/*
@@ -439,8 +426,8 @@ spawn_shell (int row, int col, const char *command, const char *coding)
*/
/* close childs's side of the pipe */
close (cp_pipe[1]);
close (pc_pipe[0]);
close(cp_pipe[1]);
close(pc_pipe[0]);
/*
* close parent control tty.
@@ -455,7 +442,7 @@ spawn_shell (int row, int col, const char *command, const char *coding)
break;
case PTY_NG:
printf ("%s\n", handshake.msg);
printf("%s\n", handshake.msg);
done = -1;
break;
@@ -463,7 +450,7 @@ spawn_shell (int row, int col, const char *command, const char *coding)
handshake.row = row;
handshake.col = col;
handshake.status = PTY_WS;
write (pc_pipe[1], (char *)&handshake, sizeof (handshake));
write(pc_pipe[1], (char *)&handshake, sizeof (handshake));
break;
}
}
+1 -13
View File
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2003-4 Kian Duffy <[email protected]>
* Copyright (c) 2004 Daniel Furrer <[email protected]>
* Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
*
* Permission is hereby granted, free of charge, to any person obtaining
@@ -31,19 +32,6 @@
#ifndef SPAWN_H
#define SPAWN_H
/*
* allow for mobility of the pty master/slave directories
*/
#define PTYDEV "/dev/pt/%c%c"
#define TTYDEV "/dev/tt/%c%c"
#define PTYCHAR1 "pqrs"
#define PTYCHAR2 "0123456789abcdef"
#define TTYFORMAT "/dev/tt/%d"
#define PTYFORMAT "/dev/pt/%d"
#define MAXPTTYS 16 * 4
#ifndef CEOF