| /*****************************************************************************\ | |
| * sercli.h - serialize and deserialize to CLI | |
| ***************************************************************************** | |
| * Copyright (C) SchedMD LLC. | |
| * | |
| * This file is part of Slurm, a resource management program. | |
| * For details, see <https://slurm.schedmd.com/>. | |
| * Please also read the included file: DISCLAIMER. | |
| * | |
| * Slurm is free software; you can redistribute it and/or modify it under | |
| * the terms of the GNU General Public License as published by the Free | |
| * Software Foundation; either version 2 of the License, or (at your option) | |
| * any later version. | |
| * | |
| * In addition, as a special exception, the copyright holders give permission | |
| * to link the code of portions of this program with the OpenSSL library under | |
| * certain conditions as described in each individual source file, and | |
| * distribute linked combinations including the two. You must obey the GNU | |
| * General Public License in all respects for all of the code used other than | |
| * OpenSSL. If you modify file(s) with this exception, you may extend this | |
| * exception to your version of the file(s), but you are not obligated to do | |
| * so. If you do not wish to do so, delete this exception statement from your | |
| * version. If you delete this exception statement from all source files in | |
| * the program, then also delete it here. | |
| * | |
| * Slurm is distributed in the hope that it will be useful, but WITHOUT ANY | |
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
| * details. | |
| * | |
| * You should have received a copy of the GNU General Public License along | |
| * with Slurm; if not, write to the Free Software Foundation, Inc., | |
| * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| \*****************************************************************************/ | |
| #ifndef _COMMON_SERCLI_H | |
| #define _COMMON_SERCLI_H | |
| #include "src/common/openapi.h" | |
| #include "src/interfaces/data_parser.h" | |
| #include "src/interfaces/serializer.h" | |
| /* | |
| * Generate meta instance for a CLI command | |
| */ | |
| extern openapi_resp_meta_t *data_parser_cli_meta(int argc, char **argv, | |
| const char *mime_type); | |
| #define DATA_PARSER_DUMP_CLI_CTXT_MAGIC 0x1BA211B3 | |
| typedef struct { | |
| int magic; /* DATA_PARSER_DUMP_CLI_CTXT_MAGIC */ | |
| int rc; | |
| list_t *errors; | |
| const char *mime_type; | |
| list_t *warnings; | |
| const char *data_parser; | |
| openapi_resp_meta_t *meta; | |
| } data_parser_dump_cli_ctxt_t; | |
| /* | |
| * Release a parser from data_parser_cli_load() and the dump ctxt it carries | |
| * (meta, errors, warnings), then NULL *parser_ptr. | |
| * NOTE: frees the parser first so no callback can touch the ctxt afterward. | |
| * NOTE: no-op when *parser_ptr is NULL (the "list"/error/no-mime paths). | |
| */ | |
| extern void data_parser_cli_free_ctxt(data_parser_t **parser_ptr); | |
| /* | |
| * data_parser_cli_load() for one-shot tools with no cleanup label: on the | |
| * "list" short-circuit or a load error, release the ctxt and exit(). | |
| * NOTE: no-op returning SLURM_SUCCESS when mime_type is unset, so it is safe | |
| * to call unconditionally. | |
| * IN/OUT parser_ptr - set to the new parser, or NULL on "list"/error | |
| * IN acct_db_conn - slurmdb connection or NULL | |
| * IN argc/argv - program argv (used to populate meta) | |
| * IN mime_type - mime type the caller intends to dump (load skipped if unset) | |
| * IN data_parser - data_parser parameters; "list" prints plugin list | |
| * RET SLURM_SUCCESS or error (only returns on the dump path) | |
| */ | |
| extern int data_parser_load_cli_or_exit(data_parser_t **parser_ptr, | |
| void *acct_db_conn, int argc, | |
| char **argv, const char *mime_type, | |
| const char *data_parser); | |
| /* | |
| * Dump a full openapi_resp_* struct to STDOUT using a parser from | |
| * data_parser_cli_load(). | |
| * NOTE: injects the parser's ctxt meta/errors/warnings into resp's common | |
| * prefix for the dump, then unhooks and releases them, leaving the parser | |
| * reusable for a subsequent dump. | |
| * IN type - data parser type for *resp | |
| * IN resp - ptr to an openapi_resp_* struct to dump | |
| * IN resp_bytes - sizeof(*resp); must be the full per-type size | |
| * IN parser - parser from data_parser_cli_load() | |
| * RET SLURM_SUCCESS or error | |
| */ | |
| extern int data_parser_dump_cli_resp(data_parser_type_t type, void *resp, | |
| int resp_bytes, data_parser_t *parser); | |
| /* | |
| * Like data_parser_dump_cli_resp() but wraps a bare object in | |
| * openapi_resp_single_t before dumping. | |
| * IN type - data parser type for the wrapped response object | |
| * IN response - ptr to the object to dump as the single response field | |
| * IN parser - parser from data_parser_cli_load() | |
| * RET SLURM_SUCCESS or error | |
| */ | |
| extern int data_parser_dump_cli_single(data_parser_type_t type, void *response, | |
| data_parser_t *parser); | |
| /* | |
| * Load a CLI dump context for a subsequent dump. | |
| * NOTE: call BEFORE the slurm_load_*() RPC so "list" can short-circuit it. | |
| * NOTE: for "list", prints plugin names to STDOUT (header on STDERR) and | |
| * returns SLURM_SUCCESS with *parser_ptr NULL and nothing allocated; the | |
| * caller detects a NULL parser and skips the dump. | |
| * NOTE: otherwise heap-allocates the dump ctxt (errors/warnings/meta) and the | |
| * parser that carries it as its callback arg, assigns acct_db_conn, and | |
| * sets meta->plugin.data_parser; release with | |
| * data_parser_cli_free_ctxt(). | |
| * NOTE: on a load error it frees everything it allocated and returns | |
| * *parser_ptr NULL, so the caller need not free on the error path. | |
| * IN/OUT parser_ptr - set to the new parser, or NULL on "list"/error | |
| * IN acct_db_conn - slurmdb connection or NULL | |
| * IN argc/argv - program argv (used to populate meta) | |
| * IN mime_type - mime type the caller intends to dump | |
| * IN data_parser - data_parser parameters; "list" prints plugin list | |
| * RET SLURM_SUCCESS or error | |
| */ | |
| extern int data_parser_cli_load(data_parser_t **parser_ptr, void *acct_db_conn, | |
| int argc, char **argv, const char *mime_type, | |
| const char *data_parser); | |
| /* | |
| * Dump given target struct src into string | |
| * NOTE: Use SERCLI_DUMP_STR() macro instead of calling directly! | |
| * NOTE: Use SERDES_DUMP() for fixed size buffers instead. | |
| * NOTE: Use SERDES_DUMP_BUF() for buffers instead. | |
| * NOTE: errors logged via openapi_error_log_foreach() | |
| * NOTE: warnings logged via openapi_warn_log_foreach() | |
| * IN type - data_parser type of obj | |
| * IN db_conn - database connection pointer | |
| * IN src - ptr to struct/scalar to dump to data_t | |
| * This *must* be a pointer to the object and not just a value of the | |
| * object. | |
| * IN src_bytes - size of object pointed to by src | |
| * OUT dst_ptr - pointer to string to populate (caller must xfree()) | |
| * IN mime_type - mime type for dumping | |
| * IN flags - optional flags to specify to serilzier to change presentation of | |
| * data | |
| * IN data_parser - data_parser plugin to dump with, or NULL for the default | |
| * IN caller - __func__ from caller for logging | |
| * RET SLURM_SUCCESS or error | |
| */ | |
| extern int sercli_dump_str(data_parser_type_t type, void *db_conn, void *src, | |
| ssize_t src_bytes, char **dst_ptr, | |
| const char *mime_type, | |
| const serializer_flags_t flags, | |
| const char *data_parser, const char *caller); | |
| #define SERCLI_DUMP_STR(type, db_conn, src, dst, mime_type, flags, \ | |
| data_parser) \ | |
| sercli_dump_str(DATA_PARSER_##type, db_conn, &(src), sizeof(src), \ | |
| &(dst), mime_type, flags, data_parser, __func__) | |
| /* | |
| * Parse given string into target struct dst | |
| * NOTE: Use SERCLI_PARSE_STR() macro instead of calling directly! | |
| * NOTE: errors logged via openapi_error_log_foreach() | |
| * NOTE: warnings logged via openapi_warn_log_foreach() | |
| * IN parser - return from data_parser_g_new() | |
| * IN db_conn - database connection pointer | |
| * IN type - expected data_parser type of obj | |
| * IN dst - ptr to struct/scalar to populate | |
| * This *must* be a pointer to the object and not just a value of the | |
| * object. | |
| * IN dst_bytes - size of object pointed to by dst | |
| * IN src - string to parse into obj. | |
| * IN src_bytes - number of bytes in string to parse. | |
| * IN mime_type - deserialize data using given mime_type | |
| * IN caller - __func__ from caller | |
| * RET SLURM_SUCCESS or error | |
| */ | |
| extern int sercli_parse_str(data_parser_type_t type, void *db_conn, void *dst, | |
| ssize_t dst_bytes, const char *src, | |
| const size_t src_bytes, const char *mime_type, | |
| const char *caller); | |
| #define SERCLI_PARSE_STR(type, db_conn, dst, src, src_bytes, mime_type) \ | |
| sercli_parse_str(DATA_PARSER_##type, db_conn, &dst, sizeof(dst), src, \ | |
| src_bytes, mime_type, __func__) | |
| /* | |
| * Create data_parser instance for CLI | |
| * IN data_parser - data_parser parameters | |
| * RET parser ptr | |
| * Must be freed by call to data_parser_g_free() | |
| */ | |
| extern data_parser_t *data_parser_cli_parser(const char *data_parser, | |
| void *arg); | |
| /* Externally to data_parser add an error to data_parser cli output */ | |
| extern void data_parser_cli_on_error(data_parser_t *parser, int error_code, | |
| const char *source, const char *why, ...); | |
| /* Externally to data_parser add a warning to data_parser cli output */ | |
| extern void data_parser_cli_on_warn(data_parser_t *parser, const char *source, | |
| const char *why, ...); | |
| #endif |