f522c518048b6a2ad50c15821db8deb3bfdffb8f
[platform/upstream/connman.git] / src / shared / debugfs.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdbool.h>
30 #include <string.h>
31 #include <limits.h>
32
33 #include "src/shared/debugfs.h"
34
35 #define STRINGIFY(x) STRINGIFY_ARG(x)
36 #define STRINGIFY_ARG(x) #x
37
38 const char *debugfs_get_path(void)
39 {
40         static char path[PATH_MAX + 1];
41         static bool found = false;
42         char type[100];
43         FILE *fp;
44
45         if (found)
46                 return path;
47
48         fp = fopen("/proc/mounts", "r");
49         if (!fp)
50                 return NULL;
51
52         while (fscanf(fp, "%*s %" STRINGIFY(PATH_MAX) "s %99s %*s %*d %*d\n",
53                                                         path, type) == 2) {
54                 if (!strcmp(type, "debugfs")) {
55                         found = true;
56                         break;
57                 }
58         }
59
60         fclose(fp);
61
62         if (!found)
63                 return NULL;
64
65         return path;
66 }