forked from hcorion/AppImageKit-checkrt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
env.c
executable file
·120 lines (99 loc) · 3.23 KB
/
env.c
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
/* Copyright (c) 2018 Pablo Marcos Oltra <pablo.marcos.oltra@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define _GNU_SOURCE
#include "env.h"
#include "debug.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
static char** env_allocate(size_t size) {
return calloc(size + 1, sizeof(char*));
}
void env_free(char* const *env) {
size_t len = 0;
while (env[len] != 0) {
free(env[len]);
len++;
}
free((char**)env);
}
static size_t get_number_of_variables(FILE *file, char **buffer, size_t *len) {
size_t number = 0;
if (getline(buffer, len, file) < 0)
return -1;
char *ptr = *buffer;
while (ptr < *buffer + *len) {
size_t var_len = strlen(ptr);
ptr += var_len + 1;
if (var_len == 0)
break;
number++;
}
return number != 0 ? (ssize_t)number : -1;
}
static char* const* env_from_buffer(FILE *file) {
char *buffer = NULL;
size_t len = 0;
size_t num_vars = get_number_of_variables(file, &buffer, &len);
char** env = env_allocate(num_vars);
size_t n = 0;
char *ptr = buffer;
while (ptr < buffer + len && n < num_vars) {
size_t var_len = strlen(ptr);
if (var_len == 0)
break;
env[n] = calloc(sizeof(char*), var_len + 1);
strncpy(env[n], ptr, var_len + 1);
DEBUG("\tenv var copied: %s\n", env[n]);
ptr += var_len + 1;
n++;
}
free(buffer);
return env;
}
static char* const* read_env_from_process(pid_t pid) {
char buffer[256] = {0};
snprintf(buffer, sizeof(buffer), "/proc/%d/environ", pid);
DEBUG("Reading env from parent process: %s\n", buffer);
FILE *env_file = fopen(buffer, "r");
if (!env_file) {
DEBUG("Error reading file: %s (%s)\n", buffer, strerror(errno));
return NULL;
}
char* const* env = env_from_buffer(env_file);
fclose(env_file);
return env;
}
char* const* read_parent_env() {
pid_t ppid = getppid();
return read_env_from_process(ppid);
}
#ifdef ENV_TEST
int main() {
putenv("APPIMAGE_CHECKRT_DEBUG=1");
DEBUG("ENV TEST\n");
char **env = NULL;
read_parent_env(&env);
return 0;
}
#endif