diff options
author | Lennart Poettering <lennart@poettering.net> | 2024-07-12 11:22:58 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-07-16 11:57:32 +0200 |
commit | 25ff515b398da61171a570baee641290374cec1a (patch) | |
tree | a41558cc31c2ed18c4b3262ed1812cdc5e0cc360 /src/userdb | |
parent | Merge pull request #33695 from DaanDeMeyer/epel (diff) | |
download | systemd-25ff515b398da61171a570baee641290374cec1a.tar.xz systemd-25ff515b398da61171a570baee641290374cec1a.zip |
sd-varlink: make our internal Varlink API public as sd-varlink.[ch]
It's time. sd-json was already done earlier in this cycle, let's now
make sd-varlink public too.
This is mostly just a search/replace job of epical proportions.
I left some functions internal (mostly IDL handling), and I turned some
static inline calls into regular calls.
Diffstat (limited to 'src/userdb')
-rw-r--r-- | src/userdb/userwork.c | 89 |
1 files changed, 46 insertions, 43 deletions
diff --git a/src/userdb/userwork.c b/src/userdb/userwork.c index 8257acc473..5781d8c788 100644 --- a/src/userdb/userwork.c +++ b/src/userdb/userwork.c @@ -4,6 +4,7 @@ #include <sys/wait.h> #include "sd-daemon.h" +#include "sd-varlink.h" #include "env-util.h" #include "fd-util.h" @@ -17,7 +18,6 @@ #include "user-record.h" #include "user-util.h" #include "userdb.h" -#include "varlink.h" #include "varlink-io.systemd.UserDatabase.h" #define ITERATIONS_MAX 64U @@ -70,7 +70,7 @@ static int add_nss_service(sd_json_variant **v) { return sd_json_variant_set_field(v, "status", status); } -static int build_user_json(Varlink *link, UserRecord *ur, sd_json_variant **ret) { +static int build_user_json(sd_varlink *link, UserRecord *ur, sd_json_variant **ret) { _cleanup_(user_record_unrefp) UserRecord *stripped = NULL; _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; UserRecordLoadFlags flags; @@ -81,7 +81,7 @@ static int build_user_json(Varlink *link, UserRecord *ur, sd_json_variant **ret) assert(ur); assert(ret); - r = varlink_get_peer_uid(link, &peer_uid); + r = sd_varlink_get_peer_uid(link, &peer_uid); if (r < 0) { log_debug_errno(r, "Unable to query peer UID, ignoring: %m"); trusted = false; @@ -114,7 +114,7 @@ static int build_user_json(Varlink *link, UserRecord *ur, sd_json_variant **ret) SD_JSON_BUILD_PAIR("incomplete", SD_JSON_BUILD_BOOLEAN(stripped->incomplete))); } -static int userdb_flags_from_service(Varlink *link, const char *service, UserDBFlags *ret) { +static int userdb_flags_from_service(sd_varlink *link, const char *service, UserDBFlags *ret) { assert(link); assert(ret); @@ -125,12 +125,12 @@ static int userdb_flags_from_service(Varlink *link, const char *service, UserDBF else if (streq_ptr(service, "io.systemd.Multiplexer")) *ret = USERDB_AVOID_MULTIPLEXER; else - return varlink_error(link, "io.systemd.UserDatabase.BadService", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.BadService", NULL); return 0; } -static int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_user_record(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { static const sd_json_dispatch_field dispatch_table[] = { { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, @@ -149,7 +149,7 @@ static int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, assert(parameters); - r = varlink_dispatch(link, parameters, dispatch_table, &p); + r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); if (r != 0) return r; @@ -174,7 +174,7 @@ static int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, * implementation detail and always return NoRecordFound in this case, since from a * client's perspective it's irrelevant if there was no entry at all or just not on * the service that the query was limited to. */ - return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); if (r < 0) return r; @@ -188,7 +188,7 @@ static int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, return r; if (last) { - r = varlink_notify(link, last); + r = sd_varlink_notify(link, last); if (r < 0) return r; @@ -201,29 +201,29 @@ static int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, } if (!last) - return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); - return varlink_reply(link, last); + return sd_varlink_reply(link, last); } if (r == -ESRCH) - return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); if (r < 0) { log_debug_errno(r, "User lookup failed abnormally: %m"); - return varlink_error(link, "io.systemd.UserDatabase.ServiceNotAvailable", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.ServiceNotAvailable", NULL); } if ((uid_is_valid(p.uid) && hr->uid != p.uid) || (p.user_name && !streq(hr->user_name, p.user_name))) - return varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL); r = build_user_json(link, hr, &v); if (r < 0) return r; - return varlink_reply(link, v); + return sd_varlink_reply(link, v); } -static int build_group_json(Varlink *link, GroupRecord *gr, sd_json_variant **ret) { +static int build_group_json(sd_varlink *link, GroupRecord *gr, sd_json_variant **ret) { _cleanup_(group_record_unrefp) GroupRecord *stripped = NULL; _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; UserRecordLoadFlags flags; @@ -234,7 +234,7 @@ static int build_group_json(Varlink *link, GroupRecord *gr, sd_json_variant **re assert(gr); assert(ret); - r = varlink_get_peer_uid(link, &peer_uid); + r = sd_varlink_get_peer_uid(link, &peer_uid); if (r < 0) { log_debug_errno(r, "Unable to query peer UID, ignoring: %m"); trusted = false; @@ -267,7 +267,7 @@ static int build_group_json(Varlink *link, GroupRecord *gr, sd_json_variant **re SD_JSON_BUILD_PAIR("incomplete", SD_JSON_BUILD_BOOLEAN(stripped->incomplete))); } -static int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_group_record(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { static const sd_json_dispatch_field dispatch_table[] = { { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, @@ -286,7 +286,7 @@ static int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters assert(parameters); - r = varlink_dispatch(link, parameters, dispatch_table, &p); + r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); if (r != 0) return r; @@ -304,7 +304,7 @@ static int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters r = groupdb_all(userdb_flags, &iterator); if (IN_SET(r, -ESRCH, -ENOLINK)) - return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); if (r < 0) return r; @@ -318,7 +318,7 @@ static int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters return r; if (last) { - r = varlink_notify(link, last); + r = sd_varlink_notify(link, last); if (r < 0) return r; @@ -331,29 +331,29 @@ static int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters } if (!last) - return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); - return varlink_reply(link, last); + return sd_varlink_reply(link, last); } if (r == -ESRCH) - return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); if (r < 0) { log_debug_errno(r, "Group lookup failed abnormally: %m"); - return varlink_error(link, "io.systemd.UserDatabase.ServiceNotAvailable", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.ServiceNotAvailable", NULL); } if ((uid_is_valid(p.gid) && g->gid != p.gid) || (p.group_name && !streq(g->group_name, p.group_name))) - return varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL); r = build_group_json(link, g, &v); if (r < 0) return r; - return varlink_reply(link, v); + return sd_varlink_reply(link, v); } -static int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_memberships(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { static const sd_json_dispatch_field dispatch_table[] = { { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 }, { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 }, @@ -369,7 +369,7 @@ static int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, assert(parameters); - r = varlink_dispatch(link, parameters, dispatch_table, &p); + r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); if (r != 0) return r; @@ -384,7 +384,7 @@ static int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, else r = membershipdb_all(userdb_flags, &iterator); if (IN_SET(r, -ESRCH, -ENOLINK)) - return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); if (r < 0) return r; @@ -404,7 +404,7 @@ static int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, if (last_user_name) { assert(last_group_name); - r = varlink_notifybo( + r = sd_varlink_notifybo( link, SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(last_user_name)), SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(last_group_name))); @@ -418,31 +418,34 @@ static int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, if (!last_user_name) { assert(!last_group_name); - return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); + return sd_varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); } assert(last_group_name); - return varlink_replybo( + return sd_varlink_replybo( link, SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(last_user_name)), SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(last_group_name))); } -static int process_connection(VarlinkServer *server, int _fd) { +static int process_connection(sd_varlink_server *server, int _fd) { _cleanup_close_ int fd = TAKE_FD(_fd); /* always take possession */ - _cleanup_(varlink_close_unrefp) Varlink *vl = NULL; + _cleanup_(sd_varlink_close_unrefp) sd_varlink *vl = NULL; int r; - r = varlink_server_add_connection(server, fd, &vl); + assert(server); + assert(fd >= 0); + + r = sd_varlink_server_add_connection(server, fd, &vl); if (r < 0) return log_error_errno(r, "Failed to add connection: %m"); TAKE_FD(fd); - vl = varlink_ref(vl); + vl = sd_varlink_ref(vl); for (;;) { - r = varlink_process(vl); + r = sd_varlink_process(vl); if (r == -ENOTCONN) { log_debug("Connection terminated."); break; @@ -452,7 +455,7 @@ static int process_connection(VarlinkServer *server, int _fd) { if (r > 0) continue; - r = varlink_wait(vl, CONNECTION_IDLE_USEC); + r = sd_varlink_wait(vl, CONNECTION_IDLE_USEC); if (r < 0) return log_error_errno(r, "Failed to wait for connection events: %m"); if (r == 0) @@ -464,7 +467,7 @@ static int process_connection(VarlinkServer *server, int _fd) { static int run(int argc, char *argv[]) { usec_t start_time, listen_idle_usec, last_busy_usec = USEC_INFINITY; - _cleanup_(varlink_server_unrefp) VarlinkServer *server = NULL; + _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *server = NULL; _cleanup_(pidref_done) PidRef parent = PIDREF_NULL; unsigned n_iterations = 0; int m, listen_fd, r; @@ -485,15 +488,15 @@ static int run(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to turn off non-blocking mode for listening socket: %m"); - r = varlink_server_new(&server, 0); + r = sd_varlink_server_new(&server, 0); if (r < 0) return log_error_errno(r, "Failed to allocate server: %m"); - r = varlink_server_add_interface(server, &vl_interface_io_systemd_UserDatabase); + r = sd_varlink_server_add_interface(server, &vl_interface_io_systemd_UserDatabase); if (r < 0) return log_error_errno(r, "Failed to add UserDatabase interface to varlink server: %m"); - r = varlink_server_bind_method_many( + r = sd_varlink_server_bind_method_many( server, "io.systemd.UserDatabase.GetUserRecord", vl_method_get_user_record, "io.systemd.UserDatabase.GetGroupRecord", vl_method_get_group_record, |