ptyの下のSSHはまだ端末からstdinをプロンプトします。

ptyの下のSSHはまだ端末からstdinをプロンプトします。

私は現在Cで疑似端末機能を学んでいます。 ptyのマスターとスレーブを作成できます。フォークの後、stdin、stdout、stderrをfdスレーブに設定しました。 exec 後も、子が stdin(fds) パスワードを読み取るのではなく、端末からパスワードを求めるメッセージを表示します。この動作を引き起こすプロセスが複製されると、ssh他のプログラムは期待どおりに動作します。

原因を見つける前に setsid(); ioctl(0, TIOCSCTTY, 1);子プロセスで使用するときにこの問題を解決できますか?これが予想される動作ですか?execvp

#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#define __USE_BSD
#include <termios.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <string.h>

int main(int ac, char *av[])
{
    int fdm, fds;
    int rc;
    char input[1024], output[150];

    // Check arguments
    if (ac <= 1)
    {
        fprintf(stderr, "Usage: %s program_name [parameters]\n", av[0]);
        exit(1);
    }

    fdm = posix_openpt(O_RDWR);
    if (fdm < 0)
    {
        fprintf(stderr, "Error %d on posix_openpt()\n", errno);
        return 1;
    }

    rc = grantpt(fdm);
    if (rc != 0)
    {
        fprintf(stderr, "Error %d on grantpt()\n", errno);
        return 1;
    }

    rc = unlockpt(fdm);
    if (rc != 0)
    {
        fprintf(stderr, "Error %d on unlockpt()\n", errno);
        return 1;
    }

    // Open the slave side ot the PTY
    fds = open(ptsname(fdm), O_RDWR);

    // Create the child process
    if (fork())
    {
        fd_set fd_in;

        // FATHER

        // Close the slave side of the PTY
        close(fds);
        int pass_entered = 0;
        while (1)
        {
            FD_ZERO(&fd_in);
            FD_SET(fdm, &fd_in);
            rc = select(fdm + 1, &fd_in, NULL, NULL, NULL);
            // If data on master side of PTY
            if (FD_ISSET(fdm, &fd_in))
            {
                rc = read(fdm, output, sizeof(input));
                if (rc > 0)
                {
                    // Send data on standard output
                    if (!pass_entered)
                    {
                        write(fdm, "password\n", 10);
                        pass_entered = 1;
                    }
                    write(2, "<<", 2);
                    write(2, output, rc);
                    write(2, ">>", 2);
                    int n = write(fdm, "id\n", 3);
                }
            }
        }
    }
    else
    {
        // CHILD

        // Close the master side of the PTY
        close(fdm);
        // The slave side of the PTY becomes the standard input and outputs of the child process
        close(0); // Close standard input (current terminal)
        close(1); // Close standard output (current terminal)
        close(2); // Close standard error (current terminal)

        dup2(fds, 0); // PTY becomes standard input (0)
        dup2(fds, 1); // PTY becomes standard output (1)
        dup2(fds, 2); // PTY becomes standard error (2)

        close(fds);
        // setsid();
        // ioctl(0, TIOCSCTTY, 1);

        {
            char **child_av;
            int i;

            // Build the command line
            child_av = (char **)malloc(ac * sizeof(char *));
            for (i = 1; i < ac; i++)
            {
                child_av[i - 1] = strdup(av[i]);
            }
            child_av[i - 1] = NULL;
            rc = execvp(child_av[0], child_av);
        }

        return 1;
    }
    return 0;
} // main

// gcc test_pty.c  && ./a.out python3 /tty_test.py //works
// gcc test_pty.c  && ./a.out su root              //works
// gcc test_pty.c  && ./a.out ssh user@localhost   //fails

ベストアンサー1

Unix環境の高度なプログラミング(APUE)は、ptyを設定するために使用される他のコードを示しています。いつもsetsid(2)新しいセッションを作成するにはaを実行します。また、FreeBSD(および他の* BSDシステム)は、TIOCSCTTY制御端末を設定するために呼び出す必要があると述べられています。

// from http://www.apuebook.com/src.3e.tar.gz -- lib/ptyfork.c
        if ((pid = fork()) < 0) {
                return(-1);
        } else if (pid == 0) {          /* child */
                if (setsid() < 0)
                        err_sys("setsid error");

                /*
                 * System V acquires controlling terminal on open().
                 */
                if ((fds = ptys_open(pts_name)) < 0)
                        err_sys("can't open slave pty");
                close(fdm);             /* all done with master in child */

#if     defined(BSD)
                /*
                 * TIOCSCTTY is the BSD way to acquire a controlling terminal.
                 */
                if (ioctl(fds, TIOCSCTTY, (char *)0) < 0)
                        err_sys("TIOCSCTTY error");
#endif

サンプルコードに加えて、ptym_openAPUEコードがいつ何をするのかを正確に理解するには、コードを見つけるために深く掘り下げる必要があります。ptys_openpty/main.c

サブ実行コードは不必要に複雑に見えます。

    {
        char **child_av;
        int i;

        // Build the command line
        child_av = (char **)malloc(ac * sizeof(char *));
        for (i = 1; i < ac; i++)
        {
            child_av[i - 1] = strdup(av[i]);
        }
        child_av[i - 1] = NULL;
        rc = execvp(child_av[0], child_av);
    }

次に置き換えることができます

av++;
execvp(av[0], av);

既存のパラメータを繰り返すのではなく、直接使用してください。あるいは、APUEは

execvp(argv[optind], &argv[optind]);

彼らはオプションを処理したからですgetopt

おすすめ記事