Cleanup comments and bad indent
[lttng-tools.git] / ltt-sessiond / traceable-app.c
CommitLineData
91d76f53
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
91d76f53
DG
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#define _GNU_SOURCE
20#include <errno.h>
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <urcu/list.h>
25
26#include "lttngerr.h"
27#include "traceable-app.h"
28
29/* Number of element for the list below. */
30static unsigned int traceable_app_count;
31
32/* Init ust traceabl application's list */
33struct ltt_traceable_app_list ltt_traceable_app_list = {
34 .head = CDS_LIST_HEAD_INIT(ltt_traceable_app_list.head),
35};
36
37/* List mutex */
38pthread_mutex_t ltt_traceable_app_list_mutex;
39
40/* Internal function */
41static void add_traceable_app(struct ltt_traceable_app *lta);
42static void del_traceable_app(struct ltt_traceable_app *lta);
43
44/*
050349bb
DG
45 * Add a traceable application structure to the global list protected by a
46 * mutex.
91d76f53
DG
47 */
48static void add_traceable_app(struct ltt_traceable_app *lta)
49{
50 pthread_mutex_lock(&ltt_traceable_app_list_mutex);
51 cds_list_add(&lta->list, &ltt_traceable_app_list.head);
52 traceable_app_count++;
53 pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
54}
55
56/*
050349bb
DG
57 * Delete a traceable application structure from the global list protected by a
58 * mutex.
91d76f53
DG
59 */
60static void del_traceable_app(struct ltt_traceable_app *lta)
61{
62 pthread_mutex_lock(&ltt_traceable_app_list_mutex);
63 cds_list_del(&lta->list);
64 /* Sanity check */
65 if (traceable_app_count != 0) {
66 traceable_app_count--;
67 }
68 pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
69}
70
71/*
050349bb
DG
72 * Using pid and uid (of the app), allocate a new ltt_traceable_app struct and
73 * add it to the global traceable app list.
91d76f53 74 *
050349bb 75 * On success, return 0, else return malloc ENOMEM.
91d76f53
DG
76 */
77int register_traceable_app(pid_t pid, uid_t uid)
78{
79 struct ltt_traceable_app *lta;
80
81 lta = malloc(sizeof(struct ltt_traceable_app));
82 if (lta == NULL) {
83 perror("malloc");
84 return -ENOMEM;
85 }
86
87 lta->uid = uid;
88 lta->pid = pid;
89 add_traceable_app(lta);
e07ae692 90 DBG("Application %d registered with UID %d", pid, uid);
91d76f53
DG
91
92 return 0;
93}
94
95/*
050349bb
DG
96 * Unregister app by removing it from the global traceable app list and freeing
97 * the data struct.
91d76f53
DG
98 */
99void unregister_traceable_app(pid_t pid)
100{
101 struct ltt_traceable_app *lta;
102
103 lta = find_app_by_pid(pid);
104 if (lta != NULL) {
105 del_traceable_app(lta);
106 free(lta);
107 DBG("PID %d unregistered", pid);
108 }
109}
110
111/*
050349bb 112 * Return traceable_app_count
91d76f53
DG
113 */
114unsigned int get_app_count(void)
115{
116 return traceable_app_count;
117}
118
119/*
050349bb
DG
120 * Iterate over the traceable apps list and return a pointer or NULL if not
121 * found.
91d76f53
DG
122 */
123struct ltt_traceable_app *find_app_by_pid(pid_t pid)
124{
125 struct ltt_traceable_app *iter;
126
127 pthread_mutex_lock(&ltt_traceable_app_list_mutex);
128 cds_list_for_each_entry(iter, &ltt_traceable_app_list.head, list) {
129 if (iter->pid == pid) {
130 pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
131 /* Found */
132 return iter;
133 }
134 }
135 pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
136
137 return NULL;
138}
139
140/*
050349bb 141 * List traceable user-space application and fill an array of pids.
91d76f53
DG
142 */
143void get_app_list_pids(pid_t *pids)
144{
145 int i = 0;
146 struct ltt_traceable_app *iter;
147
148 /* Protected by a mutex here because the threads manage_client
149 * and manage_apps can access this list.
150 */
151 pthread_mutex_lock(&ltt_traceable_app_list_mutex);
152 cds_list_for_each_entry(iter, &ltt_traceable_app_list.head, list) {
153 pids[i] = iter->pid;
154 i++;
155 }
156 pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
157}
This page took 0.040524 seconds and 4 git commands to generate.