summaryrefslogtreecommitdiff
path: root/procfs_dir.c
blob: bd1e49d6d5caed75d7285485d104a02d86dbd1d4 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/* procfs -- a translator for providing GNU/Linux compatible 
             proc pseudo-filesystem

   procfs_dir.c -- This file contains definitions to perform
                   directory operations such as creating,
                   removing and refreshing directories.
               
   Copyright (C) 2008, FSF.
   Written as a Summer of Code Project
   

   procfs 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.

   procfs 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA. 
   
   A portion of the code in this file is based on ftpfs code
   present in the hurd repositories copyrighted to FSF. The
   Copyright notice from that file is given below.
   
   Copyright (C) 1997,98,2002 Free Software Foundation, Inc.
   Written by Miles Bader <miles@gnu.org>
   This file is part of the GNU Hurd.
*/


#include <stdio.h>
#include <unistd.h>
#include <hurd/netfs.h>
#include <hurd/ihash.h>
#include <sys/stat.h>

#include "procfs.h"

/* Initial HASHTABLE length for the new directories to be created. */
#define INIT_HTABLE_LEN 5

struct procfs_dir_entry **cur_entry;

/* Return in DIR a new procfs directory, in the filesystem FS,
   with node NODE and path PATH. */
error_t procfs_dir_create (struct procfs *fs, struct node *node,
			  const char *path, struct procfs_dir **dir)
{
  struct procfs_dir *new = malloc (sizeof (struct procfs_dir));
  if (!new) 
    return ENOMEM;
  struct procfs_dir_entry **htable = calloc (INIT_HTABLE_LEN,
                            sizeof (struct procfs_dir_entry *));
  if (!htable)
    return ENOMEM;

  /* Hold a reference to the new dir's node.  */
  spin_lock (&netfs_node_refcnt_lock);
  node->references++;
  spin_unlock (&netfs_node_refcnt_lock);

  new->num_entries = 0;
  new->num_live_entries = 0;
  new->htable_len = INIT_HTABLE_LEN;
  new->htable = htable;
  new->ordered = NULL;
  new->fs_path = path;
  new->fs = fs;
  new->node = node;
  new->stat_timestamp = 0;
  new->name_timestamp = 0;

  *dir = new;

  if (fs->root != 0)
    node->nn->dir = new;

  return 0;
}

/* Put the directory entry DIR_ENTRY into the hash table HTABLE. */
static void
insert (struct procfs_dir_entry *dir_entry,
	struct procfs_dir_entry **htable, size_t htable_len)
{
  struct procfs_dir_entry **new_htable = &htable[dir_entry->hv % htable_len];
  if (*new_htable)
    (*new_htable)->self_p = &dir_entry->next;
  dir_entry->next = *new_htable;
  dir_entry->self_p = new_htable;
  *new_htable = dir_entry;
}

/* Calculate NAME's hash value.  */
static size_t
hash (const char *name)
{
  size_t hash_value = 0;
  while (*name)
    hash_value = ((hash_value << 5) + *name++) & 0xFFFFFF;
  return hash_value;
}

/* Extend the existing hashtable for DIR to accomodate values for new length
   NEW_LEN. We retain all the previous entries. */
static error_t
rehash (struct procfs_dir *dir, size_t new_len)
{
  int count;
  size_t old_len = dir->htable_len;
  struct procfs_dir_entry **old_htable = dir->htable;
  struct procfs_dir_entry **new_htable = (struct procfs_dir_entry **)
    malloc (new_len * sizeof (struct procfs_dir_entry *));

  if (! new_htable)
    return ENOMEM;

  bzero (new_htable, new_len * sizeof (struct procfs_dir_entry *));

  for (count = 0; count < old_len; count++)
    while (old_htable[count])
      {
	struct procfs_dir_entry *dir_entry = old_htable[count];

	/* Remove DIR_ENTRY from the old table */
	old_htable[count] = dir_entry->next;

	insert (dir_entry, new_htable, new_len);
      }

  free (old_htable);

  dir->htable = new_htable;
  dir->htable_len = new_len;

  return 0;
}

