I have a shell, where I switch to another user with su -l anotheruser
, this mostly works
But when there is no pty allocated to su
, it errors out with:
su: must be run from a terminal
So, I tried to wrap su
with code to create a pty:
#include <stdio.h>
#include <string.h>
#include <pty.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
int fd;
char* args[] = {"/bin/su", "-l", "anotheruser", NULL };
char name[16] = {0};
int pid ;
pid = forkpty(&fd, name, NULL, NULL);
if (pid != -1) {
printf("n pty: [%s]n", name);
if (execve(args[0], args, NULL) == -1) {
printf("n execve: failedn");
}
} else {
printf("n forkpty: failed n");
}
return 0;
}
But this doesn’t seem to work..
I am missing something here, but not able to make out.. do I have to
redirect stdin/stdout
before execve()
?