77 lines
1.9 KiB
C
77 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
#include <unistd.h>
|
|
#include <sys/ipc.h>
|
|
#include <sys/shm.h>
|
|
#include <time.h>
|
|
|
|
#define SHM_KEY 12345
|
|
#define MAX_RULES 100
|
|
#define MAX_ARGS 10
|
|
|
|
typedef struct {
|
|
char cmd[256];
|
|
char type[32];
|
|
char msg[1024];
|
|
char args[MAX_ARGS][256];
|
|
int arg_count;
|
|
} Rule;
|
|
|
|
typedef struct {
|
|
bool enabled;
|
|
Rule rules[MAX_RULES];
|
|
int rule_count;
|
|
} ConfigData;
|
|
|
|
void print_config(const ConfigData *cfg) {
|
|
printf("=== Config ===\n");
|
|
printf("Enabled: %s\n", cfg->enabled ? "true" : "false");
|
|
printf("Rule count: %d\n", cfg->rule_count);
|
|
for (int i = 0; i < cfg->rule_count && i < MAX_RULES; i++) {
|
|
const Rule *r = &cfg->rules[i];
|
|
printf("Rule %d:\n", i + 1);
|
|
printf(" CMD : %s\n", r->cmd);
|
|
printf(" Type: %s\n", r->type);
|
|
printf(" Msg : %s\n", r->msg);
|
|
printf(" Args(%d):\n", r->arg_count);
|
|
for (int j = 0; j < r->arg_count && j < MAX_ARGS; j++) {
|
|
printf(" - %s\n", r->args[j]);
|
|
}
|
|
}
|
|
printf("=====================\n\n");
|
|
}
|
|
|
|
int main() {
|
|
int shmid = shmget(SHM_KEY, sizeof(ConfigData), 0666);
|
|
if (shmid < 0) {
|
|
perror("shmget failed");
|
|
return 1;
|
|
}
|
|
|
|
ConfigData *shared_cfg = (ConfigData *)shmat(shmid, NULL, SHM_RDONLY);
|
|
if (shared_cfg == (void *)-1) {
|
|
perror("shmat failed");
|
|
return 1;
|
|
}
|
|
|
|
ConfigData last_cfg = {0};
|
|
|
|
struct timespec ts;
|
|
ts.tv_sec = 0;
|
|
ts.tv_nsec = 100 * 1000000; // 100ms
|
|
|
|
while (1) {
|
|
if (memcmp(&last_cfg, shared_cfg, sizeof(ConfigData)) != 0) {
|
|
printf(">>> Config changed:\n");
|
|
print_config(shared_cfg);
|
|
memcpy(&last_cfg, shared_cfg, sizeof(ConfigData));
|
|
}
|
|
nanosleep(&ts, NULL); // 每 100 毫秒检查一次
|
|
}
|
|
|
|
shmdt(shared_cfg);
|
|
return 0;
|
|
}
|