File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 2
2
#include <unistd.h>
3
3
#include <sys/types.h>
4
4
#include <sys/stat.h>
5
+ #include <ctype.h>
5
6
#include <fcntl.h>
6
7
#include <string.h>
7
8
#include <linux/string.h>
@@ -217,8 +218,40 @@ static int wait_or_whine(struct child_process *cmd, bool block)
217
218
218
219
int check_if_command_finished (struct child_process * cmd )
219
220
{
221
+ #ifdef __linux__
222
+ char filename [FILENAME_MAX + 12 ];
223
+ char status_line [256 ];
224
+ FILE * status_file ;
225
+
226
+ /*
227
+ * Check by reading /proc/<pid>/status as calling waitpid causes
228
+ * stdout/stderr to be closed and data lost.
229
+ */
230
+ sprintf (filename , "/proc/%d/status" , cmd -> pid );
231
+ status_file = fopen (filename , "r" );
232
+ if (status_file == NULL ) {
233
+ /* Open failed assume finish_command was called. */
234
+ return true;
235
+ }
236
+ while (fgets (status_line , sizeof (status_line ), status_file ) != NULL ) {
237
+ char * p ;
238
+
239
+ if (strncmp (status_line , "State:" , 6 ))
240
+ continue ;
241
+
242
+ fclose (status_file );
243
+ p = status_line + 6 ;
244
+ while (isspace (* p ))
245
+ p ++ ;
246
+ return * p == 'Z' ? 1 : 0 ;
247
+ }
248
+ /* Read failed assume finish_command was called. */
249
+ fclose (status_file );
250
+ return 1 ;
251
+ #else
220
252
wait_or_whine (cmd , /*block=*/ false);
221
253
return cmd -> finished ;
254
+ #endif
222
255
}
223
256
224
257
int finish_command (struct child_process * cmd )
You can’t perform that action at this time.
0 commit comments