summaryrefslogtreecommitdiff
path: root/src/common/exec.c
diff options
context:
space:
mode:
authorMichael Paquier2020-02-19 04:20:33 +0000
committerMichael Paquier2020-02-19 04:20:33 +0000
commite2e02191e23379502a38a6b0436ab7f41b2efc08 (patch)
tree081dc28955ea0a5000d4bd7029bdb608289c0c67 /src/common/exec.c
parente3ff789acfb2754cd7b5e87f6f4463fd08e35996 (diff)
Clean up some code, comments and docs referring to Windows 2000 and older
This fixes and updates a couple of comments related to outdated Windows versions. Particularly, src/common/exec.c had a fallback implementation to read a file's line from a pipe because stdin/stdout/stderr does not exist in Windows 2000 that is removed to simplify src/common/ as there are unlikely versions of Postgres running on such platforms. Author: Michael Paquier Reviewed-by: Kyotaro Horiguchi, Juan José Santamaría Flecha Discussion: https://postgr.es/m/20191219021526.GC4202@paquier.xyz
Diffstat (limited to 'src/common/exec.c')
-rw-r--r--src/common/exec.c132
1 files changed, 1 insertions, 131 deletions
diff --git a/src/common/exec.c b/src/common/exec.c
index 359442cf0c9..88400daa471 100644
--- a/src/common/exec.c
+++ b/src/common/exec.c
@@ -354,17 +354,11 @@ find_other_exec(const char *argv0, const char *target,
/*
- * The runtime library's popen() on win32 does not work when being
- * called from a service when running on windows <= 2000, because
- * there is no stdin/stdout/stderr.
- *
- * Executing a command in a pipe and reading the first line from it
- * is all we need.
+ * Execute a command in a pipe and read the first line from it.
*/
static char *
pipe_read_line(char *cmd, char *line, int maxsize)
{
-#ifndef WIN32
FILE *pgver;
/* flush output buffers in case popen does not... */
@@ -393,130 +387,6 @@ pipe_read_line(char *cmd, char *line, int maxsize)
return NULL;
return line;
-#else /* WIN32 */
-
- SECURITY_ATTRIBUTES sattr;
- HANDLE childstdoutrd,
- childstdoutwr,
- childstdoutrddup;
- PROCESS_INFORMATION pi;
- STARTUPINFO si;
- char *retval = NULL;
-
- sattr.nLength = sizeof(SECURITY_ATTRIBUTES);
- sattr.bInheritHandle = TRUE;
- sattr.lpSecurityDescriptor = NULL;
-
- if (!CreatePipe(&childstdoutrd, &childstdoutwr, &sattr, 0))
- return NULL;
-
- if (!DuplicateHandle(GetCurrentProcess(),
- childstdoutrd,
- GetCurrentProcess(),
- &childstdoutrddup,
- 0,
- FALSE,
- DUPLICATE_SAME_ACCESS))
- {
- CloseHandle(childstdoutrd);
- CloseHandle(childstdoutwr);
- return NULL;
- }
-
- CloseHandle(childstdoutrd);
-
- ZeroMemory(&pi, sizeof(pi));
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- si.dwFlags = STARTF_USESTDHANDLES;
- si.hStdError = childstdoutwr;
- si.hStdOutput = childstdoutwr;
- si.hStdInput = INVALID_HANDLE_VALUE;
-
- if (CreateProcess(NULL,
- cmd,
- NULL,
- NULL,
- TRUE,
- 0,
- NULL,
- NULL,
- &si,
- &pi))
- {
- /* Successfully started the process */
- char *lineptr;
-
- ZeroMemory(line, maxsize);
-
- /* Try to read at least one line from the pipe */
- /* This may require more than one wait/read attempt */
- for (lineptr = line; lineptr < line + maxsize - 1;)
- {
- DWORD bytesread = 0;
-
- /* Let's see if we can read */
- if (WaitForSingleObject(childstdoutrddup, 10000) != WAIT_OBJECT_0)
- break; /* Timeout, but perhaps we got a line already */
-
- if (!ReadFile(childstdoutrddup, lineptr, maxsize - (lineptr - line),
- &bytesread, NULL))
- break; /* Error, but perhaps we got a line already */
-
- lineptr += strlen(lineptr);
-
- if (!bytesread)
- break; /* EOF */
-
- if (strchr(line, '\n'))
- break; /* One or more lines read */
- }
-
- if (lineptr != line)
- {
- /* OK, we read some data */
- int len;
-
- /* If we got more than one line, cut off after the first \n */
- lineptr = strchr(line, '\n');
- if (lineptr)
- *(lineptr + 1) = '\0';
-
- len = strlen(line);
-
- /*
- * If EOL is \r\n, convert to just \n. Because stdout is a
- * text-mode stream, the \n output by the child process is
- * received as \r\n, so we convert it to \n. The server main.c
- * sets setvbuf(stdout, NULL, _IONBF, 0) which has the effect of
- * disabling \n to \r\n expansion for stdout.
- */
- if (len >= 2 && line[len - 2] == '\r' && line[len - 1] == '\n')
- {
- line[len - 2] = '\n';
- line[len - 1] = '\0';
- len--;
- }
-
- /*
- * We emulate fgets() behaviour. So if there is no newline at the
- * end, we add one...
- */
- if (len == 0 || line[len - 1] != '\n')
- strcat(line, "\n");
-
- retval = line;
- }
-
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- }
-
- CloseHandle(childstdoutwr);
- CloseHandle(childstdoutrddup);
-
- return retval;
-#endif /* WIN32 */
}