41 #define log_error(errcodefn, ...) \ 42 ereport(LOG, (errcodefn, errmsg_internal(__VA_ARGS__))) 44 #define log_error(errcodefn, ...) \ 45 (fprintf(stderr, __VA_ARGS__), fputc('\n', stderr)) 49 #define getcwd(cwd,len) GetCurrentDirectory(len, cwd) 57 static BOOL GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser);
75 char path_exe[
MAXPGPATH +
sizeof(
".exe") - 1];
78 if (strlen(path) >= strlen(
".exe") &&
79 pg_strcasecmp(path + strlen(path) - strlen(
".exe"),
".exe") != 0)
81 strlcpy(path_exe, path,
sizeof(path_exe) - 4);
82 strcat(path_exe,
".exe");
93 if (
stat(path, &buf) < 0)
104 is_r = (access(path, R_OK) == 0);
105 is_x = (access(path, X_OK) == 0);
110 return is_x ? (is_r ? 0 : -2) : -1;
138 _(
"could not identify current directory: %m"));
157 _(
"invalid binary \"%s\""), retpath);
172 if ((path = getenv(
"PATH")) && *path)
186 endp = startp + strlen(startp);
207 _(
"could not read binary \"%s\""),
215 _(
"could not find a \"%s\" to execute"), argv0);
255 _(
"could not identify current directory: %m"));
268 if (chdir(path) == -1)
271 _(
"could not change directory to \"%s\": %m"), path);
279 if (
lstat(fname, &buf) < 0 ||
280 !S_ISLNK(buf.st_mode))
284 rllen =
readlink(fname, link_buf,
sizeof(link_buf));
285 if (rllen < 0 || rllen >=
sizeof(link_buf))
288 _(
"could not read symbolic link \"%s\": %m"), fname);
291 link_buf[rllen] =
'\0';
292 strcpy(path, link_buf);
296 strlcpy(link_buf, fname,
sizeof(link_buf));
301 _(
"could not identify current directory: %m"));
307 if (chdir(orig_wd) == -1)
310 _(
"could not change directory to \"%s\": %m"), orig_wd);
325 const char *versionstr,
char *retpath)
339 "/%s%s", target,
EXE);
344 snprintf(cmd,
sizeof(cmd),
"\"%s\" -V", retpath);
349 if (strcmp(line, versionstr) != 0)
375 if ((pgver = popen(cmd,
"r")) == NULL)
377 perror(
"popen failure");
382 if (fgets(line, maxsize, pgver) == NULL)
385 fprintf(stderr,
"no data was returned by command \"%s\"\n", cmd);
387 perror(
"fgets failure");
398 SECURITY_ATTRIBUTES sattr;
399 HANDLE childstdoutrd,
402 PROCESS_INFORMATION pi;
406 sattr.nLength =
sizeof(SECURITY_ATTRIBUTES);
407 sattr.bInheritHandle = TRUE;
408 sattr.lpSecurityDescriptor = NULL;
410 if (!CreatePipe(&childstdoutrd, &childstdoutwr, &sattr, 0))
413 if (!DuplicateHandle(GetCurrentProcess(),
419 DUPLICATE_SAME_ACCESS))
421 CloseHandle(childstdoutrd);
422 CloseHandle(childstdoutwr);
426 CloseHandle(childstdoutrd);
428 ZeroMemory(&pi,
sizeof(pi));
429 ZeroMemory(&si,
sizeof(si));
431 si.dwFlags = STARTF_USESTDHANDLES;
432 si.hStdError = childstdoutwr;
433 si.hStdOutput = childstdoutwr;
434 si.hStdInput = INVALID_HANDLE_VALUE;
436 if (CreateProcess(NULL,
450 ZeroMemory(line, maxsize);
454 for (lineptr = line; lineptr < line + maxsize - 1;)
459 if (WaitForSingleObject(childstdoutrddup, 10000) != WAIT_OBJECT_0)
462 if (!ReadFile(childstdoutrddup, lineptr, maxsize - (lineptr - line),
466 lineptr += strlen(lineptr);
471 if (strchr(line,
'\n'))
481 lineptr = strchr(line,
'\n');
483 *(lineptr + 1) =
'\0';
494 if (len >= 2 && line[len - 2] ==
'\r' && line[len - 1] ==
'\n')
496 line[len - 2] =
'\n';
497 line[len - 1] =
'\0';
505 if (len == 0 || line[len - 1] !=
'\n')
511 CloseHandle(pi.hProcess);
512 CloseHandle(pi.hThread);
515 CloseHandle(childstdoutwr);
516 CloseHandle(childstdoutrddup);
532 exitstatus = pclose(stream);
537 if (exitstatus == -1)
541 _(
"pclose failed: %m"));
569 char env_path[
MAXPGPATH +
sizeof(
"PGSYSCONFDIR=")];
594 bindtextdomain(app, path);
597 if (getenv(
"PGLOCALEDIR") == NULL)
600 snprintf(env_path,
sizeof(env_path),
"PGLOCALEDIR=%s", path);
602 dup_path = strdup(env_path);
608 if (getenv(
"PGSYSCONFDIR") == NULL)
613 snprintf(env_path,
sizeof(env_path),
"PGSYSCONFDIR=%s", path);
615 dup_path = strdup(env_path);
649 ACL_SIZE_INFORMATION asi;
650 ACCESS_ALLOWED_ACE *pace;
653 DWORD dwTokenInfoLength = 0;
655 PTOKEN_USER pTokenUser = NULL;
656 TOKEN_DEFAULT_DACL tddNew;
657 TOKEN_DEFAULT_DACL *ptdd = NULL;
658 TOKEN_INFORMATION_CLASS tic = TokenDefaultDacl;
662 if (!GetTokenInformation(hToken, tic, (LPVOID) NULL, dwTokenInfoLength, &dwSize))
664 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
666 ptdd = (TOKEN_DEFAULT_DACL *) LocalAlloc(LPTR, dwSize);
674 if (!GetTokenInformation(hToken, tic, (LPVOID) ptdd, dwSize, &dwSize))
677 "could not get token information: error code %lu",
685 "could not get token information buffer size: error code %lu",
692 if (!GetAclInformation(ptdd->DefaultDacl, (LPVOID) &asi,
693 (DWORD)
sizeof(ACL_SIZE_INFORMATION),
697 "could not get ACL information: error code %lu",
703 if (!GetTokenUser(hToken, &pTokenUser))
707 dwNewAclSize = asi.AclBytesInUse +
sizeof(ACCESS_ALLOWED_ACE) +
708 GetLengthSid(pTokenUser->User.Sid) -
sizeof(DWORD);
711 pacl = (PACL) LocalAlloc(LPTR, dwNewAclSize);
719 if (!InitializeAcl(pacl, dwNewAclSize, ACL_REVISION))
722 "could not initialize ACL: error code %lu", GetLastError());
727 for (i = 0; i < (int) asi.AceCount; i++)
729 if (!GetAce(ptdd->DefaultDacl, i, (LPVOID *) &pace))
732 "could not get ACE: error code %lu", GetLastError());
736 if (!AddAce(pacl, ACL_REVISION, MAXDWORD, pace, ((PACE_HEADER) pace)->AceSize))
739 "could not add ACE: error code %lu", GetLastError());
745 if (!AddAccessAllowedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE, GENERIC_ALL, pTokenUser->User.Sid))
748 "could not add access allowed ACE: error code %lu",
754 tddNew.DefaultDacl = pacl;
756 if (!SetTokenInformation(hToken, tic, (LPVOID) &tddNew, dwNewAclSize))
759 "could not set token information: error code %lu",
768 LocalFree((HLOCAL) pTokenUser);
771 LocalFree((HLOCAL) pacl);
774 LocalFree((HLOCAL) ptdd);
788 GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
794 if (!GetTokenInformation(hToken,
800 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
802 *ppTokenUser = (PTOKEN_USER) LocalAlloc(LPTR, dwLength);
804 if (*ppTokenUser == NULL)
814 "could not get token information buffer size: error code %lu",
820 if (!GetTokenInformation(hToken,
826 LocalFree(*ppTokenUser);
830 "could not get token information: error code %lu",
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
static int resolve_symlinks(char *path)
void canonicalize_path(char *path)
int errcode(int sqlerrcode)
int pg_strcasecmp(const char *s1, const char *s2)
BOOL AddUserToTokenDacl(HANDLE hToken)
char * wait_result_to_str(int exitstatus)
int pclose_check(FILE *stream)
void pfree(void *pointer)
#define log_error(errcodefn,...)
static int validate_exec(const char *path)
int find_my_exec(const char *argv0, char *retpath)
#define readlink(path, buf, size)
int errcode_for_file_access(void)
#define is_absolute_path(filename)
char my_exec_path[MAXPGPATH]
#define PG_TEXTDOMAIN(domain)
char * first_dir_separator(const char *filename)
static void cleanup(void)
char * last_dir_separator(const char *filename)
void get_etc_path(const char *my_exec_path, char *ret_path)
size_t strlcpy(char *dst, const char *src, size_t siz)
#define StrNCpy(dst, src, len)
void join_path_components(char *ret_path, const char *head, const char *tail)
void set_pglocale_pgservice(const char *argv0, const char *app)
void get_locale_path(const char *my_exec_path, char *ret_path)
char * first_path_var_separator(const char *pathlist)
static char * pipe_read_line(char *cmd, char *line, int maxsize)