// Filename:	sysexec
// Contents:	the object that encapsulates unusual system operations
// Author:	Greg Shaw
// Created:	5/18/94

/*
This file is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file with other programs, and to distribute
those programs without any restriction coming from the use of this
file.  (The General Public License restrictions do apply in other
respects; for example, they cover modification of the file, and
distribution when not linked into another program.)

This file is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifndef _SYSEXEC_C_
#define _SYSEXEC_C_

#include "bbshdr.h"

static int child_exited = 0;  // *(%(@%&#*(@* C/C++ and signals!
static int running_pid = 0;	// more *%(#*@(isms.

// Function:	catch_child
// Function:	catch the child dying signal so that the BBS knows to "wake up"
// Input:	sig - the signal that was caught
// Output:	will reset object if pid correct.
// Author:	Greg Shaw
// Notes:	this is a 'plain c' function, as that is what signal() wants.
//		GRRRR!  Wish I could remember that typecast that goes from
//		C++ to C!
// Created:	10/16/94

void catch_child(int sig)	// catch a child dying
{
	int		status;

	if (sig == SIGCHLD)
	{
		if (wait(&status) == running_pid)
		{
			child_exited = 0;
		}
	}
}


// Method:	constructor
// Function:	initialize the object
// Author:	Greg Shaw
// Created:	5/20/94

sysexec::sysexec()
{
	running_pid = 0;	// nothing running
	child_exited = 1;	// no child
}

// Method:	destructor
// Function:	clean up the object
// Author:	Greg Shaw
// Created:	5/20/94

sysexec::~sysexec()
{
	if (running_pid > 0)	// child running?
	{
		kill(running_pid,SIGHUP);
	}
	child_exited = 0;
}

// Method:	sysint
// Function:	serve as a system() command that correctly handles SIGHUP
// Input:	command - the command to execute
// Output:	(command is executed)
// Author:	Greg Shaw
// Notes:	I have to use printf() in this function to talk to the user
//		because bbsint (the next highest object, and the one which 
//		handles i/o) requires sysint to display a file.  
// Created:	5/18/94

int sysexec::sysint(char *command, time_t logoff_time, int quiet)
{
	int		status;
	sigset_t	mask;	// old signal mask
	time_t		now;	// current time
	int		warning;	// which warning?

	if (command == NULL)	// null command?
		return(1);	// error
	
	warning = 0;	// no warnings yet
	signal(SIGCHLD, &catch_child);
	child_exited = 1;
	sigprocmask(SIG_BLOCK, NULL, &mask); // get current blocked sigs mask
	sigaddset(&mask, SIGQUIT);
	sigprocmask(SIG_SETMASK, &mask, NULL);	 // set new mask
        if (!(running_pid = fork())) {


                setgid(getgid());
                setuid(getuid());

                execl("/bin/sh", "sh", "-rc", command, (char *) 0);
		ap_log("system: exec failure");
                exit(127);
        }
        if (running_pid == -1) {
		ap_log("system: exec failure");
	 	status = -1;
	}
	else
	{
		while (child_exited != 0)
		{
			sleep(10);	// just sleep for now
			time(&now);	// get date
			if (logoff_time != 0 && logoff_time - now <= 180)	// less than 3 minutes?
			{
				if (warning == 0 && !quiet)	// first warning
				{
					printf(EXPIRE3MINUTES);
					printf(PREPARETOLOGOFF);
					warning++;
				}
				else if (warning == 1 && logoff_time - now <= 60 && !quiet) 
				{
					printf(YOUWILLBEOFF);
					printf(PLEASELOGOFF);
					warning++;	
				}
				else if (warning > 1 && logoff_time - now < 0)	
				{
					// nuke him
					kill(running_pid,SIGHUP);
					return(NUKEHIM);
					ap_log("forced user logoff - time expired");
				}
			}
		}
		status = 0;
	}
	running_pid = 0;
	return(status);
	
}


#endif // _SYSEXEC_C_
