| 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 |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 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. */ |
| 30 | static unsigned int traceable_app_count; |
| 31 | |
| 32 | /* Init ust traceabl application's list */ |
| 33 | struct ltt_traceable_app_list ltt_traceable_app_list = { |
| 34 | .head = CDS_LIST_HEAD_INIT(ltt_traceable_app_list.head), |
| 35 | }; |
| 36 | |
| 37 | /* List mutex */ |
| 38 | pthread_mutex_t ltt_traceable_app_list_mutex; |
| 39 | |
| 40 | /* Internal function */ |
| 41 | static void add_traceable_app(struct ltt_traceable_app *lta); |
| 42 | static void del_traceable_app(struct ltt_traceable_app *lta); |
| 43 | |
| 44 | /* |
| 45 | * add_traceable_app |
| 46 | * |
| 47 | * Add a traceable application structure to the global |
| 48 | * list protected by a mutex. |
| 49 | */ |
| 50 | static void add_traceable_app(struct ltt_traceable_app *lta) |
| 51 | { |
| 52 | pthread_mutex_lock(<t_traceable_app_list_mutex); |
| 53 | cds_list_add(<a->list, <t_traceable_app_list.head); |
| 54 | traceable_app_count++; |
| 55 | pthread_mutex_unlock(<t_traceable_app_list_mutex); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * del_traceable_app |
| 60 | * |
| 61 | * Delete a traceable application structure from the |
| 62 | * global list protected by a mutex. |
| 63 | */ |
| 64 | static void del_traceable_app(struct ltt_traceable_app *lta) |
| 65 | { |
| 66 | pthread_mutex_lock(<t_traceable_app_list_mutex); |
| 67 | cds_list_del(<a->list); |
| 68 | /* Sanity check */ |
| 69 | if (traceable_app_count != 0) { |
| 70 | traceable_app_count--; |
| 71 | } |
| 72 | pthread_mutex_unlock(<t_traceable_app_list_mutex); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * register_traceable_app |
| 77 | * |
| 78 | * Using pid and uid (of the app), allocate |
| 79 | * a new ltt_traceable_app struct and add it |
| 80 | * to the global traceable app list. |
| 81 | * |
| 82 | * On success, return 0, else return malloc ENOMEM. |
| 83 | */ |
| 84 | int register_traceable_app(pid_t pid, uid_t uid) |
| 85 | { |
| 86 | struct ltt_traceable_app *lta; |
| 87 | |
| 88 | lta = malloc(sizeof(struct ltt_traceable_app)); |
| 89 | if (lta == NULL) { |
| 90 | perror("malloc"); |
| 91 | return -ENOMEM; |
| 92 | } |
| 93 | |
| 94 | lta->uid = uid; |
| 95 | lta->pid = pid; |
| 96 | add_traceable_app(lta); |
| 97 | DBG("Application %d registered with UID %d", pid, uid); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * unregister_traceable_app |
| 104 | * |
| 105 | * Unregister app by removing it from the global |
| 106 | * traceable app list and freeing the data struct. |
| 107 | */ |
| 108 | void unregister_traceable_app(pid_t pid) |
| 109 | { |
| 110 | struct ltt_traceable_app *lta; |
| 111 | |
| 112 | lta = find_app_by_pid(pid); |
| 113 | if (lta != NULL) { |
| 114 | del_traceable_app(lta); |
| 115 | free(lta); |
| 116 | DBG("PID %d unregistered", pid); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * get_app_count |
| 122 | * |
| 123 | * Return traceable_app_count |
| 124 | */ |
| 125 | unsigned int get_app_count(void) |
| 126 | { |
| 127 | return traceable_app_count; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * find_app_by_pid |
| 132 | * |
| 133 | * Iterate over the traceable apps list and |
| 134 | * return a pointer or NULL if not found. |
| 135 | */ |
| 136 | struct ltt_traceable_app *find_app_by_pid(pid_t pid) |
| 137 | { |
| 138 | struct ltt_traceable_app *iter; |
| 139 | |
| 140 | pthread_mutex_lock(<t_traceable_app_list_mutex); |
| 141 | cds_list_for_each_entry(iter, <t_traceable_app_list.head, list) { |
| 142 | if (iter->pid == pid) { |
| 143 | pthread_mutex_unlock(<t_traceable_app_list_mutex); |
| 144 | /* Found */ |
| 145 | return iter; |
| 146 | } |
| 147 | } |
| 148 | pthread_mutex_unlock(<t_traceable_app_list_mutex); |
| 149 | |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * get_app_list_pids |
| 155 | * |
| 156 | * List traceable user-space application and fill an |
| 157 | * array of pids. |
| 158 | */ |
| 159 | void get_app_list_pids(pid_t *pids) |
| 160 | { |
| 161 | int i = 0; |
| 162 | struct ltt_traceable_app *iter; |
| 163 | |
| 164 | /* Protected by a mutex here because the threads manage_client |
| 165 | * and manage_apps can access this list. |
| 166 | */ |
| 167 | pthread_mutex_lock(<t_traceable_app_list_mutex); |
| 168 | cds_list_for_each_entry(iter, <t_traceable_app_list.head, list) { |
| 169 | pids[i] = iter->pid; |
| 170 | i++; |
| 171 | } |
| 172 | pthread_mutex_unlock(<t_traceable_app_list_mutex); |
| 173 | } |