about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-06-03 13:20:25 +0200
committerMichael Vetter <jubalh@iodoru.org>2020-06-03 13:20:25 +0200
commitb7d5b964a2e7d94d7019463358e2b53338feaa4b (patch)
tree8b8b0269346f908e86c045cf3abdf404aafaa82a
parentf1fe18b47442aed5a0b427bc9450bd7111ec7314 (diff)
parentfad77d9d70b405d307c85ced27796c052ace050d (diff)
downloadprofani-tty-b7d5b964a2e7d94d7019463358e2b53338feaa4b.tar.gz
Merge remote-tracking branch 'peetah/externalCallWithGSpawnSync' into
peetah-external-cmd

Merge peetahs external command calling improvements from his mirror at
https://framagit.org/peetah/profanity.
-rw-r--r--src/command/cmd_funcs.c9
-rw-r--r--src/common.c91
-rw-r--r--src/common.h2
-rw-r--r--src/config/account.c50
-rw-r--r--src/xmpp/avatar.c5
5 files changed, 117 insertions, 40 deletions
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c
index 55082765..59702fe3 100644
--- a/src/command/cmd_funcs.c
+++ b/src/command/cmd_funcs.c
@@ -4900,7 +4900,7 @@ cmd_sendfile(ProfWin *window, const char *const command, gchar **args)
 			free(filename);
 			return TRUE;
         }
-        
+
     if (access(filename, R_OK) != 0) {
         cons_show_error("Uploading '%s' failed: File not found!", filename);
         free(filename);
@@ -8921,9 +8921,10 @@ cmd_urlopen(ProfWin *window, const char *const command, gchar **args)
             return TRUE;
         }
 
-        gchar* cmd = prefs_get_string(PREF_URL_OPEN_CMD);
-        call_external(cmd, args[0]);
-        g_free(cmd);
+        gchar *argv[] = {prefs_get_string(PREF_URL_OPEN_CMD), args[0], NULL};
+        if (!call_external(argv, NULL, NULL)) {
+          cons_show_error("Unable to open url: check the logs for more information.");
+        }
     } else {
         cons_show("urlopen not supported in this window");
     }
diff --git a/src/common.c b/src/common.c
index 821acd3e..1de5f4ba 100644
--- a/src/common.c
+++ b/src/common.c
@@ -484,15 +484,90 @@ get_mentions(gboolean whole_word, gboolean case_sensitive, const char *const mes
     return mentions;
 }
 
-void
-call_external(const char *const exe, const char *const param)
+/*
+ * Take an NULL-terminated array used as the tokens of a command, and optionally
+ * pointers to the string arrays that will store each lines of the call standard
+ * output and standard error.
+ *
+ * argv - NULL-terminated string array containing the tokens of the command
+ *        line to spawn
+ * output_ptr - a pointer to the string array where to store spawned command
+ *              standard output
+ *              set to NULL to ignore the command standard output
+ * error_ptr - a pointer to the string array where to store spawned command
+ *             standard error
+ *             set to NULL to ignore the command standard error
+ *
+ * Returns:
+ * - TRUE if the command has been successfully spawned and exited normally
+ * - FALSE otherwise
+ */
+gboolean
+call_external(gchar **argv, gchar ***const output_ptr, gchar ***const error_ptr)
 {
-    GString *cmd = g_string_new("");
+    gchar *stdout_str = NULL;
+    gchar **stdout_str_ptr = &stdout_str;
+    gchar *stderr_str = NULL;
+    gchar **stderr_str_ptr = &stderr_str;
+    GSpawnFlags flags = G_SPAWN_SEARCH_PATH;
+    gint status;
+    GError *error = NULL;
+    gchar *cmd = NULL;
+
+    cmd = g_strjoinv(" ", argv);
+    log_debug("Calling external: %s", cmd);
+
+    if (!output_ptr) {
+        stdout_str_ptr = NULL;
+        flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
+    }
+
+    if (!error_ptr) {
+        stderr_str_ptr = NULL;
+        flags |= G_SPAWN_STDERR_TO_DEV_NULL;
+    }
+
+    if (!g_spawn_sync (NULL, argv, NULL, flags, NULL, NULL, stdout_str_ptr, stderr_str_ptr, &status, &error)) {
+        log_error("Spawning '%s' failed: %s.", cmd, error->message);
+        g_error_free(error);
+        error = NULL;
+        return FALSE;
+    }
 
-    g_string_append_printf(cmd, "%s %s > /dev/null 2>&1", exe, param);
-    log_debug("Calling external: %s", cmd->str);
-    FILE *stream = popen(cmd->str, "r");
+    if (!g_spawn_check_exit_status(status, &error)) {
+        log_error("Calling '%s' failed: %s.", cmd, error->message);
+        g_error_free(error);
+        error = NULL;
+        g_free(cmd);
+        cmd = NULL;
+        g_free(stdout_str);
+        stdout_str = NULL;
+        stdout_str_ptr = NULL;
+        if (stderr_str && strlen(stderr_str)) {
+            log_error("Called command returned the following on stderr: %s.", stderr_str);
+        }
+        g_free(stderr_str);
+        stderr_str = NULL;
+        stderr_str_ptr = NULL;
+        return FALSE;
+    }
 
-    pclose(stream);
-    g_string_free(cmd, TRUE);
+    g_free(cmd);
+    cmd = NULL;
+
+    if (output_ptr) {
+        *output_ptr = g_strsplit(stdout_str, "\n", 0);
+        g_free(stdout_str);
+        stdout_str = NULL;
+        stdout_str_ptr = NULL;
+    }
+
+    if (error_ptr) {
+        *error_ptr = g_strsplit(stderr_str, "\n", 0);
+        g_free(stderr_str);
+        stderr_str = NULL;
+        stderr_str_ptr = NULL;
+    }
+
+    return TRUE;
 }
