summaryrefslogtreecommitdiff
path: root/systemd/nfs-server-generator.c
blob: eec98fd27fe865df989f7eaf0868ebe11a7bc131 (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
/*
 * nfs-server-generator:
 *   systemd generator to create ordering dependencies between
 *   nfs-server and various filesystem mounts
 *
 * 1/ nfs-server should start Before any 'nfs' mountpoints are
 *    mounted, in case they are loop-back mounts.  This ordering is particularly
 *    important for the shutdown side, so the nfs-server is stopped
 *    after the filesystems are unmounted.
 * 2/ nfs-server should start After all exported filesystems are mounted
 *    so there is no risk of exporting the underlying directory.
 *    This is particularly important for _net mounts which
 *    are not caught by "local-fs.target".
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <mntent.h>
#include <alloca.h>

#include "misc.h"
#include "nfslib.h"
#include "exportfs.h"
#include "systemd.h"

/* A simple "set of strings" to remove duplicates
 * found in /etc/exports
 */
struct list {
	struct list *next;
	char *name;
};
static int is_unique(struct list **lp, char *path)
{
	struct list *l = *lp;

	while (l) {
		if (strcmp(l->name, path) == 0)
			return 0;
		l = l->next;
	}
	l = malloc(sizeof(*l));
	if (l == NULL)
		return 0;
	l->name = path;
	l->next = *lp;
	*lp = l;
	return 1;
}

static int has_noauto_flag(char *path)
{
	FILE		*fstab;
	struct mntent	*mnt;

	fstab = setmntent("/etc/fstab", "r");
	if (!fstab)
		return 0;

	while ((mnt = getmntent(fstab)) != NULL) {
		int l = strlen(mnt->mnt_dir);
		if (strncmp(mnt->mnt_dir, path, l) != 0)
			continue;
		if (path[l] && path[l] != '/')
			continue;
		if (hasmntopt(mnt, "noauto"))
			break;
	}
	fclose(fstab);
	return mnt != NULL;
}

int main(int argc, char *argv[])
{
	char		*path, *spath;
	char		dirbase[] = "/nfs-server.service.d";
	char		filebase[] = "/order-with-mounts.conf";
	nfs_export	*exp;
	int		i;
	struct list	*list = NULL;
	FILE		*f, *fstab;
	struct mntent	*mnt;

	/* Avoid using any external services */
	xlog_syslog(0);

	if (argc != 4 || argv[1][0] != '/') {
		fprintf(stderr, "nfs-server-generator: create systemd dependencies for nfs-server\n");
		fprintf(stderr, "Usage: normal-dir early-dir late-dir\n");
		exit(1);
	}

	path = alloca(strlen(argv[1]) + sizeof(dirbase) + sizeof(filebase));
	if (!path)
		exit(2);
	if (export_read(_PATH_EXPORTS, 1) +
	    export_d_read(_PATH_EXPORTS_D, 1) == 0)
		/* Nothing is exported, so nothing to do */
		exit(0);

	strcat(strcpy(path, argv[1]), dirbase);
	mkdir(path, 0755);
	strcat(path, filebase);
	f = fopen(path, "w");
	if (!f)
		exit(1);
	fprintf(f, "# Automatically generated by nfs-server-generator\n\n[Unit]\n");

	for (i = 0; i < MCL_MAXTYPES; i++) {
		for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
			if (!is_unique(&list, exp->m_export.e_path))
				continue;
			if (exp->m_export.e_mountpoint)
				continue;
			if (has_noauto_flag(exp->m_export.e_path))
				continue;
			if (strchr(exp->m_export.e_path, ' '))
				fprintf(f, "RequiresMountsFor=\"%s\"\n",
					exp->m_export.e_path);
			else
				fprintf(f, "RequiresMountsFor=%s\n",
					exp->m_export.e_path);
		}
	}

	fstab = setmntent("/etc/fstab", "r");
	if (!fstab)
		exit(1);

	while ((mnt = getmntent(fstab)) != NULL) {
		if (strcmp(mnt->mnt_type, "nfs") != 0 &&
		    strcmp(mnt->mnt_type, "nfs4") != 0)
			continue;

		spath = systemd_escape(mnt->mnt_dir, ".mount");
		if (!spath) {
			fprintf(stderr, 
				"nfs-server-generator: convert path failed: %s\n",
				mnt->mnt_dir);
			continue;
		}
		fprintf(f, "Before=%s\n", spath);
	}

	fclose(fstab);
	fclose(f);

	exit(0);
}