This program I wrote is supposed to restart tor, wait 5 seconds, then look for a PID number using regex.
It compiles and runs fine but the regex logic never displays "good".
I'm confused.
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <string.h>
// gcc -o tor_restart tor_restart.c
// chown root tor_restart
// chmod +s tor_restart
int main( int argc, char **argv ) {
regex_t regex;
int reti;
FILE *in;
extern FILE *popen();
char buff[512];
reti = regcomp(®ex, "[0-9]+", 0);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
return 1;
}
setuid(geteuid());
system("/etc/init.d/tor restart > /dev/null 2>&1");
sleep(5);
if (!(in = popen("pidof tor", "r"))) {
return 1;
}
while (fgets(buff, sizeof(buff), in) != NULL ) {
//printf("Output: %s", buff);
if (regexec(®ex, buff, 0, NULL, 0) == 0) {
puts("good");
}
}
regfree(®ex);
pclose(in);
return 0;
}