start sh script in custom ttyv*

OP
OP
O

OwnTech

New Member


Messages: 5

linux have openvt for this (openvt -c 9 script.sh)
i'm will try write program for start program on custom tty ( use system call : login_tty and execve )

--
I'm bad know english language. Sorry. :r
 

anemos

Member

Reaction score: 16
Messages: 58

I checked login_tty() but unfortunately, I can get neither login_tty() nor tcsetsid() working on my computer (7-Release). The compiler is complaining about some errors in /usr/include/libutil.h.
The commiter claims that the ioctl() way is non-standard, nevertheless, if I understood correctly, the following code does what you asked for.
Code:
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
        int d, s;
        pid_t pid;

        if ((d = open("/dev/ttyv8", O_RDWR)) < 0)
                exit(1);
        if (dup2(d, 0) < 0)
                exit(1);
        if ((pid = fork()) < 0)
                exit(1);
        else if (pid > 0)
                wait(s);
        else {
                if (setsid() < 0)
                        exit(1);
                if (ioctl(d, TIOCSCTTY) < 0)
                        exit(1);
                /* Replace /usr/bin/tty with your .sh script */
                execl("/usr/bin/tty", "/usr/bin/tty", (char *) 0);
                exit(1);
        }
}
 
OP
OP
O

OwnTech

New Member


Messages: 5

my code. simple version with login_tty()
Code:
// for compile : gcc -Wall -O2 -o opentty -lutil main.c
// usage : ./opentty /dev/ttyv8 /bin/bash
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <libutil.h>

int main(int argc, char *argv[])
{
        if(argc <= 2)
                exit(1);
        if(access(argv[1], W_OK))
                exit(2);
        pid_t pid = fork();
        if(pid == 0)
        {
                if(fork() == 0)
                {
                        if(setsid() < 0)
                                exit(1);
                        int fd = open(argv[1], O_RDWR);
                        if(fd < 0)
                                exit(2);
                        if(login_tty(fd) < 0)
                                exit(3);
                        execve(argv[2], &argv[2], NULL);
                        exit(4);
                }
        }
        return 0;
}
 
Top