File: cleanup.c

package info (click to toggle)
xfsdump 3.1.9%2B0
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 3,932 kB
  • sloc: ansic: 45,863; sh: 3,227; makefile: 545
file content (152 lines) | stat: -rw-r--r-- 2,811 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * 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.
 *
 * This program is distributed in the hope that it would 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 the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdlib.h>
#include <assert.h>

#include "cleanup.h"

struct cu {
	void (*cu_funcp)(void *arg1, void *arg2);
	void *cu_arg1;
	void *cu_arg2;
	int  cu_flags;
	struct cu *cu_nextp;
};

/* Cleanup structure flags
 */
#define CU_EARLY	0x00000001
			/* call cleanup routine before calling killall */

typedef struct cu cu_t;

static cu_t *cu_rootp;

void
cleanup_init(void)
{
	cu_rootp = 0;
}

static cleanup_t *
cleanup_register_base(void (*funcp)(void *arg1, void *arg2),
		  void *arg1,
		  void *arg2)
{
	cu_t *p;

	p = (cu_t *)calloc(1, sizeof(cu_t));
	assert(p);
	p->cu_funcp = funcp;
	p->cu_arg1 = arg1;
	p->cu_arg2 = arg2;
	p->cu_flags = 0;
	p->cu_nextp = cu_rootp;
	cu_rootp = p;

	return (cleanup_t *)p;
}

cleanup_t *
cleanup_register(void (*funcp)(void *arg1, void *arg2),
		  void *arg1,
		  void *arg2)
{
	cu_t *p;

	p = cleanup_register_base(funcp, arg1, arg2);

	return (cleanup_t *)p;
}

cleanup_t *
cleanup_register_early(void (*funcp)(void *arg1, void *arg2),
		  void *arg1,
		  void *arg2)
{
	cu_t *p;

	p = cleanup_register_base(funcp, arg1, arg2);
	p->cu_flags = CU_EARLY;

	return (cleanup_t *)p;
}

void
cleanup_cancel(cleanup_t *cleanupp)
{
	cu_t *p = (cu_t *)cleanupp;
	cu_t *nextp;
	cu_t *prevp;

	assert(cu_rootp);

	for (prevp = 0, nextp = cu_rootp
	      ;
	      nextp && nextp != p
	      ;
	      prevp = nextp, nextp = nextp->cu_nextp)
	;

	assert(nextp);
	if (prevp) {
		prevp->cu_nextp = p->cu_nextp;
	} else {
		cu_rootp = p->cu_nextp;
	}
	free((void *)p);
}

void
cleanup(void)
{
	while (cu_rootp) {
		cu_t *p = cu_rootp;
		(*p->cu_funcp)(p->cu_arg1, p->cu_arg2);
		cu_rootp = p->cu_nextp;
		free((void *)p);
	}
}

void
cleanup_early(void)
{
	cu_t *cuptr, *cuprevp;

	cuptr = cu_rootp;
	cuprevp = NULL;

	while (cuptr) {
		cu_t *cunextp = cuptr->cu_nextp;

		if (cuptr->cu_flags & CU_EARLY) {
			(*cuptr->cu_funcp)(cuptr->cu_arg1, cuptr->cu_arg2);
			free((void *)cuptr);
			if (cuprevp)  {
				cuprevp->cu_nextp = cunextp;
			} else {
				cu_rootp = cunextp;
			}
			cuptr = cunextp;
		} else {
			cuprevp = cuptr;
			cuptr = cunextp;
		}
	}
}