From: Jule Anger Date: Wed, 30 Mar 2022 13:36:13 +0000 (+0200) Subject: smbstatus: add opens to files in json output X-Git-Tag: samba-4.17.0rc1~23 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8d26456742a08991016320fa9cf46598b15affe0;p=thirdparty%2Fsamba.git smbstatus: add opens to files in json output At the moment, there is only information about the open files. Adds a list of its opens for each file. An open is represented as a dictionary. Contains only the basic information (pid, uid and time) about the open. Signed-off-by: Jule Anger Reviewed-by: Ralph Boehme --- diff --git a/source3/utils/status.c b/source3/utils/status.c index 7b1a5057f8f..da9510a38d4 100644 --- a/source3/utils/status.c +++ b/source3/utils/status.c @@ -315,7 +315,10 @@ static int print_share_mode(struct file_id fid, } else { print_share_mode_json(state, d, + e, fid, + pid, + user_str, filename); } } diff --git a/source3/utils/status_json.c b/source3/utils/status_json.c index 441f51fa1e4..e0da77192cf 100644 --- a/source3/utils/status_json.c +++ b/source3/utils/status_json.c @@ -360,6 +360,70 @@ failure: return -1; } +static int add_open_to_json(struct json_object *parent_json, + const struct share_mode_entry *e, + bool resolve_uids, + const char *pid, + const char *uid_str) +{ + struct json_object sub_json; + struct json_object opens_json; + int result = 0; + char *key = NULL; + char *share_file_id = NULL; + + TALLOC_CTX *tmp_ctx = talloc_stackframe(); + if (tmp_ctx == NULL) { + return -1; + } + + opens_json = json_get_object(parent_json, "opens"); + if (json_is_invalid(&opens_json)) { + goto failure; + } + sub_json = json_new_object(); + if (json_is_invalid(&sub_json)) { + goto failure; + } + + result = json_add_string(&sub_json, "pid", pid); + if (result < 0) { + goto failure; + } + if (resolve_uids) { + result = json_add_string(&sub_json, "username", uid_str); + if (result < 0) { + goto failure; + } + } + result = json_add_int(&sub_json, "uid", e->uid); + if (result < 0) { + goto failure; + } + share_file_id = talloc_asprintf(tmp_ctx, "%lu", e->share_file_id); + result = json_add_string(&sub_json, "share_file_id", share_file_id); + if (result < 0) { + goto failure; + } + + key = talloc_asprintf(tmp_ctx, "%s/%lu", pid, e->share_file_id); + result = json_add_object(&opens_json, key, &sub_json); + if (result < 0) { + goto failure; + } + result = json_update_object(parent_json, "opens", &opens_json); + if (result < 0) { + goto failure; + } + + TALLOC_FREE(tmp_ctx); + return 0; +failure: + json_free(&opens_json); + json_free(&sub_json); + TALLOC_FREE(tmp_ctx); + return -1; +} static int add_fileid_to_json(struct json_object *parent_json, struct file_id fid) @@ -398,7 +462,10 @@ failure: int print_share_mode_json(struct traverse_state *state, const struct share_mode_data *d, + const struct share_mode_entry *e, struct file_id fid, + const char *pid, + const char *uid_str, const char *filename) { struct json_object locks_json; @@ -443,6 +510,15 @@ int print_share_mode_json(struct traverse_state *state, goto failure; } + result = add_open_to_json(&file_json, + e, + state->resolve_uids, + pid, + uid_str); + if (result < 0) { + goto failure; + } + result = json_update_object(&locks_json, key, &file_json); if (result < 0) { goto failure; diff --git a/source3/utils/status_json.h b/source3/utils/status_json.h index bd9c2611d26..5b803080878 100644 --- a/source3/utils/status_json.h +++ b/source3/utils/status_json.h @@ -46,7 +46,10 @@ int traverse_sessionid_json(struct traverse_state *state, int print_share_mode_json(struct traverse_state *state, const struct share_mode_data *d, + const struct share_mode_entry *e, struct file_id fid, + const char *pid, + const char *uid_str, const char *filename); #endif diff --git a/source3/utils/status_json_dummy.c b/source3/utils/status_json_dummy.c index c53c329c837..668a5a63fbd 100644 --- a/source3/utils/status_json_dummy.c +++ b/source3/utils/status_json_dummy.c @@ -60,7 +60,10 @@ int traverse_sessionid_json(struct traverse_state *state, int print_share_mode_json(struct traverse_state *state, const struct share_mode_data *d, + const struct share_mode_entry *e, struct file_id fid, + const char *pid, + const char *uid_str, const char *filename) { return 0;