summaryrefslogtreecommitdiff
path: root/source3/lib/messages.c
blob: 30eef40ec9118400b4a12f704696c8ed1a849297 (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
/* 
   Unix SMB/Netbios implementation.
   Version 3.0
   Samba internal messaging functions
   Copyright (C) Andrew Tridgell 2000
   
   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
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/* this module is used for internal messaging between Samba daemons. */

#include "includes.h"

/* the locking database handle */
static TDB_CONTEXT *tdb;
static int received_signal;

/* change the message version with any incompatible changes in the protocol */
#define MESSAGE_VERSION 1

struct message_rec {
	int msg_version;
	enum message_type msg_type;
	pid_t dest;
	pid_t src;
	size_t len;
};

/****************************************************************************
notifications come in as signals
****************************************************************************/
static void sig_usr1(void)
{
	received_signal = 1;
	sys_select_signal();
}

/****************************************************************************
 Initialise the messaging functions. 
****************************************************************************/
BOOL message_init(void)
{
	if (tdb) return True;

	tdb = tdb_open(lock_path("messages.tdb"), 
		       0, TDB_CLEAR_IF_FIRST, 
		       O_RDWR|O_CREAT,0600);

	if (!tdb) {
		DEBUG(0,("ERROR: Failed to initialise messages database\n"));
		return False;
	}

	CatchSignal(SIGUSR1, sig_usr1);

	return True;
}


/*******************************************************************
 form a static tdb key from a pid
******************************************************************/
static TDB_DATA message_key_pid(pid_t pid)
{
	static char key[20];
	TDB_DATA kbuf;

	slprintf(key, sizeof(key), "PID/%d", (int)pid);

	kbuf.dptr = (char *)key;
	kbuf.dsize = sizeof(key);
	return kbuf;
}


/****************************************************************************
notify a process that it has a message. If the process doesn't exist 
then delete its record in the database
****************************************************************************/
static BOOL message_notify(pid_t pid)
{
	if (kill(pid, SIGUSR1) == -1) {
		if (errno == ESRCH) {
			DEBUG(2,("pid %d doesn't exist - deleting messages record\n", (int)pid));
			tdb_delete(tdb, message_key_pid(pid));
		} else {
			DEBUG(2,("message to process %d failed - %s\n", (int)pid, strerror(errno)));
		}
		return False;
	}
	return True;
}

/****************************************************************************
send a message to a particular pid
****************************************************************************/
BOOL message_send_pid(pid_t pid, enum message_type msg_type, void *buf, size_t len)
{
	TDB_DATA kbuf;
	TDB_DATA dbuf;
	struct message_rec rec;
	void *p;

	rec.msg_version = MESSAGE_VERSION;
	rec.msg_type = msg_type;
	rec.dest = pid;
	rec.src = sys_getpid();
	rec.len = len;

	kbuf = message_key_pid(pid);

	/* lock the record for the destination */
	tdb_lockchain(tdb, kbuf);

	dbuf = tdb_fetch(tdb, kbuf);

	if (!dbuf.dptr) {
		/* its a new record */
		p = (void *)malloc(len + sizeof(rec));
		if (!p) goto failed;

		memcpy(p, &rec, sizeof(rec));
		memcpy(p+sizeof(rec), buf, len);

		dbuf.dptr = p;
		dbuf.dsize = len + sizeof(rec);
		tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
		free(p);
		goto ok;
	}

	/* we're adding to an existing entry */
	p = (void *)malloc(dbuf.dsize + len + sizeof(rec));
	if (!p) goto failed;

	memcpy(p, dbuf.dptr, dbuf.dsize);
	memcpy(p+dbuf.dsize, &rec, sizeof(rec));
	memcpy(p+dbuf.dsize+sizeof(rec), buf, len);

	dbuf.dptr = p;
	dbuf.dsize += len + sizeof(rec);
	tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
	free(dbuf.dptr);
	free(p);

 ok:
	tdb_unlockchain(tdb, kbuf);
	return message_notify(pid);

 failed:
	tdb_unlockchain(tdb, kbuf);
	return False;
}



/****************************************************************************
retrieve the next message for the current process
****************************************************************************/
static BOOL message_recv(enum message_type *msg_type, pid_t *src, void **buf, size_t *len)
{
	TDB_DATA kbuf;
	TDB_DATA dbuf;
	struct message_rec rec;

	kbuf = message_key_pid(sys_getpid());

	tdb_lockchain(tdb, kbuf);
	
	dbuf = tdb_fetch(tdb, kbuf);
	if (dbuf.dptr == NULL || dbuf.dsize == 0) goto failed;

	memcpy(&rec, dbuf.dptr, sizeof(rec));

	if (rec.msg_version != MESSAGE_VERSION) {
		DEBUG(0,("message version %d received (expected %d)\n", rec.msg_version, MESSAGE_VERSION));
		goto failed;
	}

	(*buf) = (void *)malloc(rec.len);
	if (!(*buf)) goto failed;

	memcpy(*buf, dbuf.dptr+sizeof(rec), rec.len);
	*len = rec.len;
	*msg_type = rec.msg_type;
	*src = rec.src;

	memmove(dbuf.dptr, dbuf.dptr+sizeof(rec)+rec.len, dbuf.dsize - (sizeof(rec)+rec.len));
	dbuf.dsize -= sizeof(rec)+rec.len;
	tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);

	free(dbuf.dptr);
	tdb_unlockchain(tdb, kbuf);
	return True;

 failed:
	tdb_unlockchain(tdb, kbuf);
	return False;
}


/****************************************************************************
receive and dispatch any messages pending for this process
****************************************************************************/
void message_dispatch(void)
{
	enum message_type msg_type;
	pid_t src;
	void *buf;
	size_t len;

	if (!received_signal) return;
	received_signal = 0;

	while (message_recv(&msg_type, &src, &buf, &len)) {
		switch (msg_type) {
		case MSG_DEBUG:
			debug_message(src, buf, len);
			break;
		default:
			DEBUG(0,("Unknown message type %d from %d\n", msg_type, (int)src));
			break;
		}
	}
}