/* Lookup NAME in DIR and return its entry.  If there is no such entry, and
   DNEW, the decision variable, is true, then a new entry is allocated and
   returned, otherwise 0 is returned (if DNEW is true then 0 can be returned
   if a memory allocation error occurs).  */
struct procfs_dir_entry *
lookup_entry (struct procfs_dir *dir, const char *name, int dnew)
{
  size_t hv = hash (name);
  struct procfs_dir_entry *dir_entry = dir->htable[hv % dir->htable_len];

  while (dir_entry && strcmp (name, dir_entry->name) != 0)
    dir_entry = dir_entry->next;

  if (!dir_entry && dnew)
    {
      if (dir->num_entries > dir->htable_len)
	/* Grow the hash table.  */
	if (rehash (dir, (dir->htable_len + 1) * 2 - 1) != 0)
	  return 0;

      dir_entry = 
      (struct procfs_dir_entry *) malloc (sizeof (struct procfs_dir_entry));

      if (dir_entry)
	{
	  dir_entry->hv = hv;
	  dir_entry->name = strdup (name);
	  dir_entry->node = 0;
	  dir_entry->dir = dir;
	  dir_entry->stat_timestamp = 0;
	  bzero (&dir_entry->stat, sizeof dir_entry->stat);
	  dir_entry->symlink_target = 0;
	  dir_entry->noent = 0;
	  dir_entry->valid = 0;
	  dir_entry->name_timestamp = 0;
	  dir_entry->ordered_next = 0;
	  dir_entry->ordered_self_p = 0;
	  dir_entry->next = 0;
	  dir_entry->self_p = 0;
	  insert (dir_entry, dir->htable, dir->htable_len);
	  dir->num_entries++;
	}
    }

  return dir_entry;
}


/* Lookup NAME in DIR, returning its entry, or an error. 
   *NODE will contain the result node, locked, and with
   an additional reference, or 0 if an error occurs.  */
error_t procfs_dir_lookup (struct procfs_dir *dir, const char *name,
			  struct node **node)
{
  struct procfs_dir_entry *dir_entry = 0;
  error_t err = 0;
  char *fs_path = dir->fs_path;
  
  struct timeval tv;
  maptime_read (procfs_maptime, &tv); 
  
  time_t timestamp = tv.tv_sec;
 
  if (*name == '\0' || strcmp (name, ".") == 0)
    /* Current directory -- just add an additional reference to DIR's node
       and return it.  */
    {
      netfs_nref (dir->node);
      *node = dir->node;
      return 0;
    }
  else if (strcmp (name, "..") == 0)
    /* Parent directory.  */
    {
      if (dir->node->nn->dir_entry)
	{
	  *node = dir->node->nn->dir_entry->dir->node;
	  mutex_lock (&(*node)->lock);
	  netfs_nref (*node);
	}
      else
	{
	  err = ENOENT;		/* No .. */
	  *node = 0;
	}

      mutex_unlock (&dir->node->lock);

      return err;
    }

  err =  procfs_dir_refresh (dir, dir->node == dir->fs->root);
  if (!err && !dir_entry)
    dir_entry = lookup_entry (dir, name, 0);
   
  if (! err)
    {
      if (dir_entry && !dir_entry->noent)
	/* We've got a dir entry, get a node for it.  */
	{
	  /* If there's already a node, add a ref so that it doesn't go
             away.  */
	  spin_lock (&netfs_node_refcnt_lock);
	  if (dir_entry->node)
	    dir_entry->node->references++;
	  spin_unlock (&netfs_node_refcnt_lock);

	  if (! dir_entry->node)
	    /* No node; make one and install it into E.  */
	    {
	      if (! fs_path)
                err = EROFS;

	      if (! err)
		{
		  err = procfs_create_node (dir_entry, fs_path, &dir_entry->node);

		  if (!err && dir->num_live_entries++ == 0)
		    /* Keep a reference to dir's node corresponding to
		       children.  */
		    {
		      spin_lock (&netfs_node_refcnt_lock);
		      dir->node->references++;
		      spin_unlock (&netfs_node_refcnt_lock);
		    }
		}
	    }

	  if (! err)
	    {
	      *node = dir_entry->node;
	      /* We have to unlock DIR's node before locking the child node
		 because the locking order is always child-parent.  We know
		 the child node won't go away because we already hold the
		 additional reference to it.  */
	      mutex_unlock (&dir->node->lock);
	      mutex_lock (&dir_entry->node->lock);
	    }
	}
      else
	err = ENOENT;
    }

