I don't know why, but getppid() returns wrong result for me when application is started

This commit is contained in:
Ilya Fedin 2020-05-07 16:17:06 +04:00
parent 07e851fcc8
commit b2ac83a76e
4 changed files with 40 additions and 7 deletions

View file

@ -41,17 +41,34 @@
old_env = getenv("PYTHONPATH") ?: "";
SET_NEW_ENV(new_pythonpath, appdir_s + strlen(old_env), "PYTHONPATH=%s/usr/share/pyshared/:%s", appdir, old_env);
@@ -197,15 +208,12 @@
@@ -196,16 +207,29 @@
/* Otherwise may get errors because Python cannot write __pycache__ bytecode cache */
putenv("PYTHONDONTWRITEBYTECODE=1");
/* Run */
- /* Run */
- ret = execvp(exe, outargptrs);
-
+ /* Set pid & ppid */
+ pid_t pid = getpid();
+ pid_t ppid = getppid();
- int error = errno;
+ execvp(exe, outargptrs);
+ int pid_len = snprintf(NULL, 0, "%d", pid);
+ int ppid_len = snprintf(NULL, 0, "%d", ppid);
- if (ret == -1)
- die("Error executing '%s': %s\n", exe, strerror(error));
+ char *s_pid = malloc(pid_len + 1);
+ snprintf(s_pid, pid_len + 1, "%d", pid);
+
+ char *s_ppid = malloc(ppid_len + 1);
+ snprintf(s_ppid, ppid_len + 1, "%d", ppid);
+
+ setenv("PID", s_pid, 1);
+ setenv("PPID", s_ppid, 1);
+
+ /* Run */
+ execvp(exe, outargptrs);
+
+ free(optional_ld_library_path);
+ if (optional_ld_preload)
+ free(optional_ld_preload);
@ -61,7 +78,7 @@
free(usr_in_appdir);
free(new_pythonhome);
free(new_path);
@@ -215,5 +223,16 @@
@@ -215,5 +239,16 @@
free(new_perllib);
free(new_gsettings_schema_dir);
free(new_qt_plugin_path);

17
env.c
View file

@ -43,6 +43,21 @@ void env_free(char* const *env) {
free((char**)env);
}
pid_t get_parent_pid() {
pid_t ppid = 0;
char *s_ppid = getenv("PPID");
if(s_ppid) {
ppid = atoi(s_ppid);
}
if (!ppid) {
ppid = getppid();
}
return ppid;
}
static size_t get_number_of_variables(FILE *file, char **buffer, size_t *len) {
size_t number = 0;
@ -103,7 +118,7 @@ static char* const* read_env_from_process(pid_t pid) {
}
char* const* read_parent_env() {
pid_t ppid = getppid();
pid_t ppid = get_parent_pid();
return read_env_from_process(ppid);
}

1
env.h
View file

@ -3,6 +3,7 @@
#include <unistd.h>
pid_t get_parent_pid();
char* const* read_parent_env();
void env_free(char* const *env);

2
exec.c
View file

@ -79,7 +79,7 @@ static int exec_common(execve_func_t function, const char *filename, char* const
DEBUG("filename %s, fullpath %s\n", filename, fullpath);
char* const *env = envp;
if (is_external_process(fullpath)) {
DEBUG("External process detected. Restoring env vars from parent %d\n", getppid());
DEBUG("External process detected. Restoring env vars from parent %d\n", get_parent_pid());
env = read_parent_env();
if (!env)
env = envp;