summaryrefslogtreecommitdiff
path: root/csvncgi/system.c
blob: afab9a7b0a987d9596a6037ddccd28563d3c626e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <error.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <unistd.h>

#include <defs.h>

#include <strbuf.h>
#include <system.h>


#define SYSTEM_ERRMSG_SIZE 4096

void system_error( const char *fmt, ... )
{
  va_list arg_ptr;
  char  buf[SYSTEM_ERRMSG_SIZE];
  char  msg[SYSTEM_ERRMSG_SIZE];
  char *format = "%s: %s\n";

  va_start( arg_ptr, fmt );

  vsnprintf( msg, SYSTEM_ERRMSG_SIZE, (const void *)fmt, arg_ptr );

  va_end( arg_ptr ); /* Reset variable arguments. */

  snprintf( buf, SYSTEM_ERRMSG_SIZE, format, "system", msg );

  (void)write( STDERR_FILENO, buf, strlen( buf ) );

  exit( 1 );
}

system_errfunc system_fatal = system_error;


static void xexec( const char *cmd )
{
  char *argv[4];
  const char *shell = getenv ("SHELL");

  if( !shell ) shell = "/bin/sh";

  argv[0] = (char *) shell;
  argv[1] = (char *) "-c";
  argv[2] = (char *) cmd;
  argv[3] = NULL;

  execv( shell, argv );

  /******************************************
    xexec() is called by child process, and
    here child process faced to FATAL error:
   */
  system_fatal( "Cannot exec '%s'\n", cmd );
}

static pid_t xfork( void )
{
  pid_t p = fork();

  if( p == (pid_t) -1 )
  {
    system_fatal( "Cannot %s: %s\n", "fork()", strerror( errno ) );
  }

  return p;
}

pid_t sys_exec_command( struct strbuf *sb, const char *cmd )
{
  int pipe_fh[2];

  pid_t pid = -1;

  if( sb )
  {
    if( pipe(pipe_fh ) == -1 )
    {
      system_fatal( "Cannot create pipe: %s\n", strerror( errno ) );
    }
  }

  pid = xfork();

  if( pid != 0 )
  {
    if( sb )
    {
      ssize_t ret;

      close(pipe_fh[1]);
      ret = strbuf_read( sb, pipe_fh[0], 1024 );
      if( ret < 0 )
        system_fatal( "Cannot read pipe: %s\n", strerror( errno ) );
      close(pipe_fh[0]);
    }

    return pid;
  }

  if( sb )
  {
    dup2(pipe_fh[1], STDOUT_FILENO);
    close(pipe_fh[1]);
    close(pipe_fh[0]);
  }

  xexec( cmd );
  return pid; /* only to avoid compilaton warning */
}

/*****************************************************************
  sys_wait_command() - Wait for pid.

  Return values:
  -------------
     0  - SUCCESS
   >=1  - status returned by child process
    -1  - Child terminated on signal
    -2  - Child terminated on unknown reason
    -3  - Cannot waitpid: waitpid() retusrs -1

     Error message with SIZE length saved into *ERRMSG buffer.
 *****************************************************************/
int sys_wait_command( pid_t pid, struct strbuf *errmsg )
{
  int status;

  if( pid < 0 ) return (pid_t) -1;

  while( waitpid( pid, &status, 0 ) == -1 )
    if( errno != EINTR )
    {
      if( errmsg ) {
        strbuf_addf( errmsg, "PID %lu: Cannot %s", (unsigned long)pid, "waitpid" );
      }
      return (int) -3;
    }

  if( WIFEXITED( status ) )
  {
    if( WEXITSTATUS (status) )
    {
      if( errmsg ) {
        strbuf_addf( errmsg, "PID %lu: Child returned status %d", (unsigned long)pid, WEXITSTATUS( status ) );
      }
      return (int) WEXITSTATUS( status );
    }
  }
  else if( WIFSIGNALED( status ) )
  {
    if( errmsg ) {
      strbuf_addf( errmsg, "PID %lu: Child terminated on signal %d", (unsigned long)pid, WTERMSIG( status ) );
    }
    return (int) -1;
  }
  else
  {
    if( errmsg ) {
      strbuf_addf( errmsg, "PID %lu: Child terminated on unknown reason", (unsigned long)pid );
    }
    return (int) -2;
  }

  return 0;
}