5 kx
5 kx /**********************************************************************
5 kx
5 kx Copyright 2019 Andrey V.Kosteltsev
5 kx
9 kx Licensed under the Radix cross Linux License, Version 1.0 .
5 kx you may not use this file except in compliance with the License.
5 kx You may obtain a copy of the License at
5 kx
9 kx https://radix-linux.su/licenses/LICENSE-1.0-en_US.txt
5 kx
5 kx Unless required by applicable law or agreed to in writing, software
5 kx distributed under the License is distributed on an "AS IS" BASIS,
5 kx WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
5 kx implied.
5 kx
5 kx **********************************************************************/
5 kx
5 kx #include <config.h>
5 kx
5 kx #include <stdlib.h>
5 kx #include <stdio.h>
5 kx #include <string.h>
5 kx #include <errno.h>
5 kx #include <error.h>
5 kx #include <sys/stat.h>
5 kx #include <sys/wait.h>
5 kx #include <stdarg.h>
5 kx #include <unistd.h>
5 kx
5 kx #include <msglog.h>
5 kx
5 kx
5 kx static void xexec( const char *cmd )
5 kx {
5 kx char *argv[4];
5 kx const char *shell = getenv ("SHELL");
5 kx
5 kx if( !shell ) shell = "/bin/sh";
5 kx
5 kx argv[0] = (char *) shell;
5 kx argv[1] = (char *) "-c";
5 kx argv[2] = (char *) cmd;
5 kx argv[3] = NULL;
5 kx
5 kx execv( shell, argv );
5 kx
5 kx /******************************************
5 kx xexec() is called by child process, and
5 kx here child process faced to FATAL error:
5 kx */
5 kx logmsg( errlog, MSG_FATAL, "%s: Cannot exec", cmd );
5 kx exit( EXIT_FAILURE );
5 kx }
5 kx
5 kx static pid_t xfork( void )
5 kx {
5 kx pid_t p = fork();
5 kx
5 kx if( p == (pid_t) -1 )
5 kx {
5 kx FATAL_ERROR( "Cannot %s", "fork" );
5 kx }
5 kx
5 kx return p;
5 kx }
5 kx
5 kx pid_t sys_exec_command( const char *cmd )
5 kx {
5 kx pid_t pid = xfork();
5 kx
5 kx if( pid != 0 )
5 kx {
5 kx return pid;
5 kx }
5 kx
5 kx xexec( cmd );
5 kx return pid; /* only to avoid compilaton warning */
5 kx }
5 kx
5 kx
5 kx /*****************************************************************
5 kx sys_wait_command() - Wait for pid.
5 kx
5 kx Return values:
5 kx -------------
5 kx 0 - SUCCESS
5 kx >=1 - status returned by child process
5 kx -1 - Child terminated on signal
5 kx -2 - Child terminated on unknown reason
5 kx -3 - Cannot waitpid: waitpid() retusrs -1
5 kx
5 kx Error message with SIZE length saved into *ERRMSG buffer.
5 kx *****************************************************************/
5 kx int sys_wait_command( pid_t pid, char *errmsg, size_t size )
5 kx {
5 kx int status;
5 kx
5 kx if( pid < 0 ) return (pid_t) -1;
5 kx
5 kx while( waitpid( pid, &status, 0 ) == -1 )
5 kx if( errno != EINTR )
5 kx {
5 kx if( errmsg && size ) {
5 kx (void)snprintf( errmsg, size, "PID %lu: Cannot %s", (unsigned long)pid, "waitpid" );
5 kx }
5 kx return (int) -3;
5 kx }
5 kx
5 kx if( WIFEXITED( status ) )
5 kx {
5 kx if( WEXITSTATUS (status) )
5 kx {
5 kx if( errmsg && size ) {
5 kx (void)snprintf( errmsg, size, "PID %lu: Child returned status %d", (unsigned long)pid, WEXITSTATUS( status ) );
5 kx }
5 kx return (int) WEXITSTATUS( status );
5 kx }
5 kx }
5 kx else if( WIFSIGNALED( status ) )
5 kx {
5 kx if( errmsg && size ) {
5 kx (void)snprintf( errmsg, size, "PID %lu: Child terminated on signal %d", (unsigned long)pid, WTERMSIG( status ) );
5 kx }
5 kx return (int) -1;
5 kx }
5 kx else
5 kx {
5 kx if( errmsg && size ) {
5 kx (void)snprintf( errmsg, size, "PID %lu: Child terminated on unknown reason", (unsigned long)pid );
5 kx }
5 kx return (int) -2;
5 kx }
5 kx
5 kx return 0;
5 kx }