diff --git a/src/common.h b/src/common.h
index 108536ed..93e4a609 100644
--- a/src/common.h
+++ b/src/common.h
@@ -106,6 +106,6 @@ void get_file_paths_recursive(const char *directory, GSList **contents);
 
 char* get_random_string(int length);
 
-void call_external(const char *const exe, const char *const param);
+gboolean call_external(gchar **argv, gchar ***const output_ptr, gchar ***const error_ptr);
 
 #endif
diff --git a/src/config/account.c b/src/config/account.c
index 96397954..daa2fc77 100644
--- a/src/config/account.c
+++ b/src/config/account.c
@@ -194,32 +194,30 @@ account_eval_password(ProfAccount *account)
     assert(account != NULL);
     assert(account->eval_password != NULL);
 
-    // Evaluate as shell command to retrieve password
-    GString *cmd = g_string_new("");
-    g_string_append_printf(cmd, "%s 2>/dev/null", account->eval_password);
-
-    FILE *stream = popen(cmd->str, "r");
-    g_string_free(cmd, TRUE);
-    if (stream) {
-        // Limit to READ_BUF_SIZE bytes to prevent overflows in the case of a poorly chosen command
-        account->password = g_malloc(READ_BUF_SIZE);
-        if (!account->password) {
-            log_error("Failed to allocate enough memory to read eval_password output");
-            return FALSE;
-        }
-        account->password = fgets(account->password, READ_BUF_SIZE, stream);
-        pclose(stream);
-        if (!account->password) {
-            log_error("No result from eval_password.");
-            return FALSE;
-        }
-
-        // strip trailing newline
-        if (g_str_has_suffix(account->password, "\n")) {
-            account->password[strlen(account->password)-1] = '\0';
-        }
-    } else {
-        log_error("popen failed when running eval_password.");
+    gchar **output = NULL;
+    gchar **argv = g_strsplit(account->eval_password, " ", 0);
+
+    if (!call_external(argv, &output, NULL)) {
+        g_strfreev(argv);
+        argv = NULL;
+        return FALSE;
+    }
+
+    g_strfreev(argv);
+
+    if (!output || !output[0]) {
+        log_error("Failed to read eval_password output");
+        g_strfreev(output);
+        output = NULL;
+        return FALSE;
+    }
+
+    account->password = strdup(output[0]);
+    g_strfreev(output);
+    output = NULL;
+
+    if (!account->password) {
+        log_error("Failed to allocate enough memory to read eval_password output");
         return FALSE;
     }
 
diff --git a/src/xmpp/avatar.c b/src/xmpp/avatar.c
index 701d6cb7..c5b44411 100644
--- a/src/xmpp/avatar.c
+++ b/src/xmpp/avatar.c
@@ -266,7 +266,10 @@ _avatar_request_item_result_handler(xmpp_stanza_t *const stanza, void *const use
 
     // if we shall open it
     if (g_hash_table_contains(shall_open, from_attr)) {
-        call_external(prefs_get_string(PREF_AVATAR_CMD), filename->str);
+        gchar *argv[] = {prefs_get_string(PREF_AVATAR_CMD), filename->str, NULL};
+        if (!call_external(argv, NULL, NULL)) {
+          cons_show_error("Unable to display avatar: check the logs for more information.");
+        }
         g_hash_table_remove(shall_open, from_attr);
     }
 
-26 11:44:14 -0800 3710' href='/akkartik/mu/commit/html/089scenario_filesystem.cc.html?h=main&id=204dae921abff0c70e017215bb3c91fa6ca11aff'>204dae92 ^
9e751bb8 ^

204dae92 ^









201458e3 ^
204dae92 ^

201458e3 ^
204dae92 ^







2c678a4e ^
204dae92 ^






bfa3f5ba ^
204dae92 ^



201458e3 ^
204dae92 ^
201458e3 ^

204dae92 ^

201458e3 ^
204dae92 ^


9e751bb8 ^





1c2d788b ^
9e751bb8 ^


1c2d788b ^
9e751bb8 ^


1c2d788b ^
9e751bb8 ^


2c678a4e ^
9e751bb8 ^
1c2d788b ^
9e751bb8 ^




1c2d788b ^
9e751bb8 ^


1c2d788b ^
9e751bb8 ^


1c2d788b ^
9e751bb8 ^




1c2d788b ^
9e751bb8 ^


1c2d788b ^
9e751bb8 ^


1c2d788b ^
9e751bb8 ^


2c678a4e ^

204dae92 ^


201458e3 ^
204dae92 ^


598f1b53 ^
204dae92 ^

9e751bb8 ^
1c2d788b ^
9e751bb8 ^

1c2d788b ^
9e751bb8 ^




2c678a4e ^
9e751bb8 ^


2c678a4e ^
9e751bb8 ^







204dae92 ^



201458e3 ^
2c678a4e ^
204dae92 ^
1c2d788b ^
2c678a4e ^
1c2d788b ^
2c678a4e ^
1c2d788b ^
9e751bb8 ^
2c678a4e ^
1c2d788b ^
2c678a4e ^
1c2d788b ^
9e751bb8 ^
204dae92 ^

2c678a4e ^
1c2d788b ^
2c678a4e ^
1c2d788b ^
204dae92 ^
201458e3 ^
204dae92 ^
2c678a4e ^
1c2d788b ^
2c678a4e ^
1c2d788b ^
204dae92 ^
2c678a4e ^
1c2d788b ^
204dae92 ^

201458e3 ^
204dae92 ^








8e7b4429 ^



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312