summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremie Koenig <jk@jk.fr.eu.org>2010-08-17 12:49:33 +0000
committerJeremie Koenig <jk@jk.fr.eu.org>2010-08-30 14:14:48 +0200
commit3907bbbcfb806799a5349e46a2b804307a0e9836 (patch)
tree94aac58b745f98a4362f4ba56af83205b08391d2
parent56ba0ac1e3a3f32763060ab862a6f0054c8a50bf (diff)
Add the list of processes as a directory
* proclist.h, proclist.c: New files. * main.c: Add a proclist directory based on them. * Makefile: Include the proclist module.
-rw-r--r--Makefile2
-rw-r--r--main.c8
-rw-r--r--proclist.c81
-rw-r--r--proclist.h2
4 files changed, 92 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index ab6e406..1fe7415 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
TARGET = procfs
-OBJS = procfs.o netfs.o procfs_file.o procfs_dir.o main.o
+OBJS = procfs.o netfs.o procfs_file.o procfs_dir.o proclist.o main.o
LIBS = -lnetfs
CC = gcc
diff --git a/main.c b/main.c
index 62e440d..bc5c865 100644
--- a/main.c
+++ b/main.c
@@ -1,21 +1,29 @@
#include <mach.h>
+#include <hurd.h>
#include <error.h>
#include <argp.h>
#include <hurd/netfs.h>
#include "procfs.h"
#include "procfs_file.h"
#include "procfs_dir.h"
+#include "proclist.h"
static struct node *make_file (void *dir_hook, void *ent_hook)
{
return procfs_file_make_node (ent_hook, -1, NULL);
}
+static struct node *make_proclist (void *dir_hook, void *ent_hook)
+{
+ return proclist_make_node (getproc ());
+}
+
int main (int argc, char **argv)
{
static const struct procfs_dir_entry entries[] = {
{ "hello", make_file, "Hello, World!\n" },
{ "goodbye", make_file, "Goodbye, cruel World!\n" },
+ { "proclist", make_proclist, },
{ }
};
mach_port_t bootstrap;
diff --git a/proclist.c b/proclist.c
new file mode 100644
index 0000000..4dd6ab3
--- /dev/null
+++ b/proclist.c
@@ -0,0 +1,81 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mach.h>
+#include <hurd/process.h>
+#include "procfs.h"
+#include "procfs_file.h"
+#include "procfs_dir.h"
+
+#define PID_STR_SIZE (3 * sizeof (pid_t) + 1)
+
+struct proclist_node
+{
+ process_t process;
+};
+
+static error_t
+proclist_get_contents (void *hook, void **contents, size_t *contents_len)
+{
+ struct proclist_node *pl = hook;
+ pidarray_t pids;
+ mach_msg_type_number_t num_pids;
+ error_t err;
+ int i;
+
+ num_pids = 0;
+ err = proc_getallpids (pl->process, &pids, &num_pids);
+ if (err)
+ return EIO;
+
+ *contents = malloc (num_pids * PID_STR_SIZE);
+ if (*contents)
+ {
+ *contents_len = 0;
+ for (i=0; i < num_pids; i++)
+ {
+ int n = sprintf (*contents + *contents_len, "%d", pids[i]);
+ assert (n >= 0);
+ *contents_len += (n + 1);
+ }
+ }
+ else
+ err = ENOMEM;
+
+ vm_deallocate (mach_task_self (), (vm_address_t) pids, num_pids * sizeof pids[0]);
+ return err;
+}
+
+static error_t
+proclist_lookup (void *hook, const char *name, struct node **np)
+{
+ *np = procfs_file_make_node ("Ceci n'est pas un processus\n", -1, NULL);
+ return *np ? 0 : ENOMEM;
+}
+
+struct node *
+proclist_make_node (process_t process)
+{
+ static const struct procfs_node_ops ops = {
+ .get_contents = proclist_get_contents,
+ .lookup = proclist_lookup,
+ .cleanup_contents = free,
+ .cleanup = free,
+ };
+ struct proclist_node *pl;
+ struct node *np;
+
+ pl = malloc (sizeof *pl);
+ if (! pl)
+ return NULL;
+
+ memset (pl, 0, sizeof *pl);
+ pl->process = process;
+
+ np = procfs_make_node (&ops, pl);
+ if (! np)
+ free (pl);
+
+ return np;
+}
+
diff --git a/proclist.h b/proclist.h
new file mode 100644
index 0000000..a766d50
--- /dev/null
+++ b/proclist.h
@@ -0,0 +1,2 @@
+#include <hurd/hurd_types.h>
+struct node *proclist_make_node (process_t process);