summaryrefslogtreecommitdiffstats
path: root/source3/lib/cleanupdb.c
blob: 93d6acc9e44db4506562ff9586c1918decd56dad (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
/*
   Unix SMB/CIFS implementation.
   Implementation of reliable cleanup events
   Copyright (C) Ralph Boehme 2016

   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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "cleanupdb.h"

struct cleanup_key {
	pid_t pid;
};

struct cleanup_rec {
	bool unclean;
};

static struct tdb_wrap *cleanup_db(void)
{
	static struct tdb_wrap *db;
	char *db_path = NULL;
	int tdbflags = TDB_INCOMPATIBLE_HASH | TDB_CLEAR_IF_FIRST |
		TDB_MUTEX_LOCKING;

	if (db != NULL) {
		return db;
	}

	db_path = lock_path(talloc_tos(), "smbd_cleanupd.tdb");
	if (db_path == NULL) {
		return NULL;
	}

	db = tdb_wrap_open(NULL, db_path, 0, tdbflags,
			   O_CREAT | O_RDWR, 0644);
	if (db == NULL) {
		DBG_ERR("Failed to open smbd_cleanupd.tdb\n");
	}

	TALLOC_FREE(db_path);
	return db;
}

bool cleanupdb_store_child(const pid_t pid, const bool unclean)
{
	struct tdb_wrap *db;
	struct cleanup_key key = { .pid = pid };
	struct cleanup_rec rec = { .unclean = unclean };
	TDB_DATA tdbkey = { .dptr = (uint8_t *)&key, .dsize = sizeof(key) };
	TDB_DATA tdbdata = { .dptr = (uint8_t *)&rec, .dsize = sizeof(rec) };
	int result;

	db = cleanup_db();
	if (db == NULL) {
		return false;
	}

	result = tdb_store(db->tdb, tdbkey, tdbdata, TDB_REPLACE);
	if (result != 0) {
		DBG_ERR("tdb_store failed for pid %d\n", (int)pid);
		return false;
	}

	return true;
}

bool cleanupdb_delete_child(const pid_t pid)
{
	struct tdb_wrap *db;
	struct cleanup_key key = { .pid = pid };
	TDB_DATA tdbkey = { .dptr = (uint8_t *)&key, .dsize = sizeof(key) };
	int result;

	db = cleanup_db();
	if (db == NULL) {
		return false;
	}

	result = tdb_delete(db->tdb, tdbkey);
	if (result != 0) {
		DBG_ERR("tdb_delete failed for pid %d\n", (int)pid);
		return false;
	}

	return true;
}

struct cleanup_read_state {
	int (*fn)(const pid_t pid, const bool cleanup, void *private_data);
	void *private_data;
};

static int cleanup_traverse_fn(struct tdb_context *tdb,
			       TDB_DATA key, TDB_DATA value,
			       void *private_data)
{
	struct cleanup_read_state *state =
		(struct cleanup_read_state *)private_data;
	struct cleanup_key ckey;
	struct cleanup_rec rec;
	int result;

	if (key.dsize != sizeof(struct cleanup_key)) {
		DBG_ERR("Found invalid key length %zu in cleanup.tdb\n",
			key.dsize);
		return -1;
	}
	memcpy(&ckey, key.dptr, sizeof(struct cleanup_key));

	if (value.dsize != sizeof(struct cleanup_rec)) {
		DBG_ERR("Found invalid value length %zu in cleanup.tdb\n",
			value.dsize);
		return -1;
	}
	memcpy(&rec, value.dptr, sizeof(struct cleanup_rec));

	result = state->fn(ckey.pid, rec.unclean, state->private_data);
	if (result != 0) {
		return -1;
	}

	return 0;
}

int cleanupdb_traverse_read(int (*fn)(const pid_t pid,
				      const bool cleanup,
				      void *private_data),
			    void *private_data)
{
	struct tdb_wrap *db;
	struct cleanup_read_state state;
	int result;

	db = cleanup_db();
	if (db == NULL) {
		return -1;
	}

	state = (struct cleanup_read_state) {
		.fn = fn,
		.private_data = private_data
	};

	result = tdb_traverse_read(db->tdb, cleanup_traverse_fn, &state);
	if (result < 0) {
		DBG_ERR("tdb_traverse_read failed\n");
		return -1;
	}

	return result;
}