  if (err)
    {
      *node = 0;
      mutex_unlock (&dir->node->lock);
    }

#if 0
  if (fs_path)
    free (fs_path);
#endif

  return err;
}

/* Lookup the null name in DIR, and return a node for it in NODE.  Unlike
   procfs_dir_lookup, this won't attempt to validate the existance of the
   entry (to avoid opening a new connection if possible) -- that will happen
   the first time the entry is refreshed.  Also unlink ftpfs_dir_lookup, this
   function doesn't expect DIR to be locked, and won't return *NODE locked.
   This function is only used for bootstrapping the root node.  */
error_t
procfs_dir_null_lookup (struct procfs_dir *dir, struct node **node)
{
  struct procfs_dir_entry *dir_entry;
  error_t err = 0;

  dir_entry = lookup_entry (dir, "", 1);
  if (! dir_entry)
    return ENOMEM;

  if (! dir_entry->noent)
    /* We've got a dir entry, get a node for it.  */
    {
      /* If there's already a node, add a ref so that it doesn't go away.  */
      spin_lock (&netfs_node_refcnt_lock);
      if (dir_entry->node)
	dir_entry->node->references++;
      spin_unlock (&netfs_node_refcnt_lock);

      if (! dir_entry->node)
	/* No node; make one and install it into DIR_ENTRY.  */
	{
	  err = procfs_create_node (dir_entry, dir->fs_path, &dir_entry->node);

	  if (!err && dir->num_live_entries++ == 0)
	    /* Keep a reference to dir's node corresponding to children.  */
	    {
	      spin_lock (&netfs_node_refcnt_lock);
	      dir->node->references++;
	      spin_unlock (&netfs_node_refcnt_lock);
	    }
	}

      if (! err)
	*node = dir_entry->node;
    }
  else
    err = ENOENT;

  return err;
}

/* Free the directory entry DIR_ENTRY and all resources it consumes.  */
void
free_entry (struct procfs_dir_entry *dir_entry)
{

  assert (! dir_entry->self_p);	      /* We should only free deleted nodes.  */
  free (dir_entry->name);
  if (dir_entry->symlink_target)
    free (dir_entry->symlink_target);
  free (dir_entry->node->nn->dir);
  free (dir_entry->node->nn);
  free (dir_entry->node);
  free (dir_entry); 
}

/* Remove DIR_ENTRY from its position in the ordered_next chain.  */
static void
ordered_unlink (struct procfs_dir_entry *dir_entry)
{
  if (dir_entry->ordered_self_p)
    *dir_entry->ordered_self_p = dir_entry->ordered_next;
  if (dir_entry->ordered_next)
    dir_entry->ordered_next->self_p = dir_entry->ordered_self_p;
}

/* Delete DIR_ENTRY from its directory, freeing any resources it holds.  */
static void
delete (struct procfs_dir_entry *dir_entry, struct procfs_dir *dir)
{
  dir->num_entries--;
  
  /* Take out of the hash chain.  */
  if (dir_entry->self_p)
    *dir_entry->self_p = dir_entry->next;
  if (dir_entry->next)
    dir_entry->next->self_p = dir_entry->self_p;

  /* Take out of the directory ordered list.  */
  ordered_unlink (dir_entry); 

  /* If there's a node attached, we'll delete the entry whenever it goes
     away, otherwise, just delete it now.  */
  if (! dir_entry->node)
    free_entry (dir_entry);    
}

