[BusyBox] RFC: giving respawn init actions a controlling tty?

John van V. john_van_v at yahoo.com
Mon Sep 30 16:36:03 UTC 2002


Spawn command -- 

I like this idea.  I am trying to develop Initrds which are designed to obscure
the definition of a root disk as part of a pre-boot -- before the kernel knows
what init is or even knows that it has an /etc directory.  

The purpose is to allow complete interactive flexibility at the boot stage,
taking advantage of the pivot_root command to go anywhere it wants -- or just
exec a dedicated purpose.

The only difficutly is that execting bash produces a shell that doesnt react to
control-C.  Some of my initrds react to ctl-D though.

I have not been working on them for a couple weeks but I will make some
examples abvailble.


--- Claudio Fleiner <claudio at fleiner.com> wrote:
> How about a small applet that gets a controlling tty, say
> something called 'spawn', used as
> 
>         spawn /dev/vt03 sh -il
> 
> The following code does exactly that (it also expects the uid and gid
> under which the spawned shell should run so the full call for a root 
> shell is  
>         spawn /dev/vt03 0 0 sh -il
> ). It has been used in midori linux for quite a while. This applet can
> then be used to launch whatever program needs a controlling tty:
> 
> 
> 
> usage info:
> 
> #define spawn_trivial_usage \
>         "ttydevice [uid [gid [cmdline]]]\n"
> #define spawn_full_usage \
>         "spawn a terminal session on a device using the given uid / gid\nand
> executing the command given.\n\n" 
> 
> 
> 
> applet.c:
> 
> 
> /************************************************************************
>    spawn.c - gives an interactive root shell on specified tty device
> 
>    Copyright (C) 2000 Transmeta Corporation
> 
>    written by Nathan I. Laredo <nlaredo at transmeta.com>
> 
>    This program is modifiable/redistributable under the terms
>    of the GNU General Public Licence.
> 
>    You should have received a copy of the GNU General Public License
>    along with this program; if not, write to the Free Software
>    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
>  *************************************************************************/
> 
> #include "busybox.h"
> #include <termios.h>
> #include <stdio.h>
> #include <unistd.h>
> #include <signal.h>  
> #include <fcntl.h>
> #include <sys/ioctl.h>
> #include <sys/stat.h>
> #include <sys/types.h> 
> #include <sys/wait.h>   
> #include <stdlib.h>
> 
> int spawn_main(int argc, char **argv)
> {
> 	int fd, uid = 0, gid = 0,pid;
> 	struct stat st;
>         struct termios p;
> 
> 	if (argc < 2) 
> 		show_usage();
> 
> 	if(stat(argv[1], &st) < 0) {
> 		perror_msg_and_die("stat of %s failed", argv[1]);
> 	}
> 	if (!S_ISCHR(st.st_mode)) {
> 		fprintf(stderr, "%s is not a character device\n", argv[1]);
> 		exit(1);
> 	}
> 	if (argc > 2) {
> 		uid = atoi(argv[2]);
> 	}
> 	if (argc > 3) {
> 		gid = atoi(argv[3]);
> 	}
> 	umask(0);
> 	close(0);
> 	close(1);
> 	close(2);
> 	if ((pid=fork())) {
> 		int status;
> 		do { waitpid(pid,&status,0); } while(kill(pid,0)==0);
> 		exit(0);
> 	}
> 	if (setsid() < 0) {
> 		perror_msg_and_die("setsid failed");
> 	}
> 	if (chdir("/") < 0) {
> 		perror_msg_and_die("chdir failed");
> 	}
> 	if((fd = open(argv[1], O_RDWR)) < 0) {
> 		perror_msg_and_die("open of %s failed", argv[1]);
> 	}
> 	if (ioctl(fd, TIOCSCTTY, 0) < 0) {
> 		perror_msg_and_die("ioctl failed");
> 	}
> 	dup(fd);
> 	dup(fd);
> 	if (ioctl(fd, TIOCSCTTY, 1) < 0) {
> 		perror_msg_and_die("ioctl failed");
> 	}
> 
> 	setregid(gid, gid);
> 	setgroups(1, &gid);
> 	setreuid(uid, uid);
> 
>         tcgetattr(0,&p);
>         p.c_lflag|= ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE |
> IEXTEN;             
>         tcsetattr(0,TCSANOW,&p);
> 
> 	/* one of these should work */
> 	if (argc > 4) {	/* user specified command line, with path search */
> 		execvp(argv[4], argv + 4);
> 	}
> 	execl("/bin/bash", "/bin/bash", NULL);
> 	execl("/bin/ksh", "/bin/ksh", NULL);
> 	execl("/bin/sh", "/bin/sh", NULL);
> 	execl("/bin/ash", "/bin/ash", NULL);
> 	exit(1);
> }
> 
> 
> 
> ----------------------------------------------------------------------------
> Claudio Fleiner                                          claudio at fleiner.com
> 
> 
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://busybox.net/mailman/listinfo/busybox


=====
John van Vlaanderen--
      ##############################################################
      #   CXN, Inc. Contact:  john at thinman.com                     #
      #     President, The Linux Society                           #
      # http://groups.yahoo.com/group/thelinuxsociety              #
      # linux society distro -> http://www.thinman.com/eLSD/readme #
      ##############################################################

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com



More information about the busybox mailing list