From 5b74c7b1ca96399ba5c8bd0ce6974aea671198eb Mon Sep 17 00:00:00 2001 From: David Goulet Date: Fri, 29 Apr 2011 12:04:02 -0400 Subject: [PATCH] Move session data and functions to session.c/.h Moving all session data to a specific C file. Basic changes to the code was necessary to make functions and variables non-static. Adds a get_session_count to protect direct changes to the session counter. Also, rename ltt-sessiond.c to main.c. Signed-off-by: David Goulet --- ltt-sessiond/Makefile.am | 3 +- ltt-sessiond/ltt-sessiond.h | 20 -- ltt-sessiond/{ltt-sessiond.c => main.c} | 209 +------------------- ltt-sessiond/session.c | 242 ++++++++++++++++++++++++ ltt-sessiond/session.h | 48 +++++ 5 files changed, 295 insertions(+), 227 deletions(-) rename ltt-sessiond/{ltt-sessiond.c => main.c} (83%) create mode 100644 ltt-sessiond/session.c create mode 100644 ltt-sessiond/session.h diff --git a/ltt-sessiond/Makefile.am b/ltt-sessiond/Makefile.am index 479309189..00c0e8958 100644 --- a/ltt-sessiond/Makefile.am +++ b/ltt-sessiond/Makefile.am @@ -1,8 +1,9 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/liblttsessiondcomm +AM_CFLAGS = -fno-strict-aliasing bin_PROGRAMS = ltt-sessiond -ltt_sessiond_SOURCES = ltt-sessiond.c +ltt_sessiond_SOURCES = session.c main.c ltt_sessiond_LDADD = \ $(top_builddir)/liblttsessiondcomm/liblttsessiondcomm.la diff --git a/ltt-sessiond/ltt-sessiond.h b/ltt-sessiond/ltt-sessiond.h index 6c0824b80..f01fa2e16 100644 --- a/ltt-sessiond/ltt-sessiond.h +++ b/ltt-sessiond/ltt-sessiond.h @@ -13,7 +13,6 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ #ifndef _LTT_SESSIOND_H @@ -49,11 +48,6 @@ struct ltt_ust_marker { char *channel; }; -/* Global session list */ -struct ltt_session_list { - struct cds_list_head head; -}; - /* Traceable application list */ struct ltt_traceable_app_list { struct cds_list_head head; @@ -70,18 +64,4 @@ struct ltt_traceable_app { uid_t uid; /* User ID that owns the apps */ }; -/* - * ltt-session - This data structure contains information needed - * to identify a tracing session for both LTTng and UST. - */ -struct ltt_session { - char *name; - struct cds_list_head list; - uuid_t uuid; - struct cds_list_head ust_traces; - struct cds_list_head lttng_traces; - pid_t ust_consumer; - pid_t lttng_consumer; -}; - #endif /* _LTT_SESSIOND_H */ diff --git a/ltt-sessiond/ltt-sessiond.c b/ltt-sessiond/main.c similarity index 83% rename from ltt-sessiond/ltt-sessiond.c rename to ltt-sessiond/main.c index 4f9ca1bd4..418875a2a 100644 --- a/ltt-sessiond/ltt-sessiond.c +++ b/ltt-sessiond/main.c @@ -40,6 +40,7 @@ #include "liblttsessiondcomm.h" #include "ltt-sessiond.h" #include "lttngerr.h" +#include "session.h" /* Const values */ const char default_home_dir[] = DEFAULT_HOME_DIR; @@ -63,22 +64,13 @@ static int send_unix_sock(int sock, void *buf, size_t len); static int setup_data_buffer(char **buf, size_t size, struct lttcomm_lttng_msg *llm); static void add_traceable_app(struct ltt_traceable_app *lta); static void del_traceable_app(struct ltt_traceable_app *lta); -static void add_session_list(struct ltt_session *ls); -static void del_session_list(struct ltt_session *ls); /* Command function */ static void get_list_apps(pid_t *pids); -static void get_list_sessions(struct lttng_session *lt); static void *thread_manage_clients(void *data); static void *thread_manage_apps(void *data); -static int create_session(char *name, uuid_t *session_id); -static int destroy_session(uuid_t *uuid); - -static struct ltt_session *find_session_by_uuid(uuid_t session_id); -static struct ltt_session *find_session_by_name(char *name); - /* Variables */ const char *progname; const char *opt_tracing_group; @@ -98,14 +90,8 @@ static int apps_socket; static struct ltt_session *current_session; /* Number of element for the list below. */ -static unsigned int session_count; static unsigned int traceable_app_count; -/* Init session's list */ -static struct ltt_session_list ltt_session_list = { - .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), -}; - /* Init ust traceabl application's list */ static struct ltt_traceable_app_list ltt_traceable_app_list = { .head = CDS_LIST_HEAD_INIT(ltt_traceable_app_list.head), @@ -266,31 +252,6 @@ static void del_traceable_app(struct ltt_traceable_app *lta) pthread_mutex_unlock(<t_traceable_app_list_mutex); } -/* - * add_session_list - * - * Add a ltt_session structure to the global list. - */ -static void add_session_list(struct ltt_session *ls) -{ - cds_list_add(&ls->list, <t_session_list.head); - session_count++; -} - -/* - * del_session_list - * - * Delete a ltt_session structure to the global list. - */ -static void del_session_list(struct ltt_session *ls) -{ - cds_list_del(&ls->list); - /* Sanity check */ - if (session_count != 0) { - session_count--; - } -} - /* * send_unix_sock * @@ -387,145 +348,6 @@ static int find_app_by_pid(pid_t pid) return 0; } -/* - * find_session_by_uuid - * - * Return a ltt_session structure ptr that matches the uuid. - */ -static struct ltt_session *find_session_by_uuid(uuid_t session_id) -{ - int found = 0; - struct ltt_session *iter; - - /* Sanity check for NULL session_id */ - if (uuid_is_null(session_id)) { - goto end; - } - - cds_list_for_each_entry(iter, <t_session_list.head, list) { - if (uuid_compare(iter->uuid, session_id) == 0) { - found = 1; - break; - } - } - -end: - if (!found) { - iter = NULL; - } - return iter; -} - -/* - * find_session_by_name - * - * Return a ltt_session structure ptr that matches name. - * If no session found, NULL is returned. - */ -static struct ltt_session *find_session_by_name(char *name) -{ - int found = 0; - struct ltt_session *iter; - - cds_list_for_each_entry(iter, <t_session_list.head, list) { - if (strncmp(iter->name, name, strlen(iter->name)) == 0) { - found = 1; - break; - } - } - - if (!found) { - iter = NULL; - } - - return iter; -} - -/* - * destroy_session - * - * Delete session from the global session list - * and free the memory. - * - * Return -1 if no session is found. - * On success, return 1; - */ -static int destroy_session(uuid_t *uuid) -{ - int found = -1; - struct ltt_session *iter; - - cds_list_for_each_entry(iter, <t_session_list.head, list) { - if (uuid_compare(iter->uuid, *uuid) == 0) { - del_session_list(iter); - free(iter); - found = 1; - break; - } - } - - return found; -} - -/* - * create_session - * - * Create a brand new session and add it to the - * global session list. - */ -static int create_session(char *name, uuid_t *session_id) -{ - struct ltt_session *new_session; - - new_session = find_session_by_name(name); - if (new_session != NULL) { - goto error; - } - - /* Allocate session data structure */ - new_session = malloc(sizeof(struct ltt_session)); - if (new_session == NULL) { - perror("malloc"); - goto error_mem; - } - - if (name != NULL) { - if (asprintf(&new_session->name, "%s", name) < 0) { - goto error_mem; - } - } else { - /* Generate session name based on the session count */ - if (asprintf(&new_session->name, "%s%d", "lttng-", session_count) < 0) { - goto error_mem; - } - } - - /* UUID generation */ - uuid_generate(new_session->uuid); - uuid_copy(*session_id, new_session->uuid); - - /* Set consumer (identifier) to 0. This means that there is - * NO consumer attach to that session yet. - */ - new_session->ust_consumer = 0; - new_session->lttng_consumer = 0; - - /* Init list */ - CDS_INIT_LIST_HEAD(&new_session->ust_traces); - CDS_INIT_LIST_HEAD(&new_session->lttng_traces); - - /* Add new session to the global session list */ - add_session_list(new_session); - - return 0; - -error: - return -1; - -error_mem: - return -ENOMEM; -} - /* * ust_create_trace * @@ -593,32 +415,6 @@ static void get_list_apps(pid_t *pids) pthread_mutex_unlock(<t_traceable_app_list_mutex); } -/* - * get_list_sessions - * - * List sessions and fill the data buffer. - */ -static void get_list_sessions(struct lttng_session *lt) -{ - int i = 0; - struct ltt_session *iter; - struct lttng_session lsess; - - /* Iterate over session list and append data after - * the control struct in the buffer. - */ - cds_list_for_each_entry(iter, <t_session_list.head, list) { - /* Copy name and uuid */ - uuid_unparse(iter->uuid, lsess.uuid); - strncpy(lsess.name, iter->name, sizeof(lsess.name)); - lsess.name[sizeof(lsess.name) - 1] = '\0'; - memcpy(<[i], &lsess, sizeof(lsess)); - i++; - /* Reset struct for next pass */ - memset(&lsess, 0, sizeof(lsess)); - } -} - /* * copy_common_data * @@ -779,6 +575,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) } case LTTNG_LIST_SESSIONS: { + unsigned int session_count = get_session_count(); /* Stop right now if no session */ if (session_count == 0) { ret = LTTCOMM_NO_SESS; @@ -793,7 +590,7 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) goto end; } - get_list_sessions((struct lttng_session *)(send_buf + header_size)); + get_lttng_session((struct lttng_session *)(send_buf + header_size)); break; } diff --git a/ltt-sessiond/session.c b/ltt-sessiond/session.c new file mode 100644 index 000000000..c6bc285cd --- /dev/null +++ b/ltt-sessiond/session.c @@ -0,0 +1,242 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#include "lttngerr.h" +#include "session.h" + +/* Variables */ +static unsigned int session_count; + +/* Static internal function */ +static void add_session_list(struct ltt_session *ls); +static void del_session_list(struct ltt_session *ls); + +/* Init session's list */ +static struct ltt_session_list ltt_session_list = { + .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), +}; + +/* + * get_session_count + * + * Return session_count + */ +unsigned int get_session_count(void) +{ + return session_count; +} + +/* + * add_session_list + * + * Add a ltt_session structure to the global list. + */ +static void add_session_list(struct ltt_session *ls) +{ + cds_list_add(&ls->list, <t_session_list.head); + session_count++; +} + +/* + * del_session_list + * + * Delete a ltt_session structure to the global list. + */ +static void del_session_list(struct ltt_session *ls) +{ + cds_list_del(&ls->list); + /* Sanity check */ + if (session_count != 0) { + session_count--; + } +} + +/* + * find_session_by_uuid + * + * Return a ltt_session structure ptr that matches the uuid. + */ +struct ltt_session *find_session_by_uuid(uuid_t session_id) +{ + int found = 0; + struct ltt_session *iter; + + /* Sanity check for NULL session_id */ + if (uuid_is_null(session_id)) { + goto end; + } + + cds_list_for_each_entry(iter, <t_session_list.head, list) { + if (uuid_compare(iter->uuid, session_id) == 0) { + found = 1; + break; + } + } + +end: + if (!found) { + iter = NULL; + } + return iter; +} + +/* + * find_session_by_name + * + * Return a ltt_session structure ptr that matches name. + * If no session found, NULL is returned. + */ +struct ltt_session *find_session_by_name(char *name) +{ + int found = 0; + struct ltt_session *iter; + + cds_list_for_each_entry(iter, <t_session_list.head, list) { + if (strncmp(iter->name, name, strlen(iter->name)) == 0) { + found = 1; + break; + } + } + + if (!found) { + iter = NULL; + } + + return iter; +} + +/* + * destroy_session + * + * Delete session from the global session list + * and free the memory. + * + * Return -1 if no session is found. + * On success, return 1; + */ +int destroy_session(uuid_t *uuid) +{ + int found = -1; + struct ltt_session *iter; + + cds_list_for_each_entry(iter, <t_session_list.head, list) { + if (uuid_compare(iter->uuid, *uuid) == 0) { + del_session_list(iter); + free(iter); + found = 1; + break; + } + } + + return found; +} + +/* + * create_session + * + * Create a brand new session and add it to the + * global session list. + */ +int create_session(char *name, uuid_t *session_id) +{ + struct ltt_session *new_session; + + new_session = find_session_by_name(name); + if (new_session != NULL) { + goto error; + } + + /* Allocate session data structure */ + new_session = malloc(sizeof(struct ltt_session)); + if (new_session == NULL) { + perror("malloc"); + goto error_mem; + } + + if (name != NULL) { + if (asprintf(&new_session->name, "%s", name) < 0) { + goto error_mem; + } + } else { + /* Generate session name based on the session count */ + if (asprintf(&new_session->name, "%s%d", "lttng-", session_count) < 0) { + goto error_mem; + } + } + + /* UUID generation */ + uuid_generate(new_session->uuid); + uuid_copy(*session_id, new_session->uuid); + + /* Set consumer (identifier) to 0. This means that there is + * NO consumer attach to that session yet. + */ + new_session->ust_consumer = 0; + new_session->lttng_consumer = 0; + + /* Init list */ + CDS_INIT_LIST_HEAD(&new_session->ust_traces); + CDS_INIT_LIST_HEAD(&new_session->lttng_traces); + + /* Add new session to the global session list */ + add_session_list(new_session); + + return 0; + +error: + return -1; + +error_mem: + return -ENOMEM; +} + +/* + * get_lttng_session + * + * Iterate over the global session list and + * fill the lttng_session array. + */ +void get_lttng_session(struct lttng_session *lt) +{ + int i = 0; + struct ltt_session *iter; + struct lttng_session lsess; + + /* Iterate over session list and append data after + * the control struct in the buffer. + */ + cds_list_for_each_entry(iter, <t_session_list.head, list) { + /* Copy name and uuid */ + uuid_unparse(iter->uuid, lsess.uuid); + strncpy(lsess.name, iter->name, sizeof(lsess.name)); + lsess.name[sizeof(lsess.name) - 1] = '\0'; + memcpy(<[i], &lsess, sizeof(lsess)); + i++; + /* Reset struct for next pass */ + memset(&lsess, 0, sizeof(lsess)); + } +} + diff --git a/ltt-sessiond/session.h b/ltt-sessiond/session.h new file mode 100644 index 000000000..5bfe16ac0 --- /dev/null +++ b/ltt-sessiond/session.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef _LTT_SESSION_H +#define _LTT_SESSION_H + +/* Global session list */ +struct ltt_session_list { + struct cds_list_head head; +}; + +/* ltt-session - This data structure contains information needed + * to identify a tracing session for both LTTng and UST. + */ +struct ltt_session { + char *name; + struct cds_list_head list; + uuid_t uuid; + struct cds_list_head ust_traces; + struct cds_list_head lttng_traces; + pid_t ust_consumer; + pid_t lttng_consumer; +}; + +/* Prototypes */ +int create_session(char *name, uuid_t *session_id); +int destroy_session(uuid_t *uuid); +void get_lttng_session(struct lttng_session *lt); +struct ltt_session *find_session_by_uuid(uuid_t session_id); +struct ltt_session *find_session_by_name(char *name); +unsigned int get_session_count(void); + +#endif /* _LTT_SESSION_H */ -- 2.34.1