summaryrefslogtreecommitdiffstats
path: root/app/core/gimp-spawn.c
blob: bfa08e832b1c3b846b42fc81451d0f2a6b510304 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimp-spawn.c
 * Copyright (C) 2018 Ell
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <glib-object.h>

#ifdef HAVE_VFORK
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#ifdef G_OS_WIN32
#include <windows.h>
#include <io.h>
#endif

#include "core-types.h"

#include "gimp-spawn.h"

#include "gimp-intl.h"


#ifdef HAVE_VFORK

/* copied from glib */
static gint
exec_err_to_g_error (gint en)
{
  switch (en)
    {
#ifdef EACCES
    case EACCES:
      return G_SPAWN_ERROR_ACCES;
      break;
#endif

#ifdef EPERM
    case EPERM:
      return G_SPAWN_ERROR_PERM;
      break;
#endif

#ifdef E2BIG
    case E2BIG:
      return G_SPAWN_ERROR_TOO_BIG;
      break;
#endif

#ifdef ENOEXEC
    case ENOEXEC:
      return G_SPAWN_ERROR_NOEXEC;
      break;
#endif

#ifdef ENAMETOOLONG
    case ENAMETOOLONG:
      return G_SPAWN_ERROR_NAMETOOLONG;
      break;
#endif

#ifdef ENOENT
    case ENOENT:
      return G_SPAWN_ERROR_NOENT;
      break;
#endif

#ifdef ENOMEM
    case ENOMEM:
      return G_SPAWN_ERROR_NOMEM;
      break;
#endif

#ifdef ENOTDIR
    case ENOTDIR:
      return G_SPAWN_ERROR_NOTDIR;
      break;
#endif

#ifdef ELOOP
    case ELOOP:
      return G_SPAWN_ERROR_LOOP;
      break;
#endif

#ifdef ETXTBUSY
    case ETXTBUSY:
      return G_SPAWN_ERROR_TXTBUSY;
      break;
#endif

#ifdef EIO
    case EIO:
      return G_SPAWN_ERROR_IO;
      break;
#endif

#ifdef ENFILE
    case ENFILE:
      return G_SPAWN_ERROR_NFILE;
      break;
#endif

#ifdef EMFILE
    case EMFILE:
      return G_SPAWN_ERROR_MFILE;
      break;
#endif

#ifdef EINVAL
    case EINVAL:
      return G_SPAWN_ERROR_INVAL;
      break;
#endif

#ifdef EISDIR
    case EISDIR:
      return G_SPAWN_ERROR_ISDIR;
      break;
#endif

#ifdef ELIBBAD
    case ELIBBAD:
      return G_SPAWN_ERROR_LIBBAD;
      break;
#endif

    default:
      return G_SPAWN_ERROR_FAILED;
      break;
    }
}

#endif /* HAVE_VFORK */

gboolean
gimp_spawn_async (gchar       **argv,
                  gchar       **envp,
                  GSpawnFlags   flags,
                  GPid         *child_pid,
                  GError      **error)
{
  g_return_val_if_fail (argv != NULL, FALSE);
  g_return_val_if_fail (argv[0] != NULL, FALSE);

#ifdef HAVE_VFORK
  if (flags == (G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
                G_SPAWN_DO_NOT_REAP_CHILD      |
                G_SPAWN_CHILD_INHERITS_STDIN))
    {
      pid_t pid;

      pid = vfork ();

      if (pid < 0)
        {
          gint errsv = errno;

          g_set_error (error,
                       G_SPAWN_ERROR,
                       G_SPAWN_ERROR_FORK,
                       _("Failed to fork (%s)"),
                       g_strerror (errsv));

          return FALSE;
        }
      else if (pid == 0)
        {
          if (envp)
            execve (argv[0], argv, envp);
          else
            execv (argv[0], argv);

          _exit (errno);
        }
      else
        {
          int   status = -1;
          pid_t result;

          result = waitpid (pid, &status, WNOHANG);

          if (result)
            {
              if (result < 0)
                {
                  g_warning ("waitpid() should not fail in "
                             "gimp_spawn_async()");
                }

              if (WIFEXITED (status))
                status = WEXITSTATUS (status);
              else
                status = -1;

              g_set_error (error,
                           G_SPAWN_ERROR,
                           exec_err_to_g_error (status),
                           _("Failed to execute child process “%s” (%s)"),
                           argv[0],
                           g_strerror (status));

              return FALSE;
            }

          if (child_pid) *child_pid = pid;

          return TRUE;
        }
    }
#endif /* HAVE_VFORK */

  return g_spawn_async (NULL, argv, envp, flags, NULL, NULL, child_pid, error);
}

void
gimp_spawn_set_cloexec (gint fd)
{
#if defined (G_OS_WIN32)
  SetHandleInformation ((HANDLE) _get_osfhandle (fd), HANDLE_FLAG_INHERIT, 0);
#elif defined (HAVE_FCNTL_H)
  fcntl (fd, F_SETFD, fcntl (fd, F_GETFD, 0) | FD_CLOEXEC);
#elif defined (__GNUC__)
#warning gimp_spawn_set_cloexec() is not implemented for the target platform
#endif
}