diff options
Diffstat (limited to 'src/rest.c')
-rw-r--r-- | src/rest.c | 84 |
1 files changed, 62 insertions, 22 deletions
@@ -1012,15 +1012,68 @@ static int og_cmd_reboot(json_t *element, struct og_msg_params *params) #define OG_TFTP_TMPL_PATH_UEFI "/opt/opengnsys/tftpboot/grub/templates" #define OG_TFTP_TMPL_PATH "/opt/opengnsys/tftpboot/menu.lst/templates" +struct og_boot_mode { + struct list_head list; + char name[FILENAME_MAX]; +}; + +static void og_boot_mode_free(struct list_head *boot_mode_list) +{ + struct og_boot_mode *mode, *tmp; + + list_for_each_entry_safe(mode, tmp, boot_mode_list, list) { + list_del(&mode->list); + free(mode); + } +} + +static int og_get_boot_modes(struct list_head *boot_mode_list) +{ + struct og_boot_mode *mode; + struct dirent *dent; + DIR *d; + + d = opendir(OG_TFTP_TMPL_PATH); + if (!d) { + syslog(LOG_ERR, "Cannot open directory %s (%s:%d)\n", + OG_TFTP_TMPL_PATH, __func__, __LINE__); + return -1; + } + + dent = readdir(d); + while (dent) { + if (dent->d_type != DT_REG) { + dent = readdir(d); + continue; + } + + mode = calloc(1, sizeof(struct og_boot_mode)); + if (!mode) { + closedir(d); + og_boot_mode_free(boot_mode_list); + return -1; + } + + snprintf(mode->name, FILENAME_MAX, "%s", dent->d_name); + list_add_tail(&mode->list, boot_mode_list); + + dent = readdir(d); + } + closedir(d); + + return 0; +} + static int og_cmd_get_modes(json_t *element, struct og_msg_params *params, char *buffer_reply) { struct og_buffer og_buffer = { .data = buffer_reply }; + struct og_boot_mode *mode; + LIST_HEAD(boot_mode_list); json_t *root, *modes; - struct dirent *dent; - DIR *d = NULL; + int ret; root = json_object(); if (!root) @@ -1032,35 +1085,22 @@ static int og_cmd_get_modes(json_t *element, struct og_msg_params *params, return -1; } - d = opendir(OG_TFTP_TMPL_PATH); - if (!d) { + if (og_get_boot_modes(&boot_mode_list) < 0) { json_decref(modes); json_decref(root); - syslog(LOG_ERR, "Cannot open directory %s\n", - OG_TFTP_TMPL_PATH); return -1; } - dent = readdir(d); - while (dent) { - if (dent->d_type != DT_REG) { - dent = readdir(d); - continue; - } - json_array_append_new(modes, json_string(dent->d_name)); - dent = readdir(d); - } - json_object_set_new(root, "modes", modes); + list_for_each_entry(mode, &boot_mode_list, list) + json_array_append_new(modes, json_string(mode->name)); - if (json_dump_callback(root, og_json_dump_clients, &og_buffer, 0)) { - json_decref(root); - return -1; - } + og_boot_mode_free(&boot_mode_list); + json_object_set_new(root, "modes", modes); + ret = json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); json_decref(root); - closedir(d); - return 0; + return ret; } static int og_change_db_mode(struct og_dbi *dbi, const char *mac, |