set_ulimit();
}
- if (run_as_create_worker(argv[0]) < 0) {
+ if (run_as_create_worker(argv[0], NULL, NULL) < 0) {
goto exit_init_data;
}
DBG("Destruction of all sessions completed");
}
+static int run_as_worker_post_fork_cleanup(void *data)
+{
+ struct sessiond_config *sessiond_config = data;
+
+ sessiond_config_fini(sessiond_config);
+ return 0;
+}
+
+static int launch_run_as_worker(const char *procname)
+{
+ /*
+ * Clean-up before forking the run-as worker. Any dynamically
+ * allocated memory of which the worker is not aware will
+ * be leaked as the process forks a run-as worker (and performs
+ * no exec*()). The same would apply to any opened fd.
+ */
+ return run_as_create_worker(procname, run_as_worker_post_fork_cleanup,
+ &config);
+}
+
/*
* main
*/
}
}
- if (run_as_create_worker(argv[0]) < 0) {
+ if (launch_run_as_worker(argv[0]) < 0) {
goto exit_create_run_as_worker_cleanup;
}
}
static
-int run_as_create_worker_no_lock(const char *procname)
+int run_as_create_worker_no_lock(const char *procname,
+ post_fork_cleanup_cb clean_up_func,
+ void *clean_up_user_data)
{
pid_t pid;
int i, ret = 0;
reset_sighandler();
set_worker_sighandlers();
+ if (clean_up_func) {
+ if (clean_up_func(clean_up_user_data) < 0) {
+ ERR("Run-as post-fork clean-up failed, exiting.");
+ exit(EXIT_FAILURE);
+ }
+ }
/* Just close, no shutdown. */
if (close(worker->sockpair[0])) {
run_as_destroy_worker();
/* Create a new run_as worker process*/
- ret = run_as_create_worker_no_lock(procname);
+ ret = run_as_create_worker_no_lock(procname, NULL, NULL);
if (ret < 0 ) {
ERR("Restarting the worker process failed");
ret = -1;
}
LTTNG_HIDDEN
-int run_as_create_worker(const char *procname)
+int run_as_create_worker(const char *procname,
+ post_fork_cleanup_cb clean_up_func,
+ void *clean_up_user_data)
{
int ret;
pthread_mutex_lock(&worker_lock);
- ret = run_as_create_worker_no_lock(procname);
+ ret = run_as_create_worker_no_lock(procname, clean_up_func,
+ clean_up_user_data);
pthread_mutex_unlock(&worker_lock);
return ret;
}
#include <sys/types.h>
#include <unistd.h>
+/*
+ * The run-as process is launched by forking without an exec*() call. This means
+ * that any resource allocated before the run-as worker is launched should be
+ * cleaned-up after the fork(). This callback allows the user to perform this
+ * clean-up.
+ *
+ * Note that the callback will _not_ be invoked if the LTTNG_DEBUG_NOCLONE
+ * environment variable is set as the clean-up is not needed (and may not be
+ * expected).
+ *
+ * A negative return value will cause the run-as process to exit with a non-zero
+ * value.
+ */
+typedef int (*post_fork_cleanup_cb)(void *user_data);
+
LTTNG_HIDDEN
int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid);
LTTNG_HIDDEN
const char* probe_name, uid_t uid, gid_t gid,
uint64_t **offsets, uint32_t *num_offset);
LTTNG_HIDDEN
-int run_as_create_worker(const char *procname);
+int run_as_create_worker(const char *procname,
+ post_fork_cleanup_cb clean_up_func, void *clean_up_user_data);
LTTNG_HIDDEN
void run_as_destroy_worker(void);