diff options
author | Martin Baulig <martin@home-of-linux.org> | 1998-08-17 21:11:46 +0000 |
---|---|---|
committer | Martin Baulig <martin@src.gnome.org> | 1998-08-17 21:11:46 +0000 |
commit | 7ad67781ef204c52114e832971011b4e6e6a1276 (patch) | |
tree | adb2a21830c16828f22914f36552a8f44d0552e5 | |
parent | 616f4f4eaf0d19e070f8a4d85396e0faceb35858 (diff) | |
download | libgtop-7ad67781ef204c52114e832971011b4e6e6a1276.tar.gz |
Added `offset' and `perm' fields.
1998-08-17 Martin Baulig <martin@home-of-linux.org>
* include/glibtop/procmap (glibtop_map_entry): Added `offset' and
`perm' fields.
* src/inodedb: New directory.
* src/inodedb/README: New file - read it to know what this is about.
* configure.in (--with-libgtop-inodedb): New `configure' parameter.
* include/glibtop/inodedb.h: New file.
* sysdeps/common/inodedb.c: New file.
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | acconfig.h | 1 | ||||
-rw-r--r-- | configure.in | 20 | ||||
-rw-r--r-- | include/glibtop/Makefile.am | 3 | ||||
-rw-r--r-- | include/glibtop/inodedb.h | 58 | ||||
-rw-r--r-- | include/glibtop/procmap.h | 2 | ||||
-rw-r--r-- | lib/inodedb.c | 114 | ||||
-rw-r--r-- | src/Makefile.am | 10 | ||||
-rw-r--r-- | src/inodedb/.cvsignore | 4 | ||||
-rw-r--r-- | src/inodedb/Makefile.am | 13 | ||||
-rw-r--r-- | src/inodedb/README | 73 | ||||
-rw-r--r-- | src/inodedb/file_by_inode.c | 61 | ||||
-rw-r--r-- | src/inodedb/mkinodedb.c | 131 | ||||
-rw-r--r-- | sysdeps/common/Makefile.am | 15 | ||||
-rw-r--r-- | sysdeps/common/inodedb.c | 114 |
15 files changed, 625 insertions, 6 deletions
@@ -1,5 +1,17 @@ 1998-08-17 Martin Baulig <martin@home-of-linux.org> + * include/glibtop/procmap (glibtop_map_entry): Added `offset' and + `perm' fields. + + * src/inodedb: New directory. + * src/inodedb/README: New file - read it to know what this is about. + + * configure.in (--with-libgtop-inodedb): New `configure' parameter. + * include/glibtop/inodedb.h: New file. + * sysdeps/common/inodedb.c: New file. + +1998-08-17 Martin Baulig <martin@home-of-linux.org> + * LIBGTOP-VERSION: Released libgtop-0.25pre1. With this release I made what Linus calles a ``feature freeze'' until 0.25 is out. A Tarball and RPMS can be found at my site: @@ -117,6 +117,7 @@ #undef GLIBTOP_GUILE #undef GLIBTOP_GUILE_NAMES #undef GLIBTOP_EXAMPLES +#undef GLIBTOP_INODEDB #undef NEED_LIBGTOP diff --git a/configure.in b/configure.in index e3687119..9183c7b0 100644 --- a/configure.in +++ b/configure.in @@ -64,6 +64,20 @@ AC_ARG_WITH(libgtop-guile, test x$build_guile = xno && ac_cv_guile_found=no +AC_ARG_WITH(libgtop-inodedb, +[ --with-libgtop-inodedb Build the inodedb stuff (default=no)], +[build_inodedb="$withval"], [build_inodedb=no]) + +if test x$build_inodedb = xyes ; then + AC_CHECK_LIB(gdbm, gdbm_open, , build_inodedb=no) +fi + +AM_CONDITIONAL(INODEDB, test x$build_inodedb = xyes) + +if test x$build_inodedb = xyes ; then + AC_DEFINE(GLIBTOP_INODEDB) +fi + AC_DEFINE(GLIBTOP_NAMES) if test x$ac_cv_guile_found = xyes ; then @@ -182,6 +196,11 @@ AC_SUBST(SUPPORTINCS) dnl These definitions are expanded in make. LIBGTOP_LIBS='-L$(libdir) -lgtop' LIBGTOP_INCS='-I$(includedir)' + +if test x$build_inodedb = xyes ; then + LIBGTOP_INCS="$LIBGTOP_INCS -DGLIBTOP_INODEDB" +fi + LIBGTOP_GUILE_LIBS="$LIBGTOP_LIBS" LIBGTOP_GUILE_INCS="$LIBGTOP_INCS" LIBGTOP_BINDIR='$(bindir)' @@ -285,6 +304,7 @@ sysdeps/kernel/Makefile sysdeps/freebsd/Makefile src/Makefile src/daemon/Makefile +src/inodedb/Makefile lib/Makefile guile/Makefile examples/Makefile diff --git a/include/glibtop/Makefile.am b/include/glibtop/Makefile.am index 482444e9..dcd3540f 100644 --- a/include/glibtop/Makefile.am +++ b/include/glibtop/Makefile.am @@ -6,4 +6,5 @@ glibtop_HEADERS = close.h loadavg.h prockernel.h procstate.h \ procmem.h procuid.h swap.h write.h error.h open.h \ procsegment.h read.h sysdeps.h xmalloc.h global.h \ procsignal.h read_data.h union.h types.h gnuserv.h \ - parameter.h mountlist.h fsusage.h procmap.h signal.h + parameter.h mountlist.h fsusage.h procmap.h signal.h \ + inodedb.h diff --git a/include/glibtop/inodedb.h b/include/glibtop/inodedb.h new file mode 100644 index 00000000..fbc8d7b0 --- /dev/null +++ b/include/glibtop/inodedb.h @@ -0,0 +1,58 @@ +/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + This file is part of the Gnome Top Library. + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + The Gnome Top Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Top Library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#ifndef __GLIBTOP_INODEDB_H__ +#define __GLIBTOP_INODEDB_H__ + +#include <glibtop.h> +#include <gdbm.h> + +#define GLIBTOP_INODEDB_SYSTEM 1 +#define GLIBTOP_INODEDB_USER 2 +#define GLIBTOP_INODEDB_CACHE 4 + +#define GLIBTOP_INODEDB_ALL 7 + +__BEGIN_DECLS + +typedef struct _glibtop_inodedb glibtop_inodedb; + +typedef struct _glibtop_inodedb_key glibtop_inodedb_key; + +struct _glibtop_inodedb_key +{ + u_int64_t device, inode; +}; + +struct _glibtop_inodedb +{ + GDBM_FILE system_dbf, user_dbf; +}; + +#define glibtop_inodedb_open(p1,p2) glibtop_inodedb_open_s(glibtop_global_server, p1, p2) +#define glibtop_inodedb_lookup(p1,p2,p3) glibtop_inodedb_lookup_s(glibtop_global_server, p1, p2, p3) +#define glibtop_inodedb_close(p1) glibtop_inodedb_close_s(glibtop_global_server) + +extern glibtop_inodedb *glibtop_inodedb_open_s __P((glibtop *, unsigned, unsigned long)); +extern const char *glibtop_inodedb_lookup_s __P((glibtop *, glibtop_inodedb *, u_int64_t, u_int64_t)); +extern void glibtop_inodedb_close_s __P((glibtop *, glibtop_inodedb *)); + +__END_DECLS + +#endif diff --git a/include/glibtop/procmap.h b/include/glibtop/procmap.h index 215f0045..75e19f67 100644 --- a/include/glibtop/procmap.h +++ b/include/glibtop/procmap.h @@ -39,7 +39,7 @@ typedef struct _glibtop_proc_map glibtop_proc_map; struct _glibtop_map_entry { - u_int64_t start, end, inode, device; + u_int64_t start, end, offset, perm, inode, device; }; struct _glibtop_proc_map diff --git a/lib/inodedb.c b/lib/inodedb.c new file mode 100644 index 00000000..77d9a9c7 --- /dev/null +++ b/lib/inodedb.c @@ -0,0 +1,114 @@ +/* $Id$ */ + +/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + This file is part of the Gnome Top Library. + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + The Gnome Top Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Top Library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/xmalloc.h> +#include <glibtop/inodedb.h> + +#include <pwd.h> +#include <gdbm.h> + +#ifndef SYSTEM_INODEDB +#define SYSTEM_INODEDB "/home/baulig/INSTALL/var/libgtop/inode.db" +#endif + +glibtop_inodedb * +glibtop_inodedb_open_s (glibtop *server, unsigned databases, + unsigned long cachesize) +{ + glibtop_inodedb *inodedb; + char filename [BUFSIZ]; + struct passwd *pwd; + struct stat statb; + + if (!databases) + databases = GLIBTOP_INODEDB_ALL; + + inodedb = glibtop_calloc_r (server, 1, sizeof (glibtop_inodedb)); + + if (stat (SYSTEM_INODEDB, &statb)) + databases &= ~GLIBTOP_INODEDB_SYSTEM; + + if (databases & GLIBTOP_INODEDB_SYSTEM) { + inodedb->system_dbf = gdbm_open + (SYSTEM_INODEDB, 0, GDBM_READER, 0, 0); + if (!inodedb->system_dbf) + glibtop_error_io_r + (server, "gdbm_open (%s)", SYSTEM_INODEDB); + } + + pwd = getpwuid (getuid ()); + if (!pwd) glibtop_error_io_r (server, "getpwuid"); + + sprintf (filename, "%s/var/libgtop/inode.db", pwd->pw_dir); + + if (stat (filename, &statb)) + databases &= ~GLIBTOP_INODEDB_USER; + + if (databases & GLIBTOP_INODEDB_USER) { + inodedb->user_dbf = gdbm_open + (filename, 0, GDBM_READER, 0, 0); + if (!inodedb->user_dbf) + glibtop_error_io_r + (server, "gdbm_open (%s)", filename); + } + + return inodedb; +} + +const char * +glibtop_inodedb_lookup_s (glibtop *server, glibtop_inodedb *inodedb, + u_int64_t device, u_int64_t inode) +{ + glibtop_inodedb_key key; + datum d_key, d_content; + + d_key.dptr = (void *) &key; + d_key.dsize = sizeof (key); + + key.device = device; + key.inode = inode; + + if (inodedb->system_dbf) { + d_content = gdbm_fetch (inodedb->system_dbf, d_key); + if (d_content.dptr) return d_content.dptr; + } + + if (inodedb->user_dbf) { + d_content = gdbm_fetch (inodedb->user_dbf, d_key); + if (d_content.dptr) return d_content.dptr; + } + + return NULL; +} + +void +glibtop_inodedb_close_s (glibtop *server, glibtop_inodedb *inodedb) +{ + if (inodedb->system_dbf) + gdbm_close (inodedb->system_dbf); + + if (inodedb->user_dbf) + gdbm_close (inodedb->user_dbf); + + glibtop_free_r (server, inodedb); +} diff --git a/src/Makefile.am b/src/Makefile.am index 247882b8..46edd9c2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,10 @@ -SUBDIRS = daemon +if INODEDB +inodedb_SUBDIRS = inodedb +else +inodedb_SUBDIRS = +endif -DIST_SUBDIRS = daemon +SUBDIRS = daemon $(inodedb_SUBDIRS) + +DIST_SUBDIRS = daemon inodedb diff --git a/src/inodedb/.cvsignore b/src/inodedb/.cvsignore new file mode 100644 index 00000000..72360a9b --- /dev/null +++ b/src/inodedb/.cvsignore @@ -0,0 +1,4 @@ +Makefile.in +Makefile +mkinodedb +file_by_inode diff --git a/src/inodedb/Makefile.am b/src/inodedb/Makefile.am new file mode 100644 index 00000000..e09927c5 --- /dev/null +++ b/src/inodedb/Makefile.am @@ -0,0 +1,13 @@ +bin_PROGRAMS = mkinodedb file_by_inode + +mkinodedb_LDADD = $(top_builddir)/lib/libgtop.la \ + $(top_builddir)/sysdeps/common/libgtop_common.la \ + $(top_builddir)/sysdeps/@sysdeps_dir@/libgtop_sysdeps.la \ + @INTLLIBS@ @LIBSUPPORT@ @libs_xauth@ -lgdbm +mkinodedb_LDFLAGS = -static + +file_by_inode_LDADD = $(top_builddir)/lib/libgtop.la \ + $(top_builddir)/sysdeps/common/libgtop_common.la \ + $(top_builddir)/sysdeps/@sysdeps_dir@/libgtop_sysdeps.la \ + @INTLLIBS@ @LIBSUPPORT@ @libs_xauth@ -lgdbm +file_by_inode_LDFLAGS = -static diff --git a/src/inodedb/README b/src/inodedb/README new file mode 100644 index 00000000..82d9ecc5 --- /dev/null +++ b/src/inodedb/README @@ -0,0 +1,73 @@ +Not all UNIXes provide an easy way to get the filename if you have +the device it is mounted on and its inode. + +Well, under Linux we can simply read /proc/<pid>/maps to get the filenames +but I don't see any way to get this under FreeBSD. + +In this case you can give configure the optional `--with-libgtop-inodedb' +parameter to build this stuff here. + +It uses the GNU database library `gdbm' to find the inode in a database +which is created when libgtop is installed. This should be reasonable +fast since the entire filesystem only needs to be traversed once when +libgtop is installed (and each time you install new software, of cause). + +We have two databases: + +* First the system administrator may place a system-wide database + in `$(prefix)/var/libgtop/inodedb.db' when installing libgtop. + +* Every user can have his/her own one in `~/var/libgtop/inodedb.db'. + This one has precedence over the system wide one. + +The `mkinodedb' program which is build in this directory takes two +command line arguments: the full pathname of the database to be created +and the name of a configuration file consisting of directory and file names +each on a line by itself - see `/etc/ld.so.conf' for an example. + +Putting a directory name in this file means all regular files found in this +directory are included in the database, but it will not recursively descend +into subdirectories (for instance, we want everythink in `/usr/lib' but not +every single file in `/usr/lib/sgml'). You can also use filenames to include +a single file. + +To use this interface in your program, you first have to call +`glibtop_inodedb_open ()': + +glibtop_inodedb * +glibtop_inodedb_open (unsigned databases, unsigned long cachesize) + +glibtop_inodedb * +glibtop_inodedb_open_s (glibtop *server, unsigned databases, + unsigned long cachesize) + +There are some constants defined in <glibtop/inodedb.h> for the `databases' +parameter - you can use zero as default: + +#define GLIBTOP_INODEDB_SYSTEM 1 +#define GLIBTOP_INODEDB_USER 2 +#define GLIBTOP_INODEDB_CACHE 4 + +The `cachesize' gives the size of a not yet implemented in-memory cache for +looked up entries. + +This function will return a pointer of type `glibtop_inodedb *' which you need +to lookup an entry in the database. + +To look up an entry, use the `glibtop_inodedb_lookup ()' function: + +const char * +glibtop_inodedb_lookup (glibtop_inodedb *inodedb, + u_int64_t device, u_int64_t inode) +const char * +glibtop_inodedb_lookup_s (glibtop *server, glibtop_inodedb *inodedb, + u_int64_t device, u_int64_t inode) + +It will either return a pointer to the filename - which you have to +`glibtop_free ()' once done with it - or NULL on error. + +You can use the `file_by_inode' program in this directory to test the +interface - just call it with two command line arguments: the device number +and the inode number. + +Martin <martin@home-of-linux.org> diff --git a/src/inodedb/file_by_inode.c b/src/inodedb/file_by_inode.c new file mode 100644 index 00000000..5dd4cbab --- /dev/null +++ b/src/inodedb/file_by_inode.c @@ -0,0 +1,61 @@ +/* $Id$ */ + +/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + This file is part of the Gnome Top Library. + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + The Gnome Top Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Top Library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/inodedb.h> + +#include <pwd.h> +#include <dirent.h> + +#include <sys/stat.h> + +int +main (int argc, const char *argv []) +{ + glibtop_inodedb *inodedb; + const char *filename; + unsigned device, inode; + + if (argc != 3) { + fprintf (stderr, "Usage: %s device inode\n", argv [0]); + exit (1); + } + + if (sscanf (argv [1], "%d", &device) != 1) { + fprintf (stderr, "Usage: %s device inode\n", argv [0]); + exit (1); + } + + if (sscanf (argv [2], "%d", &inode) != 1) { + fprintf (stderr, "Usage: %s device inode\n", argv [0]); + exit (1); + } + + inodedb = glibtop_inodedb_open (0, 0); + if (!inodedb) exit (1); + + filename = glibtop_inodedb_lookup (inodedb, device, inode); + if (!filename) exit (0); + + fprintf (stderr, "FILENAME: %lu - %lu - '%s'\n", + device, inode, filename); +} diff --git a/src/inodedb/mkinodedb.c b/src/inodedb/mkinodedb.c new file mode 100644 index 00000000..51479d86 --- /dev/null +++ b/src/inodedb/mkinodedb.c @@ -0,0 +1,131 @@ +/* $Id$ */ + +/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + This file is part of the Gnome Top Library. + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + The Gnome Top Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Top Library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/inodedb.h> + +#include <pwd.h> +#include <dirent.h> + +#include <sys/stat.h> + +int +main (int argc, const char *argv []) +{ + GDBM_FILE dbf; + char dirname [BUFSIZ]; + FILE *f; + + if (argc != 3) { + fprintf (stderr, "Usage: %s database filename\n", argv [0]); + exit (1); + } + + f = fopen (argv [2], "rt"); + if (!f) + glibtop_error_io ("fopen (%s)", argv [2]); + + dbf = gdbm_open (argv [1], 512, GDBM_WRCREAT, 0600, 0); + if (!dbf) + glibtop_error_io ("gdbm_open (%s)", argv [1]); + + while (fgets (dirname, BUFSIZ-1, f)) { + struct dirent *entry; + struct stat statb; + DIR *directory; + size_t len; + + len = strlen (dirname); + if (!len) continue; + + if (dirname [len-1] == '\n') + dirname [len-1] = 0; + + if (stat (dirname, &statb)) + continue; + + if (S_ISREG (statb.st_mode)) { + glibtop_inodedb_key key; + datum d_key, d_content; + + d_key.dptr = (void *) &key; + d_key.dsize = sizeof (key); + + d_content.dptr = dirname; + d_content.dsize = strlen (dirname) + 1; + + key.device = (u_int64_t) statb.st_dev; + key.inode = (u_int64_t) statb.st_ino; + + if (gdbm_store (dbf, d_key, d_content, GDBM_REPLACE)) + glibtop_error_io ("gdbm_store (%s)", dirname); + + printf ("%-52s - %8lu - %8lu\n", + dirname, (unsigned long) statb.st_dev, + (unsigned long) statb.st_ino); + + continue; + } + + if (!S_ISDIR (statb.st_mode)) + continue; + + directory = opendir (dirname); + if (!directory) continue; + + while (entry = readdir (directory)) { + glibtop_inodedb_key key; + char filename [BUFSIZ]; + datum d_key, d_content; + + sprintf (filename, "%s/%s", dirname, entry->d_name); + + if (stat (filename, &statb)) + continue; + + if (!S_ISREG (statb.st_mode)) + continue; + + d_key.dptr = (void *) &key; + d_key.dsize = sizeof (key); + + d_content.dptr = filename; + d_content.dsize = strlen (filename) + 1; + + key.device = (u_int64_t) statb.st_dev; + key.inode = (u_int64_t) statb.st_ino; + + if (gdbm_store (dbf, d_key, d_content, GDBM_REPLACE)) + glibtop_error_io ("gdbm_store (%s)", filename); + + printf ("%-52s - %8lu - %8lu\n", + filename, (unsigned long) statb.st_dev, + (unsigned long) statb.st_ino); + } + + closedir (directory); + } + + gdbm_close (dbf); + + fclose (f); +} diff --git a/sysdeps/common/Makefile.am b/sysdeps/common/Makefile.am index eb7be430..fcf635f2 100644 --- a/sysdeps/common/Makefile.am +++ b/sysdeps/common/Makefile.am @@ -1,10 +1,21 @@ LINK = $(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -o $@ -CFLAGS = -Wall -W @CFLAGS@ +if INODEDB +inodedb_SRCLIST = inodedb.c +inodedb_DEFS = -DSYSTEM_INODEDB=\"$(prefix)/var/libgtop/inodedb.db\" +else +inodedb_SRCLIST = +inodedb_DEFS = +endif + +CFLAGS = -Wall -W @CFLAGS@ $(inodedb_DEFS) lib_LTLIBRARIES = libgtop_common.la libgtop_suid_common.la libgtop_common_la_SOURCES = xmalloc.c error.c gnuslib.c \ - fsusage.c fsusage.h mountlist.c mountlist.h + fsusage.c fsusage.h mountlist.c mountlist.h \ + $(inodedb_SRCLIST) libgtop_suid_common_la_SOURCES = xmalloc.c error.c sysdeps_suid.c + +EXTRA_DIST = inodedb.c diff --git a/sysdeps/common/inodedb.c b/sysdeps/common/inodedb.c new file mode 100644 index 00000000..77d9a9c7 --- /dev/null +++ b/sysdeps/common/inodedb.c @@ -0,0 +1,114 @@ +/* $Id$ */ + +/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + This file is part of the Gnome Top Library. + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + The Gnome Top Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Top Library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/xmalloc.h> +#include <glibtop/inodedb.h> + +#include <pwd.h> +#include <gdbm.h> + +#ifndef SYSTEM_INODEDB +#define SYSTEM_INODEDB "/home/baulig/INSTALL/var/libgtop/inode.db" +#endif + +glibtop_inodedb * +glibtop_inodedb_open_s (glibtop *server, unsigned databases, + unsigned long cachesize) +{ + glibtop_inodedb *inodedb; + char filename [BUFSIZ]; + struct passwd *pwd; + struct stat statb; + + if (!databases) + databases = GLIBTOP_INODEDB_ALL; + + inodedb = glibtop_calloc_r (server, 1, sizeof (glibtop_inodedb)); + + if (stat (SYSTEM_INODEDB, &statb)) + databases &= ~GLIBTOP_INODEDB_SYSTEM; + + if (databases & GLIBTOP_INODEDB_SYSTEM) { + inodedb->system_dbf = gdbm_open + (SYSTEM_INODEDB, 0, GDBM_READER, 0, 0); + if (!inodedb->system_dbf) + glibtop_error_io_r + (server, "gdbm_open (%s)", SYSTEM_INODEDB); + } + + pwd = getpwuid (getuid ()); + if (!pwd) glibtop_error_io_r (server, "getpwuid"); + + sprintf (filename, "%s/var/libgtop/inode.db", pwd->pw_dir); + + if (stat (filename, &statb)) + databases &= ~GLIBTOP_INODEDB_USER; + + if (databases & GLIBTOP_INODEDB_USER) { + inodedb->user_dbf = gdbm_open + (filename, 0, GDBM_READER, 0, 0); + if (!inodedb->user_dbf) + glibtop_error_io_r + (server, "gdbm_open (%s)", filename); + } + + return inodedb; +} + +const char * +glibtop_inodedb_lookup_s (glibtop *server, glibtop_inodedb *inodedb, + u_int64_t device, u_int64_t inode) +{ + glibtop_inodedb_key key; + datum d_key, d_content; + + d_key.dptr = (void *) &key; + d_key.dsize = sizeof (key); + + key.device = device; + key.inode = inode; + + if (inodedb->system_dbf) { + d_content = gdbm_fetch (inodedb->system_dbf, d_key); + if (d_content.dptr) return d_content.dptr; + } + + if (inodedb->user_dbf) { + d_content = gdbm_fetch (inodedb->user_dbf, d_key); + if (d_content.dptr) return d_content.dptr; + } + + return NULL; +} + +void +glibtop_inodedb_close_s (glibtop *server, glibtop_inodedb *inodedb) +{ + if (inodedb->system_dbf) + gdbm_close (inodedb->system_dbf); + + if (inodedb->user_dbf) + gdbm_close (inodedb->user_dbf); + + glibtop_free_r (server, inodedb); +} |