- 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:
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2003-4 Kian Duffy <[email protected]>
|
* 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.
|
* Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
@@ -28,6 +29,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <dirent.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <sys/file.h>
|
#include <sys/file.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -56,7 +58,7 @@ extern char **environ;
|
|||||||
|
|
||||||
char spawn_alert_msg [] = \
|
char spawn_alert_msg [] = \
|
||||||
"alert --stop " \
|
"alert --stop " \
|
||||||
"'MuTerminal Error!!\n\
|
"'Haiku Terminal Error!!\n\
|
||||||
Cannot execute shell [%s].\n\
|
Cannot execute shell [%s].\n\
|
||||||
Check \"Shell\" in preferance panel.' "\
|
Check \"Shell\" in preferance panel.' "\
|
||||||
"'Exec /bin/sh' 'Abort'";
|
"'Exec /bin/sh' 'Abort'";
|
||||||
@@ -118,15 +120,12 @@ typedef struct
|
|||||||
/* global varriables */
|
/* global varriables */
|
||||||
|
|
||||||
pid_t sh_pid;
|
pid_t sh_pid;
|
||||||
char tty_name[16];
|
char tty_name[B_PATH_NAME_LENGTH];
|
||||||
int pfd_num = 0;
|
|
||||||
|
|
||||||
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;
|
int done = 0;
|
||||||
char *s;
|
|
||||||
char *t = 0;
|
|
||||||
pid_t pgrp;
|
pid_t pgrp;
|
||||||
|
|
||||||
int master = 0;
|
int master = 0;
|
||||||
@@ -136,8 +135,6 @@ spawn_shell (int row, int col, const char *command, const char *coding)
|
|||||||
|
|
||||||
handshake_t handshake;
|
handshake_t handshake;
|
||||||
|
|
||||||
char pty_name[16];
|
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
time_t now_time_t;
|
time_t now_time_t;
|
||||||
struct tm *now_time;
|
struct tm *now_time;
|
||||||
@@ -149,37 +146,39 @@ spawn_shell (int row, int col, const char *command, const char *coding)
|
|||||||
|
|
||||||
char err_msg[256];
|
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
|
* Get a psuedo-tty. We do this by cycling through files in the
|
||||||
* names. 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 successed.
|
* which is already in use, so we simply go until the open succeeds.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for (s = PTYCHAR1; *s != 0; s++) {
|
DIR *dir;
|
||||||
for (t = PTYCHAR2; *t != 0; t++) {
|
dir = opendir("/dev/pt/");
|
||||||
sprintf (pty_name, PTYDEV, *s, *t);
|
|
||||||
if ((master = open (pty_name, O_RDWR)) >= 0)
|
struct dirent *dir_entry;
|
||||||
goto out;
|
char pty_name[B_PATH_NAME_LENGTH];
|
||||||
pfd_num++;
|
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 master is still < 0 then we haven't found a tty we can use
|
||||||
/*
|
if (master < 0)
|
||||||
* If s and t are NULL, we ran out of pseudo ttys before we found
|
{
|
||||||
* one we can use
|
printf("didn't find any available pesudo ttys.");
|
||||||
*/
|
|
||||||
if ((*s == 0) && (*t == 0)) {
|
|
||||||
printf ("don't found pesudo ttys.");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Change "/dev/pt/XX (master) to "/dev/tt/XX" (slave). */
|
// Set the tty that corresponds to the pty we found
|
||||||
|
sprintf (tty_name, "/dev/tt/%s", dir_entry->d_name);
|
||||||
sprintf (pty_name,"%c%c", *s, *t);
|
|
||||||
sprintf (tty_name, TTYDEV, *s, *t);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get the modes of the current terminal. We will duplicates these
|
* 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;
|
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. */
|
/* Fork a child process. */
|
||||||
if ((sh_pid=fork()) < 0) {
|
if ((sh_pid = fork()) < 0) {
|
||||||
close (master);
|
close(master);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,25 +203,20 @@ spawn_shell (int row, int col, const char *command, const char *coding)
|
|||||||
* Now in child process.
|
* 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
|
* Make our controlling tty the pseudo tty. This hapens because
|
||||||
* we cleared our original controlling terminal above.
|
* we cleared our original controlling terminal above.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
close (cp_pipe[0]);
|
close(cp_pipe[0]);
|
||||||
close (pc_pipe[1]);
|
close(pc_pipe[1]);
|
||||||
|
|
||||||
/* Set process session leader */
|
/* Set process session leader */
|
||||||
if ((pgrp = setsid()) < 0) {
|
if ((pgrp = 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 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).
|
* to this process group ID (equal process ID).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pgrp = getpid ();
|
pgrp = getpid();
|
||||||
setpgid (pgrp, pgrp);
|
setpgid(pgrp, pgrp);
|
||||||
tcsetpgrp (0, pgrp);
|
tcsetpgrp(0, pgrp);
|
||||||
|
|
||||||
/* 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, 1);
|
||||||
@@ -405,7 +392,7 @@ spawn_shell (int row, int col, const char *command, const char *coding)
|
|||||||
Setenv ("SHELL=", *args);
|
Setenv ("SHELL=", *args);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Print Welcome MuTerm World! 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_t = time(NULL);
|
||||||
@@ -430,7 +417,7 @@ spawn_shell (int row, int col, const char *command, const char *coding)
|
|||||||
if (system (err_msg) == 0)
|
if (system (err_msg) == 0)
|
||||||
execl ("/bin/sh", "/bin/sh", "-login", NULL);
|
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 childs's side of the pipe */
|
||||||
close (cp_pipe[1]);
|
close(cp_pipe[1]);
|
||||||
close (pc_pipe[0]);
|
close(pc_pipe[0]);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* close parent control tty.
|
* close parent control tty.
|
||||||
@@ -455,7 +442,7 @@ spawn_shell (int row, int col, const char *command, const char *coding)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PTY_NG:
|
case PTY_NG:
|
||||||
printf ("%s\n", handshake.msg);
|
printf("%s\n", handshake.msg);
|
||||||
done = -1;
|
done = -1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -463,7 +450,7 @@ spawn_shell (int row, int col, const char *command, const char *coding)
|
|||||||
handshake.row = row;
|
handshake.row = row;
|
||||||
handshake.col = col;
|
handshake.col = col;
|
||||||
handshake.status = PTY_WS;
|
handshake.status = PTY_WS;
|
||||||
write (pc_pipe[1], (char *)&handshake, sizeof (handshake));
|
write(pc_pipe[1], (char *)&handshake, sizeof (handshake));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2003-4 Kian Duffy <[email protected]>
|
* 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.
|
* Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
@@ -31,19 +32,6 @@
|
|||||||
#ifndef SPAWN_H
|
#ifndef SPAWN_H
|
||||||
#define 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
|
#define MAXPTTYS 16 * 4
|
||||||
|
|
||||||
#ifndef CEOF
|
#ifndef CEOF
|
||||||
|
|||||||
Reference in New Issue
Block a user