config_t* parse_config(const char* config_file) { config_t* config = calloc(1, sizeof(config_t)); if(!config) return NULL; FILE* f = fopen(config_file, "r"); if(!f) { free_config(config); return NULL; } char buf[BUFSIZE]; while(fgets(buf, BUFSIZE, f)) { if(*buf == '#' || *buf == '\n' || !*buf) continue; if(-1 == add_config_entry(config, buf)) { /* FIXME */ fprintf(stderr, "Unable to parse config line: %s\n", buf); } } if(EOF == fclose(f)) { free_config(config); return NULL; } return config; }
bool shepherd_read_qrsh_pid_file(const char *filename, pid_t *qrsh_pid, int *replace_qrsh_pid) { bool ret = true; FILE *fp = NULL; fp = fopen(filename, "r"); if (fp != NULL) { int arguments = fscanf(fp, pid_t_fmt, qrsh_pid); if (arguments == 1) { char buffer[50]; /* set pid from qrsh_starter as job_pid */ sprintf(buffer, pid_t_fmt, *qrsh_pid); /* TODO: should better be add_or_replace */ add_config_entry("job_pid", buffer); *replace_qrsh_pid = 0; } else { shepherd_trace("could not read qrsh_pid file"); ret = false; } FCLOSE(fp); } else { /* * CR 6588743 - raising a shepherd_error here would set the queue in * error state and rerun the job */ shepherd_trace(MSG_FILE_NOOPEN_SS, filename, strerror(errno)); ret = false; } return ret; FCLOSE_ERROR: /* * CR 6588743 - raising a shepherd_error here would set the queue in * error state and rerun the job */ shepherd_trace(MSG_FILE_NOCLOSE_SS, filename, strerror(errno)); return false; }
void test() { bool result; const uint8_t* filename = (const uint8_t*)"/home/user/test.conf"; uint8_t buffer[64]; config_parser_t cfg; for(;;) { toggle_led(LED2); log_info(&log, "remove file %s", filename); // delete the file unlink((const char*)filename); // check the file is not present if(open_config_file(&cfg, buffer, sizeof(buffer), filename)) { while(get_next_config(&cfg)) log_info(&log, "read %s=%s", get_config_key(&cfg), get_config_value(&cfg)); close_config_file(&cfg); } else log_info(&log, "file %s removed successfully", filename); // add to a new file result = add_config_entry(buffer, sizeof(buffer), filename, (const uint8_t*)"testkey", (const uint8_t*)"testvalue"); log_info(&log, "wrote %s=%s to %s, result=%d", "testkey", "testvalue", filename, result); result = add_config_entry(buffer, sizeof(buffer), filename, (const uint8_t*)"testkey1", (const uint8_t*)"testvalue1"); log_info(&log, "wrote %s=%s to %s, result=%d", "testkey1", "testvalue1", filename, result); // check the file is now present with if(open_config_file(&cfg, buffer, sizeof(buffer), filename)) { while(get_next_config(&cfg)) log_info(&log, "read %s=%s", get_config_key(&cfg), get_config_value(&cfg)); close_config_file(&cfg); } // edit within file result = edit_config_entry(buffer, sizeof(buffer), filename, (const uint8_t*)"testkey", (const uint8_t*)"newtestvalue"); log_info(&log, "wrote %s=%s to %s, result=%d", "testkey", "testvalue", filename, result); result = edit_config_entry(buffer, sizeof(buffer), filename, (const uint8_t*)"testkey1", (const uint8_t*)"newtestvalue1"); log_info(&log, "wrote %s=%s to %s, result=%d", "testkey1", "testvalue1", filename, result); // check the file is now present with new values if(open_config_file(&cfg, buffer, sizeof(buffer), filename)) { while(get_next_config(&cfg)) log_info(&log, "read %s=%s", get_config_key(&cfg), get_config_value(&cfg)); close_config_file(&cfg); } sleep(10); } pthread_exit(0); }