Debian Codegraph @

src/test/nss-test-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <dlfcn.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 #include "log.h"
8 #include "nss-test-util.h"
9 #include "string-util.h"
10
11 const char* nss_status_to_string(enum nss_status status, char *buf, size_t buf_len) {
12 switch (status) {
13 case NSS_STATUS_TRYAGAIN:
14 return "NSS_STATUS_TRYAGAIN";
15 case NSS_STATUS_UNAVAIL:
16 return "NSS_STATUS_UNAVAIL";
17 case NSS_STATUS_NOTFOUND:
18 return "NSS_STATUS_NOTFOUND";
19 case NSS_STATUS_SUCCESS:
20 return "NSS_STATUS_SUCCESS";
21 case NSS_STATUS_RETURN:
22 return "NSS_STATUS_RETURN";
23 default:
24 (void) snprintf(buf, buf_len, "%i", status);
25 return buf;
26 }
27 };
28
29 void* nss_open_handle(const char *dir, const char *module, int flags) {
30 const char *path = NULL;
31 void *handle;
32
33 if (dir)
34 path = strjoina(dir, "/libnss_", module, ".so.2");
35 if (!path || access(path, F_OK) < 0)
36 path = strjoina("libnss_", module, ".so.2");
37
38 log_debug("Using %s", path);
39 handle = dlopen(path, flags);
40 if (!handle)
41 log_error("Failed to load module %s: %s", module, dlerror());
42 return handle;
43 }