2003-06-05 07:14:21 +00:00
|
|
|
/* exec.c - generic call-a-program code
|
2006-04-19 11:26:11 +00:00
|
|
|
* Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
|
2003-06-05 07:14:21 +00:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 19:49:40 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 07:14:21 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG 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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-06-05 07:14:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
2014-03-07 09:46:44 +01:00
|
|
|
# ifdef HAVE_WINSOCK2_H
|
|
|
|
# include <winsock2.h>
|
|
|
|
# endif
|
|
|
|
# include <windows.h>
|
2003-06-05 07:14:21 +00:00
|
|
|
#endif
|
|
|
|
#include <string.h>
|
2006-04-19 11:26:11 +00:00
|
|
|
|
|
|
|
#include "gpg.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
#include "options.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/i18n.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
#include "exec.h"
|
|
|
|
|
|
|
|
#ifdef NO_EXEC
|
2019-07-25 11:21:58 +09:00
|
|
|
int set_exec_path(const char *path) { return GPG_ERR_GENERAL; }
|
|
|
|
#else
|
2003-09-23 17:48:33 +00:00
|
|
|
#if defined (_WIN32)
|
2003-06-05 07:14:21 +00:00
|
|
|
/* This is a nicer system() for windows that waits for programs to
|
|
|
|
return before returning control to the caller. I hate helpful
|
|
|
|
computers. */
|
2019-07-25 11:21:58 +09:00
|
|
|
int
|
2009-07-13 17:36:02 +00:00
|
|
|
w32_system(const char *command)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2019-05-17 12:31:11 +02:00
|
|
|
if (!strncmp (command, "!ShellExecute ", 14))
|
|
|
|
{
|
|
|
|
SHELLEXECUTEINFOW see;
|
|
|
|
wchar_t *wname;
|
|
|
|
int waitms;
|
|
|
|
|
|
|
|
command = command + 14;
|
|
|
|
while (spacep (command))
|
|
|
|
command++;
|
|
|
|
waitms = atoi (command);
|
|
|
|
if (waitms < 0)
|
|
|
|
waitms = 0;
|
|
|
|
else if (waitms > 60*1000)
|
|
|
|
waitms = 60000;
|
|
|
|
while (*command && !spacep (command))
|
|
|
|
command++;
|
|
|
|
while (spacep (command))
|
|
|
|
command++;
|
|
|
|
|
|
|
|
wname = utf8_to_wchar (command);
|
|
|
|
if (!wname)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset (&see, 0, sizeof see);
|
|
|
|
see.cbSize = sizeof see;
|
|
|
|
see.fMask = (SEE_MASK_NOCLOSEPROCESS
|
|
|
|
| SEE_MASK_NOASYNC
|
|
|
|
| SEE_MASK_FLAG_NO_UI
|
|
|
|
| SEE_MASK_NO_CONSOLE);
|
|
|
|
see.lpVerb = L"open";
|
|
|
|
see.lpFile = (LPCWSTR)wname;
|
|
|
|
see.nShow = SW_SHOW;
|
|
|
|
|
|
|
|
if (DBG_EXTPROG)
|
|
|
|
log_debug ("running ShellExecuteEx(open,'%s')\n", command);
|
|
|
|
if (!ShellExecuteExW (&see))
|
|
|
|
{
|
|
|
|
if (DBG_EXTPROG)
|
|
|
|
log_debug ("ShellExecuteEx failed: rc=%d\n", (int)GetLastError ());
|
|
|
|
xfree (wname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (DBG_EXTPROG)
|
|
|
|
log_debug ("ShellExecuteEx succeeded (hProcess=%p,hInstApp=%d)\n",
|
|
|
|
see.hProcess, (int)see.hInstApp);
|
|
|
|
|
|
|
|
if (!see.hProcess)
|
|
|
|
{
|
|
|
|
gnupg_usleep (waitms*1000);
|
|
|
|
if (DBG_EXTPROG)
|
|
|
|
log_debug ("ShellExecuteEx ready (wait=%dms)\n", waitms);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WaitForSingleObject (see.hProcess, INFINITE);
|
|
|
|
if (DBG_EXTPROG)
|
|
|
|
log_debug ("ShellExecuteEx ready\n");
|
|
|
|
}
|
|
|
|
CloseHandle (see.hProcess);
|
|
|
|
|
|
|
|
xfree (wname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *string;
|
2021-03-08 21:26:16 +01:00
|
|
|
wchar_t *wstring;
|
2019-05-17 12:31:11 +02:00
|
|
|
PROCESS_INFORMATION pi;
|
2021-03-08 21:26:16 +01:00
|
|
|
STARTUPINFOW si;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2019-05-17 12:31:11 +02:00
|
|
|
/* We must use a copy of the command as CreateProcess modifies
|
|
|
|
* this argument. */
|
|
|
|
string = xstrdup (command);
|
2021-03-08 21:26:16 +01:00
|
|
|
wstring = utf8_to_wchar (string);
|
|
|
|
xfree (string);
|
|
|
|
if (!wstring)
|
|
|
|
return -1;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2019-05-17 12:31:11 +02:00
|
|
|
memset (&pi, 0, sizeof(pi));
|
|
|
|
memset (&si, 0, sizeof(si));
|
|
|
|
si.cb = sizeof (si);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2021-03-08 21:26:16 +01:00
|
|
|
if (!CreateProcessW (NULL, wstring, NULL, NULL, FALSE,
|
|
|
|
DETACHED_PROCESS,
|
|
|
|
NULL, NULL, &si, &pi))
|
|
|
|
{
|
|
|
|
xfree (wstring);
|
|
|
|
return -1;
|
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2019-05-17 12:31:11 +02:00
|
|
|
/* Wait for the child to exit */
|
|
|
|
WaitForSingleObject (pi.hProcess, INFINITE);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2019-05-17 12:31:11 +02:00
|
|
|
CloseHandle (pi.hProcess);
|
|
|
|
CloseHandle (pi.hThread);
|
2021-03-08 21:26:16 +01:00
|
|
|
xfree (wstring);
|
2019-05-17 12:31:11 +02:00
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-17 12:31:11 +02:00
|
|
|
#endif /*_W32*/
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
/* Replaces current $PATH */
|
2011-02-04 12:57:53 +01:00
|
|
|
int
|
2009-07-13 17:36:02 +00:00
|
|
|
set_exec_path(const char *path)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2006-04-19 11:26:11 +00:00
|
|
|
char *p;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
p=xmalloc(5+strlen(path)+1);
|
2003-06-05 07:14:21 +00:00
|
|
|
strcpy(p,"PATH=");
|
|
|
|
strcat(p,path);
|
|
|
|
|
|
|
|
if(DBG_EXTPROG)
|
2006-04-19 11:26:11 +00:00
|
|
|
log_debug("set_exec_path: %s\n",p);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
/* Notice that path is never freed. That is intentional due to the
|
|
|
|
way putenv() works. This leaks a few bytes if we call
|
|
|
|
set_exec_path multiple times. */
|
|
|
|
|
|
|
|
if(putenv(p)!=0)
|
2015-01-22 12:06:11 +01:00
|
|
|
return GPG_ERR_GENERAL;
|
2003-06-05 07:14:21 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* ! NO_EXEC */
|