if (!pty) {
printf("ERROR: -p required\n"); exit(-1);
}
pid = fork();
if (pid == -1) {
printf("ERROR: fork failed\n"); exit(1);
}
int pty_fd = open(pty, O_RDWR);
if (pid == 0) { int saved_stdout = -1; int saved_errno;
// Remember stdout so that if exec fails we can still output. // Mark the remembered fd as close-on-exec so that it's not // inherited past the exec.
saved_stdout = fcntl(1, F_DUPFD, 3);
fcntl(saved_stdout, F_SETFD, FD_CLOEXEC);
// If we don't do this ^C/^Z etc won't work. // We used to use 'setpgrp()' but it was only effective on Solaris // and not Linux. 'setsid()' seems to work for both. if (setsid() == -1) {
printf("ERROR setsid() failed -- %s\n", strerror(errno)); exit(-1);
}
if (pty_fd == -1) {
printf("ERROR cannot open pty \"%s\" -- %s\n",
pty, strerror(errno)); exit(-1);
}
// setsid() leaves us w/o a controlling terminal. // On Linux and Solaris the first open makes whatever we opened // our controlling terminal. // On BSD/Mac we need to explicitly assign a controlling terminal // using TIOCSCTTY. It does no harm on Linux either.
#ifdefined(TIOCSCTTY) if (ioctl(pty_fd, TIOCSCTTY, 0) == -1) {
printf("ERROR ioctl(TIOCSCTTY) failed on \"%s\" -- %s\n",
pty, strerror(errno)); exit(-1);
} #endif
// redirect stdio through the pty
dup2(pty_fd, 0);
dup2(pty_fd, 1);
dup2(pty_fd, 2);
close(pty_fd);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.