Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadavg: make cleanup of start_loadavg #612

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/lxcfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,37 @@ static int start_loadavg(void)
{
char *error;
pthread_t (*__load_daemon)(int);
int (*__load_daemon_v2)(pthread_t *, int);

/* try a new load_daemon_v2() API */
dlerror();
__load_daemon_v2 = (int (*)(pthread_t *, int))dlsym(dlopen_handle, "load_daemon_v2");
error = dlerror();
if (error)
/* try with an old symbol name */
goto old_api;

lxcfs_debug("start_loadavg: using load_daemon_v2");

if (__load_daemon_v2(&loadavg_pid, 1)) {
/* we have to NULLify loadavg_pid as in case of error it's contents are undefined */
loadavg_pid = 0;
return log_error(-1, "Failed to start loadavg daemon");
}

/* we are done */
return 0;

old_api:
/* go with an old load_daemon() API */
dlerror();
__load_daemon = (pthread_t(*)(int))dlsym(dlopen_handle, "load_daemon");
error = dlerror();
if (error)
return log_error(-1, "%s - Failed to start loadavg daemon", error);

lxcfs_debug("start_loadavg: using load_daemon");

loadavg_pid = __load_daemon(1);
if (!loadavg_pid)
return -1;
Expand Down
24 changes: 22 additions & 2 deletions src/proc_loadavg.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ static void load_free(void)
}
}

/* Return a positive number on success, return 0 on failure.*/
/* Return a positive number on success, return 0 on failure. */
pthread_t load_daemon(int load_use)
{
int ret;
Expand All @@ -644,11 +644,31 @@ pthread_t load_daemon(int load_use)
return (pthread_t)log_error(0, "Create pthread fails in load_daemon!");
}

/* use loadavg, here loadavg = 1*/
/* use loadavg, here loadavg = 1 */
loadavg = load_use;
return pid;
}

/* Return 0 on success, return -1 on failure. */
int load_daemon_v2(pthread_t *thread, int load_use)
{
int ret;

ret = init_load();
if (ret == -1)
return log_error(-1, "Initialize hash_table fails in load_daemon!");

ret = pthread_create(thread, NULL, load_begin, NULL);
if (ret != 0) {
load_free();
return log_error(-1, "%s - Create pthread fails in load_daemon!", strerror(ret));
}

/* use loadavg, here loadavg = 1 */
loadavg = load_use;
return 0;
}

/* Returns 0 on success. */
int stop_load_daemon(pthread_t pid)
{
Expand Down
1 change: 1 addition & 0 deletions src/proc_loadavg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "macro.h"

__visible extern pthread_t load_daemon(int load_use);
__visible extern int load_daemon_v2(pthread_t *thread, int load_use);
__visible extern int stop_load_daemon(pthread_t pid);

extern int proc_loadavg_read(char *buf, size_t size, off_t offset, struct fuse_file_info *fi);
Expand Down
Loading