resource.c 2.39 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* resource.c
 *
 * External resources associated with a connection, for instance
 * processes and ports. Used to kill or release the resource in
 * question when the connection dies.
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * 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 of the
 * License, 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
J.H.M. Dassen's avatar
J.H.M. Dassen committed
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Niels Möller's avatar
Niels Möller committed
26
27
28
29
30
31
32
33
 */

#include "resource.h"

#include "werror.h"
#include "xalloc.h"

/* Prototypes */
34
void do_mark_resources(struct lsh_queue *q,
Niels Möller's avatar
Niels Möller committed
35
36
		       void (*mark)(struct lsh_object *o));

37
void do_free_resources(struct lsh_queue *q);
Niels Möller's avatar
Niels Möller committed
38
39
40
41
42
43
void dont_free_live_resource(int alive);

#define CLASS_DEFINE
#include "resource.h.x"
#undef CLASS_DEFINE

44
void do_mark_resources(struct lsh_queue *q,
Niels Möller's avatar
Niels Möller committed
45
46
		       void (*mark)(struct lsh_object *o))
{
47
  FOR_QUEUE(q, struct resource_node *, n)
Niels Möller's avatar
Niels Möller committed
48
49
50
    mark(&n->resource->super);
}

51
void do_free_resources(struct lsh_queue *q)
Niels Möller's avatar
Niels Möller committed
52
{
53
54
  FOR_QUEUE(q, struct resource_node *, n)
    lsh_space_free(n);
Niels Möller's avatar
Niels Möller committed
55
56
57
58
59
}

void dont_free_live_resource(int alive)
{
  if (alive)
60
61
    fatal("dont_free_live_resource: "
	  "About to garbage collect a live resource!\n");
Niels Möller's avatar
Niels Möller committed
62
63
64
65
66
67
68
69
70
71
}

static struct resource_node *do_remember_resource(struct resource_list *self,
						  struct resource *r)
{
  struct resource_node *n;

  NEW_SPACE(n);
  n->resource = r;

72
  lsh_queue_add_head(&self->q, &n->header);
Niels Möller's avatar
Niels Möller committed
73
74
75
76
77
78

  return n;
}

static void do_kill_all(struct resource_list *self)
{
79
80
  /* FIXME: Doesn't deallocate any nodes (but gc should do that
   * later). */
Niels Möller's avatar
Niels Möller committed
81

82
  FOR_QUEUE(&self->q, struct resource_node *, n)
Niels Möller's avatar
Niels Möller committed
83
84
85
86
87
88
    KILL_RESOURCE(n->resource);
}
  
struct resource_list *empty_resource_list(void)
{
  NEW(resource_list, self);
89
  lsh_queue_init(&self->q);
Niels Möller's avatar
Niels Möller committed
90
91
92
93
94
95
96

  self->remember = do_remember_resource;
  self->kill_all = do_kill_all;
  /* self->kill = do_kill_resource; */

  return self;
}