File: mountlist.c

package info (click to toggle)
libgtop2 2.40.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 8,072 kB
  • sloc: ansic: 25,393; sh: 7,089; makefile: 418; perl: 268; xml: 179; sed: 16
file content (164 lines) | stat: -rw-r--r-- 3,679 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* mountlist.c -- return a list of mounted filesystems
   Copyright (C) 1991, 1992 Free Software Foundation, Inc.

   This file is a fork of sysdeps/common/mountlist.c.

   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, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */


/*
Local Variables:
mode: C
c-file-style: "gnu"
End:
*/

#include <config.h>

#include <stdio.h>
#include <mntent.h>

#include <glibtop.h>
#include <glibtop/mountlist.h>
#include <glibtop/error.h>


/* IgnoreList */


typedef struct
{
  GHashTable *table;
} IgnoreList;


static IgnoreList*
ignore_list_new(void)
{
  IgnoreList* ig;
  ig = g_new(IgnoreList, 1);
  ig->table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
  return ig;
}


static void
ignore_list_delete(IgnoreList* ig)
{
  if (ig) {
    g_hash_table_destroy(ig->table);
    g_free(ig);
  }
}


static void
ignore_list_add(IgnoreList* ig, const char* fs)
{
  g_hash_table_insert(ig->table, g_strdup(fs), GINT_TO_POINTER(1));
}


static gboolean
ignore_list_has(IgnoreList* ig, const char* fs)
{
  gpointer data;
  data = g_hash_table_lookup(ig->table, fs);
  return data != NULL;
}


/* ~IgnoreList */


static gboolean
ignore_fs(const char *fstype, IgnoreList** ig)
{
  if (!*ig) {
    FILE* fs;
    char line[128];

    *ig = ignore_list_new();

    ignore_list_add(*ig, "none");

    if ((fs = fopen("/proc/filesystems", "r")) != NULL) {
      while (fgets(line, sizeof line, fs)) {
	if (!strncmp(line, "nodev", 5)) {
	  char *type;
	  type = g_strstrip(line + 5);
	  ignore_list_add(*ig, type);
	}
      }
      fclose(fs);
    }
  }

  return ignore_list_has(*ig, fstype);
}


glibtop_mountentry *
glibtop_get_mountlist_s(glibtop *server, glibtop_mountlist *buf, int all_fs)
{
  const struct mntent *mnt;
  FILE *fp;
  GArray* entries;
  IgnoreList* ig = NULL;

  memset(buf, 0, sizeof(glibtop_mountlist));

  /* wild guess, preallocate 8 entries
     on a desktop, almost everyone has / and a tmpfs for udev
     if all_fs, there are also proc, sys, fuse, binfmt, etc */
  entries = g_array_sized_new(FALSE, FALSE, sizeof(glibtop_mountentry), 8);

  if (!(fp = setmntent(MOUNTED, "r"))) {
      glibtop_warn_io_r(server, "Could not open %s", MOUNTED);
      goto out;
    }

  while ((mnt = getmntent(fp)))
    {
      glibtop_mountentry *me;
      gsize len;

      if (!all_fs && ignore_fs(mnt->mnt_type, &ig))
	continue;

      len = entries->len;
      g_array_set_size(entries, len + 1);
      me = &g_array_index(entries, glibtop_mountentry, len);

      g_strlcpy(me->devname, mnt->mnt_fsname, sizeof me->devname);
      g_strlcpy(me->mountdir, mnt->mnt_dir, sizeof me->mountdir);
      g_strlcpy(me->type, mnt->mnt_type, sizeof me->type);
    }

    endmntent(fp);


  out:
    ignore_list_delete(ig);

    buf->size = sizeof(glibtop_mountentry);
    buf->number = entries->len;
    buf->total = buf->number * buf->size;
    buf->flags = (1 << GLIBTOP_MOUNTLIST_SIZE)
    | (1 << GLIBTOP_MOUNTLIST_NUMBER)
    | (1 << GLIBTOP_MOUNTLIST_TOTAL);

  return (glibtop_mountentry*) g_array_free(entries, FALSE);
}