/* Make all the directory entries invalid  */
static void
make_dir_invalid (struct procfs_dir *dir)
{
  int count;
  size_t len = dir->htable_len;
  struct procfs_dir_entry **htable = dir->htable;
  struct procfs_dir_entry *dir_entry;

  for (count = 0; count < len; count++)
    {
      dir_entry = htable[count];
      while (dir_entry)
        {
          dir_entry->valid = 0;
          dir_entry = dir_entry->next;
        }
    }  
}

/* Delete any entries in DIR which don't have their valid bit set.  */
static void
sweep (struct procfs_dir *dir)
{
  size_t len = dir->htable_len, i;
  struct procfs_dir_entry **htable = dir->htable, *dir_entry;

  for (i = 0; i < len; i++)
    {
      dir_entry = htable[i];
      while (dir_entry)
        {
          if (!dir_entry->valid && !dir_entry->noent && dir->num_entries)
            delete (dir_entry, dir);	
          dir_entry = dir_entry->next;
        }
      if (htable[i])
        {
          free (htable[i]);
          htable[i] = 0;
        }

    }      

}

/* Remove the specified DIR and free all its allocated
   storage. */
void procfs_dir_entries_remove (struct procfs_dir *dir)
{
  /* Free all entries.  */
  make_dir_invalid (dir);
  sweep (dir);  
}

/* Checks if the DIR name is in list of 
   Active pids. */
int is_in_pid_list (struct procfs_dir *dir)
{
  int dir_name;
  int count;
  pid_t *pids = NULL;
  int pidslen = 0;
  error_t err;

  if (dir->node->nn)
    {
      dir_name = atoi (dir->node->nn->dir_entry->name);
      err = proc_getallpids (getproc (), &pids, &pidslen);

      for (count = 0; count < pidslen; ++count)
        if (pids[count] == dir_name)
          return 1;
    }

  return 0;

}

/* Checks if DIR is a directory that
   represents a pid. */
int check_parent (struct procfs_dir *dir)
{
  if (dir == dir->fs->root)
    return 0;
  else
    if (is_in_pid_list (dir))
      return 1;
    else
      return 0;

}

/* Refresh DIR.  */
error_t procfs_dir_refresh (struct procfs_dir *dir, int isroot)
{
  error_t err;
  int is_parent_pid;
  struct node *node;
  
  struct timeval tv;
  maptime_read (procfs_maptime, &tv); 
  
  time_t timestamp = tv.tv_sec;
  cur_entry = &dir->ordered;
  if (isroot)
    err = procfs_fill_root_dir(dir, timestamp);
  else
    {
      err = update_dir_entries (dir, timestamp);
      is_parent_pid = check_parent (dir);
      if (is_parent_pid)
          err = procfs_create_files (dir, &node, timestamp);    
    }

  return err;
}

/* Update the directory entry for NAME to reflect STAT and SYMLINK_TARGET. 
   This also creates a valid linked list of entries imposing ordering on
   them. */
