8000 minix/minix/commands/rshd/rshd.c at v3.3.0 · ziyw/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
445 lines (419 loc) · 8.56 KB

rshd.c

File metadata and controls

445 lines (419 loc) · 8.56 KB
{
/* Let's go to a different process group. */
new_pg= setsid();
pid= fork();
if (pid<0)
{
if (errno != EAGAIN)
{
fprintf(stderr, "%s: fork()= %d : %s\n",
prog_name, errno, strerror(errno));
}
printf("\1Try again.\n");
exit(1);
}
if (pid)
{
close(0); /* stdin */
close(1); /* stdout */
#if USEATTACH
close(err_fd); /* stderr for shell */
#endif
dup2(2,0);
dup2(2,1);
for (;;)
{
#if !USEATTACH
if (read(err_fd, &sig, 1) <= 0)
#else
if (read(err2_fd, &sig, 1) <= 0)
#endif
{
#if 0
printf("read failed: %d\n", errno);
#endif
exit(0);
}
pid= 0;
#if 0
printf("killing %d with %d\n", -new_pg, sig);
#endif
kill(-new_pg, sig);
}
}
#if USEATTACH
close(err2_fd); /* signal channel for parent */
#endif
result= pipe(pds);
if (result<0)
{
printf("\1Can't make pipe\n");
kill(getppid(), SIGTERM);
exit(1);
}
pid1= fork();
if (pid1<0)
{
if (errno != EAGAIN)
{
fprintf(stderr, "%s: fork()= %d : %s\n",
prog_name, errno, strerror(errno));
}
printf("\1Try again.\n");
kill(-new_pg, SIGTERM);
exit(1);
}
if (pid1)
{
close(pds[1]); /* write side of pipe */
for (;;)
{
result= read(pds[0], buffer, sizeof(buffer));
if (result<=0)
{
kill(pid, SIGTERM);
exit(0);
}
buff_ptr= buffer;
while (result>0)
{
result1= write (err_fd, buff_ptr,
result);
if (result1 <= 0)
{
fprintf(stderr,
"%s: write()= %d : %s\n",
prog_name, errno,
strerror(errno));
kill(-new_pg, SIGTERM);
exit(1);
}
result -= result1;
}
}
}
close(err_fd); /* file descriptor for error channel */
close (pds[0]); /* read side of pipe */
dup2(pds[1], 2);
close (pds[1]); /* write side of pipe */
}
if (*pwent->pw_shell == '\0')
pwent->pw_shell= "/bin/sh";
#if __minix_vmd
initgroups(pwent->pw_name, pwent->pw_gid);
#endif
setgid(pwent->pw_gid);
setuid(pwent->pw_uid);
TZ=getenv("TZ");
environ= envinit;
strncat(homedir, pwent->pw_dir, sizeof(homedir)-6);
strncat(shell, pwent->pw_shell, sizeof(shell)-7);
strncat(username, pwent->pw_name, sizeof(username)-6);
if (TZ)
strncat(tz, TZ, sizeof(tz)-4);
else
envinit[3]= NULL;
cp= strrchr(pwent->pw_shell, '/');
if (cp)
cp++;
else
cp= pwent->pw_shell;
if (!err_port)
dup2(1, 2);
write(1, "\0", 1);
execl(pwent->pw_shell, cp, "-c", cmdbuf, 0);
close(2);
open("/dev/tty", O_RDWR);
fprintf(stderr, "%s: execl(%s, %s, .., %s)= %d : %s\n", prog_name,
pwent->pw_shell, cp, cmdbuf, errno, strerror(errno));
kill(getppid(), SIGTERM);
exit(1);
}
void getstr(buf, cnt, err)
char *buf;
int cnt;
char *err;
{
char c;
do
{
if (read(0, &c, 1) != 1)
exit(1);
*buf++ = c;
if (--cnt == 0)
{
printf("\1%s too long", err);
exit(1);
}
} while (c != 0);
}
void close_on_exec(fd)
int fd;
{
(void) fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
}
0