/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <wait.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include "pkglib.h"
#include "pkglibmsgs.h"
#include "pkglocale.h"
/* global environment inherited by this process */
extern char **environ;
/* dstream.c */
extern int ds_curpartcnt;
/*
* global internal (private) variables
*/
/* received signal count - bumped with hooked signals are caught */
static int sig_received = 0;
/*
* Name: sig_trap
* Description: hooked up to signal counts number of signals received
* Arguments: a_signo - [RO, *RO] - (int)
* Integer representing the signal received; see signal(3c)
* Returns: <void>
*/
static void
{
sig_received++;
}
/*
* Name: pkgexecv
* Description: Asynchronously execute a package command in a separate process
* and return results - the subprocess MUST arm it's own SIGINT
* and SIGHUP signals and must return a standard package command
* exit code (see returns below)
* Only another package command (such as pkginstall, pkgremove,
* etc.) may be called via this interface. No files are closed
* because open files are passed across to certain commands using
* either implicit agreements between the two (yuk!) or by using
* the '-p' option which passes a string of digits, some of which
* represent open file descriptors passed through this interface!
* Arguments: filein - [RO, *RO] - (char *)
* Pointer to string representing the name of the file to
* use for the package commands's stdin
* == (char *)NULL or == "" - the current stdin
* is used for the new package command process
* fileout - [RO, *RO] - (char *)
* Pointer to string representing the name of the file to
* use for the package commands's stdout and stderr
* is used for the new package command process
* uname - [RO, *RO] - (char *)
* Pointer to string representing the user name to execute
* the package command as - the user name is looked up
* using the ncgrpw:cpwnam() interface
* == (char *)NULL or == "" - the user name of the current
* process is used for the new package command process
* gname - [RO, *RO] - (char *)
* Pointer to string representing the group name to execute
* the package command as - the group name is looked up
* using the ncgrpw:cgrnam() interface
* == (char *)NULL or == "" - the group name of the current
* process is used for the new package command process
* arg - [RO, *RO] - (char **)
* Pointer to array of character pointers representing the
* arguments to pass to the package command - the array is
* terminated with a pointer to (char *)NULL
* Returns: int
* == 99 - exec() of package command failed
* == -1 - fork failed or other fatal error during
* execution of the package command
* otherwise - exit code from package command:
* 0 - successful
* 1 - package operation failed (fatal error)
* 2 - non-fatal error (warning)
* 4 - admin settings prevented operation
* 5 - administration required and -n was specified
* IN addition:
* 10 is added to the return code if reboot after the
* installation of all packages is required
* 20 is added to the return code if immediate reboot
* after installation of this package is required
*/
int
{
int exit_no;
int n;
int status;
void (*funcSighup)();
void (*funcSigint)();
/* flush standard i/o before creating new process */
/*
* after the vfork() the parent and child need to setup their respective
* interrupt handling and release the hold on the signals
*/
sig_received = 0;
/*
* create new process to execute command in;
* vfork() is being used to avoid duplicating the parents
* memory space - this means that the child process may
* not modify any of the parents memory including the
* standard i/o descriptors - all the child can do is
* adjust interrupts and open files as a prelude to a
* call to exec().
*/
if (pid < 0) {
/*
* *************************************************************
* fork failed!
* *************************************************************
*/
/* release hold on signals */
return (-1);
}
if (pid > 0) {
/*
* *************************************************************
* This is the forking (parent) process
* *************************************************************
*/
/* close datastream if any portion read */
if (ds_curpartcnt >= 0) {
if (ds_close(0) != 0) {
/* kill child process */
/* release hold on signals */
return (-1);
}
}
/*
* setup signal handlers for SIGINT and SIGHUP and release hold
*/
/* hook SIGINT to sig_trap() */
} else {
}
/* hook SIGHUP to sig_trap() */
} else {
}
/* release hold on signals */
/*
* wait for the process to exit, reap child exit status
*/
for (;;) {
status = 0;
if (waitstat < 0) {
/* waitpid returned error */
/* try again */
continue;
}
continue;
}
/* error from waitpid: bail */
break;
/* child exit status available */
break;
}
}
/*
* reset signal handlers
*/
/* reset SIGINT */
/* reset SIGHUP */
/* error if child process does not match */
return (-1);
}
/*
* determine final exit code:
* - if signal received, then return interrupted (3)
* - if child exit status is available, return exit child status
* - otherwise return error (-1)
*/
if (sig_received != 0) {
} else {
}
return (exit_no);
}
/*
* *********************************************************************
* This is the forked (child) process
* *********************************************************************
*/
/* reset all signals to default */
for (n = 0; n < NSIG; n++) {
}
/* release hold on signals held by parent before fork() */
/*
* The caller wants to have stdin connected to filein.
*/
/*
*/
/*
* If stdin is connected to a tty device.
*/
if (isatty(STDIN_FILENO)) {
/*
*/
if (n >= 0) {
(void) dup2(n, STDIN_FILENO);
}
}
} else {
/*
* connect input to the requested file no questions.
*/
if (n >= 0) {
(void) dup2(n, STDIN_FILENO);
}
}
}
/*
* The caller wants to have stdout and stderr connected to fileout.
*/
/*
*/
/*
* If stdout is connected to a tty device.
*/
if (isatty(STDOUT_FILENO)) {
/*
*/
if (n >= 0) {
/*
* current standard output stream, and
*/
(void) dup2(n, STDOUT_FILENO);
}
}
/*
* not connected to tty device - probably redirect to
* file - preserve existing output device
*/
} else {
/*
* connect output to the requested file no questions.
*/
/* LINTED O_CREAT without O_EXCL specified in call to */
if (n >= 0) {
(void) dup2(n, STDOUT_FILENO);
}
}
/*
* Dup stderr from stdout.
*/
}
/*
* do NOT close all file descriptors except stdio
* file descriptors are passed in to some subcommands
* (see dstream:ds_getinfo() and dstream:ds_putinfo())
*/
}
}
}
}
/* execute target executable */
_exit(99);
/*NOTREACHED*/
}