struct procfs_dir_entry*
update_entries_list (struct procfs_dir *dir, const char *name,
                             const struct stat *stat, time_t timestamp,
                             const char *symlink_target)
{
  ino_t ino;
  struct procfs_dir_entry *dir_entry = lookup_entry (dir, name, 1);
  struct procfs *fs = dir->fs;
  
  if (! dir_entry)
    return ENOMEM;

  if (dir_entry->stat.st_ino)
    ino = dir_entry->stat.st_ino;
  else
    ino = fs->next_inode++;

  dir_entry->name_timestamp = timestamp;

  if (stat)
    /* The ST and SYMLINK_TARGET parameters are only valid if ST isn't 0.  */
    {
      dir_entry->stat = *stat;
      dir_entry->stat_timestamp = timestamp;

      if (!dir_entry->symlink_target || !symlink_target
	  || strcmp (dir_entry->symlink_target, symlink_target) != 0)
	{
	  if (dir_entry->symlink_target)
	    free (dir_entry->symlink_target);
	  dir_entry->symlink_target = symlink_target ? strdup (symlink_target) : 0;
	}
    }

  /* The st_ino field is always valid.  */
  dir_entry->stat.st_ino = ino;
  dir_entry->stat.st_fsid = fs->fsid;
  dir_entry->stat.st_fstype = PROCFILESYSTEM;
 
  dir_entry->valid = 1;

  if (! dir_entry->ordered_self_p)
    /* Position DIR_ENTRY in the ordered chain following the previously seen entry.  */
    {
      /* The PREV_ENTRY_NEXT_P field holds a pointer to the NEXT-field of the
	 previous entry, or a pointer to the ORDERED field in the directory. */
      dir_entry->ordered_self_p = cur_entry;

      if (*dir_entry->ordered_self_p)
	/* Update the self_p pointer of the previous successor.  */
	(*dir_entry->ordered_self_p)->ordered_self_p = &dir_entry->ordered_next;

      /* DIR_ENTRY comes before the previous successor.  */
      dir_entry->ordered_next = *dir_entry->ordered_self_p;

      *dir_entry->ordered_self_p = dir_entry;	/* Put DIR_ENTRY there.  */
    }

  /* Put the next entry after this one. */
  cur_entry = &dir_entry->ordered_next;

  return dir_entry;
}

/* Fills DIR, the root directory with all the pids of 
   processes running in the system as directories. */
error_t 
procfs_fill_root_dir(struct procfs_dir *dir, time_t timestamp)
{
  error_t err;
  char *data;
  pid_t *pids;
  int pidslen;
  struct stat stat;
  stat.st_mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | 
                  S_IROTH | S_IXOTH;
  stat.st_nlink = 1;
  stat.st_size = 0;

  int count;
  char *dir_name_pid;
  struct node *node;
  struct procfs_dir *new_dir;
  struct procfs_dir_entry *dir_entry;
  struct proc_stat *ps;
    
  pids = NULL;
  pidslen = 0;
  err = proc_getallpids (getproc (), &pids, &pidslen);

  if (!err)
    {
      for (count = 0; count < pidslen; count++)
	{
          if (asprintf (&dir_name_pid, "%d", pids[count]) == -1)
	    return errno;

#if 0
          node = (struct node *) malloc (sizeof (struct node));
          new_dir = (struct procfs_dir *) malloc (sizeof (struct procfs_dir ));

          if (! node || ! new_dir )
           return ENOMEM;
#endif
          err = _proc_stat_create (pids[count], ps_context, &ps);
          if (! err)
            {
              err = set_field_value (ps, PSTAT_PROC_INFO);
              if (! err)
                {
                  stat.st_uid = proc_stat_proc_info (ps)->owner;
                  stat.st_gid = proc_stat_proc_info (ps)->pgrp;

                  dir_entry = update_entries_list (dir, dir_name_pid,
                                 &stat, timestamp, NULL);
                  err = procfs_create_node (dir_entry, dir_name_pid, &node);

                  procfs_dir_create (dir->fs, node, 
                                      dir_name_pid, &new_dir);
                  free(dir_name_pid);
                  _proc_stat_free (ps);
                }
            }
	}
    }

  if ((err = procfs_create_uptime (dir, &node, timestamp)) != 0)
    return err;

  if ((err = procfs_create_stat (dir, &node, timestamp)) != 0)
    return err;
  
  if ((err = procfs_create_version (dir, &node, timestamp)) != 0)
    return err;

  if ((err = procfs_create_meminfo (dir, &node, timestamp)) != 0)
    return err;

  if ((err = procfs_create_loadavg (dir, &node, timestamp)) != 0)
    return err;

  if ((err = procfs_create_mounts (dir, &node, timestamp)) != 0)
    return err;

  return 0;
}

error_t update_dir_entries (struct procfs_dir *dir)
{
  /* STUB */
  return 0;
}