From 95712e61b874404d462b41459e8fe658002ec52f Mon Sep 17 00:00:00 2001 From: Jule Anger Date: Wed, 30 Mar 2022 15:11:11 +0200 Subject: [PATCH] smbstatus: add a basic dictionary with open files Adds an empty json dictionary under the key "open_files" and adds foreach locked file a dictionary with information (path, filename and pending deletes) to the locked files dictionary. Uses path and filename as key. Only print to stdout, if json_output is not set. Signed-off-by: Jule Anger Reviewed-by: Ralph Boehme --- source3/utils/status.c | 36 +++++++++++------- source3/utils/status_json.c | 61 +++++++++++++++++++++++++++++++ source3/utils/status_json.h | 4 ++ source3/utils/status_json_dummy.c | 7 ++++ 4 files changed, 95 insertions(+), 13 deletions(-) diff --git a/source3/utils/status.c b/source3/utils/status.c index 007f97bf7bb..1b9729e562a 100644 --- a/source3/utils/status.c +++ b/source3/utils/status.c @@ -155,9 +155,12 @@ static int print_share_mode_stdout(struct traverse_state *state, static int prepare_share_mode(struct traverse_state *state) { - /* only print header line if there are open files */ - state->first = true; - + if (!state->json_output) { + /* only print header line if there are open files */ + state->first = true; + } else { + add_section_to_json(state, "open_files"); + } return 0; } @@ -297,16 +300,23 @@ static int print_share_mode(struct file_id fid, } timestr = time_to_asc((time_t)e->time.tv_sec); - print_share_mode_stdout(state, - pid, - user_str, - denymode, - (unsigned int)e->access_mask, - rw, - oplock, - d->servicepath, - filename, - timestr); + + if (!state->json_output) { + print_share_mode_stdout(state, + pid, + user_str, + denymode, + (unsigned int)e->access_mask, + rw, + oplock, + d->servicepath, + filename, + timestr); + } else { + print_share_mode_json(state, + d, + filename); + } } TALLOC_FREE(tmp_ctx); return 0; diff --git a/source3/utils/status_json.c b/source3/utils/status_json.c index 5ad91094b37..a11024e7d4b 100644 --- a/source3/utils/status_json.c +++ b/source3/utils/status_json.c @@ -22,6 +22,7 @@ #include "lib/util/time_basic.h" #include "conn_tdb.h" #include "session.h" +#include "librpc/gen_ndr/open_files.h" #include "status_json.h" #include "../libcli/security/security.h" #include "status.h" @@ -358,3 +359,63 @@ failure: TALLOC_FREE(tmp_ctx); return -1; } + +int print_share_mode_json(struct traverse_state *state, + const struct share_mode_data *d, + const char *filename) +{ + struct json_object locks_json; + struct json_object file_json; + char *key = NULL; + int result = 0; + + TALLOC_CTX *tmp_ctx = talloc_stackframe(); + if (tmp_ctx == NULL) { + return -1; + } + + if (d->servicepath[strlen(d->servicepath)-1] == '/') { + key = talloc_asprintf(tmp_ctx, "%s%s", d->servicepath, filename); + } else { + key = talloc_asprintf(tmp_ctx, "%s/%s", d->servicepath, filename); + } + + locks_json = json_get_object(&state->root_json, "open_files"); + if (json_is_invalid(&locks_json)) { + goto failure; + } + file_json = json_get_object(&locks_json, key); + if (json_is_invalid(&file_json)) { + goto failure; + } + + result = json_add_string(&file_json, "service_path", d->servicepath); + if (result < 0) { + goto failure; + } + result = json_add_string(&file_json, "filename", filename); + if (result < 0) { + goto failure; + } + result = json_add_int(&file_json, "num_pending_deletes", d->num_delete_tokens); + if (result < 0) { + goto failure; + } + + result = json_update_object(&locks_json, key, &file_json); + if (result < 0) { + goto failure; + } + result = json_update_object(&state->root_json, "open_files", &locks_json); + if (result < 0) { + goto failure; + } + + TALLOC_FREE(tmp_ctx); + return 0; +failure: + json_free(&file_json); + json_free(&locks_json); + TALLOC_FREE(tmp_ctx); + return -1; +} diff --git a/source3/utils/status_json.h b/source3/utils/status_json.h index 72fa9f98fd0..44b339914ac 100644 --- a/source3/utils/status_json.h +++ b/source3/utils/status_json.h @@ -44,4 +44,8 @@ int traverse_sessionid_json(struct traverse_state *state, enum crypto_degree signing_degree, const char *connection_dialect); +int print_share_mode_json(struct traverse_state *state, + const struct share_mode_data *d, + const char *filename); + #endif diff --git a/source3/utils/status_json_dummy.c b/source3/utils/status_json_dummy.c index 6530b2f8f07..96b6bc6b569 100644 --- a/source3/utils/status_json_dummy.c +++ b/source3/utils/status_json_dummy.c @@ -57,3 +57,10 @@ int traverse_sessionid_json(struct traverse_state *state, { return 0; } + +int print_share_mode_json(struct traverse_state *state, + const struct share_mode_data *d, + const char *filename) +{ + return 0; +} -- 2.47.3