summaryrefslogtreecommitdiffstats
path: root/source3/lib/server_mutex.c
blob: cbb83575c022324fde0ec9f3f75884482fb375ae (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
/* 
   Unix SMB/CIFS implementation.
   Authenticate against a remote domain
   Copyright (C) Andrew Tridgell 1992-2002
   Copyright (C) Andrew Bartlett 2002
   
   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 "includes.h"
#include "system/filesys.h"
#include "lib/tdb_wrap/tdb_wrap.h"
#include "util_tdb.h"
#include "lib/param/param.h"

/* For reasons known only to MS, many of their NT/Win2k versions
   need serialised access only.  Two connections at the same time
   may (in certain situations) cause connections to be reset,
   or access to be denied.

   This locking allows smbd's multithread architecture to look
   like the single-connection that NT makes. */

struct named_mutex {
	struct tdb_wrap *tdb;
	char *name;
};

static int unlock_named_mutex(struct named_mutex *mutex)
{
	tdb_unlock_bystring(mutex->tdb->tdb, mutex->name);
	return 0;
}

struct named_mutex *grab_named_mutex(TALLOC_CTX *mem_ctx, const char *name,
				     int timeout)
{
	struct named_mutex *result;
	struct loadparm_context *lp_ctx;
	char *fname;

	result = talloc(mem_ctx, struct named_mutex);
	if (result == NULL) {
		DEBUG(0, ("talloc failed\n"));
		return NULL;
	}

	lp_ctx = loadparm_init_s3(result, loadparm_s3_helpers());
	if (lp_ctx == NULL) {
		DEBUG(0, ("loadparm_init_s3 failed\n"));
		talloc_free(result);
		return NULL;
	}

	result->name = talloc_strdup(result, name);
	if (result->name == NULL) {
		DEBUG(0, ("talloc failed\n"));
		TALLOC_FREE(result);
		return NULL;
	}

	fname = lock_path(talloc_tos(), "mutex.tdb");
	if (fname == NULL) {
		TALLOC_FREE(result);
		return NULL;
	}

	result->tdb = tdb_wrap_open(result, fname,
				    lpcfg_tdb_hash_size(lp_ctx, fname),
				    lpcfg_tdb_flags(lp_ctx,
						    TDB_DEFAULT |
						    TDB_CLEAR_IF_FIRST |
						    TDB_INCOMPATIBLE_HASH),
				    O_RDWR|O_CREAT, 0600);
	TALLOC_FREE(fname);
	talloc_unlink(result, lp_ctx);
	if (result->tdb == NULL) {
		DEBUG(1, ("Could not open mutex.tdb: %s\n",
			  strerror(errno)));
		TALLOC_FREE(result);
		return NULL;
	}

	if (tdb_lock_bystring_with_timeout(result->tdb->tdb, name,
					   timeout) != 0) {
		DEBUG(1, ("Could not get the lock for %s\n", name));
		TALLOC_FREE(result);
		return NULL;
	}

	talloc_set_destructor(result, unlock_named_mutex);
	return result;
}