Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Thu, 26 Sep 2013 14:59:39 -0400
From: Rich Felker <dalias@...ifal.cx>
To: musl@...ts.openwall.com
Subject: Re: GLOB_BRACE

On Thu, Sep 26, 2013 at 08:50:33PM +0200, Daniel Cegiełka wrote:
> @Rich
> 
> Referring to your description:
> 
> http://www.openwall.com/lists/musl/2012/06/10/23
> 
> "I have
> a 22-line (C) init program that does nothing but run the boot script
> and reap orphaned zombies, and a 34-line (C) program that repeatedly
> re-runs a program in a new session every time it exits. The latter,
> combined with a 14-line (shell script) getty program, is sufficient to
> handle all console logins.
> "
> 
> it's a bit more then 22 LOC :)

Yeah, yours does a few more things, and does so somewhat less
efficiently.

> static void
> sigchild(int sig)
> {
> 	while(waitpid(-1, NULL, WNOHANG) > 0);
> }

Not needed.

> static void
> openconsole(void)
> {
> 	int fd;
> 
> 	if((fd = open("/dev/console", O_RDWR|O_NOCTTY)) >= 0){
> 		dup2(fd, 0);
> 		dup2(fd, 1);
> 		dup2(fd, 2);
> 		if(fd > 2)
> 			close(fd);
> 	}
> }

Unclear what this is needed for. Normally init starts on the console
anyway, and if it doesn't, why would you want to force it to use the
console instead of whatever stdin/out/err it started with?

> int
> main(int argc, char **argv)
> {
> 	int i, fd, l;
> 	struct sigaction sa;
> 	pid_t t;
> 
> 	if(getpid() != 1)
> 		return 1;
> 	for(i = 0, l = 0; i < argc; i++)
> 		l = strlen(argv[i]) + 1;
> 	if(l > 1){
> 		memset(argv[0], 0, l);
> 		strncpy(argv[0], "init", l - 1);
> 	}
> 	for(i = 1; i < NSIG; i++)
> 		if(i != SIGCHLD)
> 			(void)signal(i, SIG_IGN);

Why are you ignoring signals rather than blocking them? It's more work
and harder to undo before running the rc script (and you don't seem to
be undoing it).

> 	reboot(RB_DISABLE_CAD);
> 	if((fd = open("/dev/console", O_RDWR|O_NOCTTY)) >= 0){
> 		ioctl(fd, KDSIGACCEPT, SIGWINCH);

What does this do?

> 	if(!fork()){
> 		setsid();
> 		openconsole();
> 		tcsetpgrp(0, getpgrp());
> 		execl("/bin/sh", "sh", "/etc/rc", NULL);
> 		while(waitpid(t, NULL, 0) != t);
> 	}
> 	sigemptyset(&sa.sa_mask);
> 	sa.sa_sigaction = 0;
> 	sa.sa_flags = SA_RESTART|SA_NOCLDSTOP;
> 	sa.sa_handler = sigchild;
> 	sigaction(SIGCHLD, &sa, 0);
> 	for(;;)
> 		pause();

Instead use:

	for (;;) wait(&status);

and you don't have to handle SIGCHLD.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.