# start sh script in custom ttyv*



## OwnTech (Jul 29, 2009)

Hi,

How to start sh script in custom tty.
(Without editing /etc/ttys)


----------



## mk (Jul 29, 2009)

be more verbose on what you are after.


----------



## graudeejs (Jul 29, 2009)

you can't as far as i know....


----------



## OwnTech (Jul 29, 2009)

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 (Jul 29, 2009)

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.

```
#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);
        }
}
```


----------



## OwnTech (Jul 31, 2009)

my code. simple version with login_tty()

```
// 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;
}
```


----------

