summaryrefslogtreecommitdiff
path: root/procfs.c
blob: f7b28347ed67d0174888a895134e1f25f07b5cbf (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <mach.h>
#include <hurd/netfs.h>
#include <hurd/fshelp.h>
#include "procfs.h"

struct netnode
{
  const struct procfs_node_ops *ops;
  void *hook;

  /* (cached) contents of the node */
  char *contents;
  ssize_t contents_len;

  /* parent directory, if applicable */
  struct node *parent;
};

void
procfs_cleanup_contents_with_free (void *hook, char *cont, ssize_t len)
{
  free (cont);
}

void
procfs_cleanup_contents_with_vm_deallocate (void *hook, char *cont, ssize_t len)
{
  vm_deallocate (mach_task_self (), (vm_address_t) cont, (vm_size_t) len);
}

struct node *procfs_make_node (const struct procfs_node_ops *ops, void *hook)
{
  struct netnode *nn;
  struct node *np;
 
  nn = malloc (sizeof *nn);
  if (! nn)
    goto fail;

  memset (nn, 0, sizeof *nn);
  nn->ops = ops;
  nn->hook = hook;

  np = netfs_make_node (nn);
  if (! np)
    goto fail;

  np->nn = nn;
  memset (&np->nn_stat, 0, sizeof np->nn_stat);
  np->nn_translated = 0;

  if (np->nn->ops->lookup)
    np->nn_stat.st_mode = S_IFDIR | 0555;
  else
    np->nn_stat.st_mode = S_IFREG | 0444;

  return np;

fail:
  if (ops->cleanup)
    ops->cleanup (hook);

  free (nn);
  return NULL;
}

void procfs_node_chown (struct node *np, uid_t owner)
{
  np->nn_stat.st_uid = owner;
}

void procfs_node_chmod (struct node *np, mode_t mode)
{
  np->nn_stat.st_mode = (np->nn_stat.st_mode & S_IFMT) | mode;
  np->nn_translated = np->nn_stat.st_mode;
}

void procfs_node_chtype (struct node *np, mode_t type)
{
  np->nn_stat.st_mode = (np->nn_stat.st_mode & ~S_IFMT) | type;
  np->nn_translated = np->nn_stat.st_mode;
  if (type == S_IFLNK)
    procfs_node_chmod (np, 0777);
}

/* FIXME: possibly not the fastest hash function... */
ino64_t
procfs_make_ino (struct node *np, const char *filename)
{
  unsigned short x[3];

  if (! strcmp (filename, "."))
    return np->nn_stat.st_ino;
  if (! strcmp (filename, ".."))
    return np->nn->parent ? np->nn->parent->nn_stat.st_ino : /* FIXME: */ 42;

  assert (sizeof np->nn_stat.st_ino > sizeof x);
  memcpy (x, &np->nn_stat.st_ino, sizeof x);

  while (*filename)
    {
      x[0] ^= *(filename++);
      jrand48 (x);
    }

  return (unsigned long) jrand48 (x);
}

error_t procfs_get_contents (struct node *np, char **data, ssize_t *data_len)
{
  if (! np->nn->contents && np->nn->ops->get_contents)
    {
      char *contents;
      ssize_t contents_len;
      error_t err;

      contents_len = -1;
      err = np->nn->ops->get_contents (np->nn->hook, &contents, &contents_len);
      if (err)
	return err;
      if (contents_len < 0)
	return ENOMEM;

      np->nn->contents = contents;
      np->nn->contents_len = contents_len;
    }

  *data = np->nn->contents;
  *data_len = np->nn->contents_len;
  return 0;
}

void procfs_refresh (struct node *np)
{
  if (np->nn->contents && np->nn->ops->cleanup_contents)
    np->nn->ops->cleanup_contents (np->nn->hook, np->nn->contents, np->nn->contents_len);

  np->nn->contents = NULL;
}

error_t procfs_lookup (struct node *np, const char *name, struct node **npp)
{
  error_t err = ENOENT;

  if (err && ! strcmp (name, "."))
    {
      netfs_nref(*npp = np);
      err = 0;
    }

  if (err && np->nn->parent && ! strcmp (name, ".."))
    {
      netfs_nref(*npp = np->nn->parent);
      err = 0;
    }

  if (err && np->nn->ops->lookup)
    {
      err = np->nn->ops->lookup (np->nn->hook, name, npp);
      if (! err)
        {
	  (*npp)->nn_stat.st_ino = procfs_make_ino (np, name);
	  netfs_nref ((*npp)->nn->parent = np);
	}
    }

  return err;
}

void procfs_cleanup (struct node *np)
{
  procfs_refresh (np);

  if (np->nn->ops->cleanup)
    np->nn->ops->cleanup (np->nn->hook);

  if (np->nn->parent)
    netfs_nrele (np->nn->parent);

  free (np->nn);
}