- if (notty || record_file != NULL) {
- int cpid;
-
- cpid = fork();
- if (cpid == -1) {
- error("Can't fork process for character shunt: %m");
- goto fail;
- }
- if (cpid == 0) {
- close(pty_slave);
- setuid(uid);
- if (getuid() != uid)
- fatal("setuid failed");
- setgid(getgid());
- charshunt(ttyfd);
- exit(0);
+ if (ptycommand != NULL) {
+ if (record_file != NULL) {
+ int ipipe[2], opipe[2], ok;
+
+ if (pipe(ipipe) < 0 || pipe(opipe) < 0)
+ fatal("Couldn't create pipes for record option: %m");
+ ok = device_script(ptycommand, opipe[0], ipipe[1], 1) == 0
+ && start_charshunt(ipipe[0], opipe[1]);
+ close(ipipe[0]);
+ close(ipipe[1]);
+ close(opipe[0]);
+ close(opipe[1]);
+ if (!ok)
+ goto fail;
+ } else {
+ if (device_script(ptycommand, pty_master, pty_master, 1) < 0)
+ goto fail;
+ ttyfd = pty_slave;
+ close(pty_master);
+ pty_master = -1;