[go: up one dir, main page]

File: registry.c

package info (click to toggle)
drbd-utils 9.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,388 kB
  • sloc: ansic: 43,698; xml: 15,968; cpp: 7,783; sh: 3,699; makefile: 1,353; perl: 353
file content (202 lines) | stat: -rw-r--r-- 4,848 bytes parent folder | download | duplicates (17)
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
/*
   drbdadm_registry.c

   This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
   It was written by Johannes Thoma <johannes.thoma@linbit.com>

   Copyright (C) 2002-2008, LINBIT Information Technologies GmbH.
   Copyright (C) 2002-2008, Philipp Reisner <philipp.reisner@linbit.com>.
   Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.

   drbd 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, or (at your option)
   any later version.

   drbd 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 drbd; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

 */

/* This keeps track of which DRBD minor was configured in which
 * config file. This is required to have alternative config files
 * (-c switch) and userland event handlers.
 */


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <limits.h>

#include "config.h"
#include "registry.h"

static void linkname_from_minor(char *linkname, int minor)
{
	sprintf(linkname, "%s/drbd-minor-%d.conf", DRBD_RUN_DIR, minor);
}

int unregister_minor(int minor)
{
	char linkname[PATH_MAX];

	linkname_from_minor(linkname, minor);
	if (unlink(linkname) < 0) {
		if (errno != ENOENT) {
			perror("unlink");
			return -1;
		}
	}
	return 0;
}

static ssize_t __readlink(const char *path, char *buf, size_t bufsiz)
{
	ssize_t ret;

	ret = readlink(path, buf, bufsiz);
	if (ret >= 0) {
		if (ret >= bufsiz) {
			errno = ENAMETOOLONG;
			return -1;
		}
		buf[ret] = 0;
	}
	return ret;
}

static int register_path(const char *linkname, const char *path)
{
	char target[PATH_MAX];

	if (path[0] != '/') {
		fprintf(stderr, "File %s: absolute path expected; won't "
				"register relative path.",
			path);
		return -1;
	}
	/* safeguard against symlink loops in DRBD_RUN_DIR */
	if (!strncmp(path, DRBD_RUN_DIR "/", strlen(DRBD_RUN_DIR "/")))
		return -1;
	if (__readlink(linkname, target, sizeof(target)) >= 0 &&
	    !strcmp(target, path))
		return 0;
	if (unlink(linkname) != 0 && errno != ENOENT) {
		perror(linkname);
		return -1;
	}
	if (mkdir(DRBD_RUN_DIR, S_IRWXU) != 0 && errno != EEXIST) {
		perror(DRBD_RUN_DIR);
		return -1;
	}
	if (symlink(path, linkname) != 0) {
		fprintf(stderr, "symlink(%s, %s): %m\n", path, linkname);
		return -1;
	}
	return 0;
}

int register_minor(int minor, const char *path)
{
	char linkname[PATH_MAX];

	linkname_from_minor(linkname, minor);
	return register_path(linkname, path);
}

static char *resolve_symlink(const char *linkname)
{
	static char target[PATH_MAX];

	if (__readlink(linkname, target, sizeof(target)) < 0)
		return NULL;
	return target;
}

char *lookup_minor(int minor)
{
	static char linkname[PATH_MAX];
	struct stat stat_buf;

	linkname_from_minor(linkname, minor);
	if (stat(linkname, &stat_buf) != 0) {
		if (errno != ENOENT)
			perror(linkname);
		return NULL;
	}
	return resolve_symlink(linkname);
}

static void linkname_from_resource_name(char *linkname, const char *name)
{
	sprintf(linkname, "%s/drbd-resource-%s.conf", DRBD_RUN_DIR, name);
}

int unregister_resource(const char *name)
{
	char linkname[PATH_MAX];

	linkname_from_resource_name(linkname, name);
	if (unlink(linkname) != 0) {
		if (errno != ENOENT) {
			perror(linkname);
			return -1;
		}
	}
	return 0;
}

int register_resource(const char *name, const char *path)
{
	char linkname[PATH_MAX];

	linkname_from_resource_name(linkname, name);
	return register_path(linkname, path);
}

/* This returns a static buffer containing the real
 * configuration file known to be used last for this minor.
 * If you need the return value longer, stuff it away with strdup. */
char *lookup_resource(const char *name)
{
	static char linkname[PATH_MAX];
	struct stat stat_buf;

	linkname_from_resource_name(linkname, name);
	if (stat(linkname, &stat_buf) != 0) {
		if (errno != ENOENT)
			perror(linkname);
		return NULL;
	}
	return resolve_symlink(linkname);
}


#ifdef TEST

int main(int argc, char ** argv)
{
	register_minor(1, "/etc/drbd-xy.conf");
	register_minor(15, "/etc/drbd-82.conf");
	register_minor(14, "/../../../../../../etc/drbd-82.conf");
	printf("Minor 1 is %s.\n", lookup_minor(1));
	printf("Minor 2 is %s.\n", lookup_minor(2));
	printf("Minor 14 is %s.\n", lookup_minor(14));
	printf("Minor 15 is %s.\n", lookup_minor(15));
	return 0;
}

#endif