Fix: resource leak in enable-event command
[lttng-tools.git] / src / bin / lttng-sessiond / fd-limit.c
1 /*
2 * Copyright (C) 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <urcu/uatomic.h>
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include "fd-limit.h"
25
26 /* total count of fd. */
27 static long fd_count;
28
29 /*
30 * threshold in % of number of fd allowed.
31 */
32 static long fd_threshold[LTTNG_FD_NR_TYPES] = {
33 [LTTNG_FD_APPS] = 75,
34 };
35
36 static rlim_t max_nr_fd;
37
38 int lttng_fd_get(enum lttng_fd_type type, unsigned int nr)
39 {
40 long newval;
41
42 if (type >= LTTNG_FD_NR_TYPES) {
43 return -EINVAL;
44 }
45
46 newval = uatomic_add_return(&fd_count, (long) nr);
47 if ((long) (newval * 100)
48 - (long) (max_nr_fd * fd_threshold[type]) > 0) {
49 uatomic_sub(&fd_count, (long) nr);
50 return -EPERM;
51 }
52 return 0;
53 }
54
55 void lttng_fd_put(enum lttng_fd_type type, unsigned int nr)
56 {
57 uatomic_sub(&fd_count, (long) nr);
58 }
59
60 void lttng_fd_init(void)
61 {
62 struct rlimit rlim;
63 int ret;
64
65 ret = getrlimit(RLIMIT_NOFILE, &rlim);
66 if (ret < 0) {
67 perror("getrlimit");
68 }
69 max_nr_fd = rlim.rlim_cur;
70 }
This page took 0.030778 seconds and 4 git commands to generate.