[ale]The ways of unix

Chris Fowler cfowler at outpostsentinel.com
Mon Nov 26 12:08:14 EST 2001


I am creating a simple autologin type command and want to be sure I have grasped 
the correct way to handle such events.  It appears that without th catchSig() any 
death of autologin will allow bash to hang around.  I want this sw to behave just
like a simple getty with no login authentication.  Am I doing it correctly?  Do
I need autologin to become a process group leader?

SOURCE:
/* autologin.c */



#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <asm/ioctls.h>
#include <signal.h>
#include "config.h"
#include "ops.h"


static pid_t	pid;

void
autoSig(int signum)
{

	switch(signum)
	{
		case SIGHUP:
			kill(pid, SIGHUP);
			exit(0);
			break;
	}

}
	


int
autologin(int argc, char *argv[])
{
	
	int fd;
	struct uentry *pwd;
	char *argv2[2];
	extern char **environ;
	int status;
	char pname[64];

	
	if(argc != 3)
	{
		printf("autologin [tty] [username] \n");
		exit(1);
	}

	
	pwd = getuser(argv[2]);
	if(pwd == NULL)
	{	
		fprintf(stderr,"user %s does not exist\n", argv[2]);
		exit(1);
	}

	argv2[0] = "/bin/bash";
	argv2[1] = '\0';

	sprintf(pname, "/dev/%s", argv[1]);

	signal(SIGHUP, autoSig);

	pid = fork();

	switch(pid)
	{
		case -1:
			err_quit("fork");
			break;
		case 0:
			close(0);
			close(1);

			if((fd = open(pname, O_RDWR)) == -1)
				err_quit("open: %s", argv[1]);

			close(2);
			setsid();

			ioctl(fd, TIOCSCTTY, NULL);

			
		
			setterm(fd);

			dup2(fd, 0);
			dup2(fd, 1);
			dup2(fd, 2);

			setgid((pwd->status & ISADMIN) ? 0:1);
			setuid((pwd->status & ISADMIN) ? 0:1);
			
			chdir("/");

			execve(argv2[0], argv2, environ);
			err_quit("exec %s", argv2[0]);
			break;
	}

	
	wait(&status);	


	return 0;
}






---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list