Skip to content

Commit 38cd455

Browse files
committed
broker: use dirwalk to search for module DSO
Problem: the broker uses a custom function to search for a module DSO by pattern. Switch to dirwalk().
1 parent a046a68 commit 38cd455

File tree

1 file changed

+20
-44
lines changed

1 file changed

+20
-44
lines changed

src/broker/broker.c

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "src/common/libczmqcontainers/czmq_containers.h"
3939
#include "src/common/libutil/log.h"
4040
#include "src/common/libutil/cleanup.h"
41+
#include "src/common/libutil/dirwalk.h"
4142
#include "src/common/libidset/idset.h"
4243
#include "src/common/libutil/ipaddr.h"
4344
#include "src/common/libutil/fsd.h"
@@ -1188,43 +1189,6 @@ static int mod_svc_cb (const flux_msg_t *msg, void *arg)
11881189
return rc;
11891190
}
11901191

1191-
/* Find file <name>.ext in one of the colon-separated directories in
1192-
* 'searchpath' and place its full path in 'path' (caller must free).
1193-
* Return 0 on success, -1 on failure. Errno is not set.
1194-
*/
1195-
static int search_file_ext (const char *name,
1196-
const char *ext,
1197-
const char *searchpath,
1198-
char **path)
1199-
{
1200-
char *argz = NULL;
1201-
size_t argz_len = 0;
1202-
char *entry = NULL;
1203-
char *fullpath = NULL;
1204-
int e;
1205-
int rc = -1;
1206-
1207-
if ((e = argz_create_sep (searchpath, ':', &argz, &argz_len)) != 0)
1208-
return -1;
1209-
while ((entry = argz_next (argz, argz_len, entry))) {
1210-
char *s;
1211-
if (asprintf (&s, "%s/%s%s", entry, name, ext) < 0)
1212-
goto out;
1213-
if (access (s, F_OK | R_OK) == 0) {
1214-
fullpath = s;
1215-
break;
1216-
}
1217-
free (s);
1218-
}
1219-
if (fullpath) {
1220-
*path = fullpath;
1221-
rc = 0;
1222-
}
1223-
out:
1224-
free (argz);
1225-
return rc;
1226-
}
1227-
12281192
/* Load broker module.
12291193
* 'name' is the name to use for the module (NULL = use dso basename minus .so)
12301194
* 'path' is either a dso path or a dso basename (e.g. "kvs" or "/a/b/kvs.so".
@@ -1237,7 +1201,8 @@ static int load_module (broker_ctx_t *ctx,
12371201
flux_error_t *error)
12381202
{
12391203
const char *searchpath;
1240-
char *fullpath = NULL;
1204+
char *pattern = NULL;
1205+
zlist_t *files = NULL;
12411206
module_t *p;
12421207

12431208
if (!strchr (path, '/')) {
@@ -1246,12 +1211,22 @@ static int load_module (broker_ctx_t *ctx,
12461211
errno = EINVAL;
12471212
return -1;
12481213
}
1249-
if (search_file_ext (path, ".so", searchpath, &fullpath) < 0) {
1214+
if (asprintf (&pattern, "%s.so", path) < 0) {
1215+
errprintf (error, "out of memory");
1216+
return -1;
1217+
}
1218+
if (!(files = dirwalk_find (searchpath,
1219+
DIRWALK_REALPATH,
1220+
pattern,
1221+
1,
1222+
NULL,
1223+
NULL))
1224+
|| zlist_size (files) == 0) {
12501225
errprintf (error, "module not found in search path");
12511226
errno = ENOENT;
1252-
return -1;
1227+
goto error;
12531228
}
1254-
path = fullpath;
1229+
path = zlist_first (files);
12551230
}
12561231
if (!(p = module_add (ctx->modhash, name, path, args, error)))
12571232
goto error;
@@ -1274,15 +1249,16 @@ static int load_module (broker_ctx_t *ctx,
12741249
goto service_remove;
12751250
}
12761251
flux_log (ctx->h, LOG_DEBUG, "insmod %s", module_get_name (p));
1277-
free (fullpath);
1278-
1252+
zlist_destroy (&files);
1253+
free (pattern);
12791254
return 0;
12801255
service_remove:
12811256
service_remove_byuuid (ctx->services, module_get_uuid (p));
12821257
module_remove:
12831258
module_remove (ctx->modhash, p);
12841259
error:
1285-
ERRNO_SAFE_WRAP (free, fullpath);
1260+
ERRNO_SAFE_WRAP (zlist_destroy, &files);
1261+
ERRNO_SAFE_WRAP (free, pattern);
12861262
return -1;
12871263
}
12881264

0 commit comments

Comments
 (0)