Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stddef.h>
// definitions
//
#define FLAG_PATH "{{ flag_path }}"
#define BUFSZ (128-8)
char *cp = NULL;
char *item_list = NULL;
enum {
RUNNING,
STOPPED,
PROBE_FILE
};
unsigned char state = STOPPED;
char default_list[] = {
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
};
// prototypes
//
void jobmgmt(void);
void read_flag(void);
void start_polling(void (*)(void));
// implementation
//
void jobmgmt(void)
{
if(state == STOPPED) {
printf("running job.\n");
state = RUNNING;
} else if(state == RUNNING) {
printf("ending job.\n");
state = STOPPED;
} else if(state == PROBE_FILE) {
printf("reading flag file.\n");
read_flag();
state = STOPPED;
} else {
printf("unknown state %d.. bailing.\n", state);
exit(1);
}
}
void read_flag(void)
{
FILE *fp = NULL;
char buffer[BUFSZ] = {0};
fp = fopen(FLAG_PATH, "r");
if(fp) {
fgets(buffer, sizeof(buffer), fp);
fclose(fp);
printf("%s\n", buffer);
} else {
perror("fopen " FLAG_PATH " failed");
}
exit(1);
}
void start_polling(void (*state_change)(void))
{
int cnt = 0xdeadbeef;
char buffer[BUFSZ] = {0x41};
cp = buffer;
state_change(); // running
for(cnt = 0; cnt <= BUFSZ; cnt++) {
*cp++ = item_list[cnt];
}
state_change(); // stopped
}
int main(int argc, char **argv)
{
void (*cb)(void) = jobmgmt;
item_list = (argc <= 1 ? default_list : argv[1]);
start_polling(cb);
return 0;
}