summarylogtreecommitdiffstats
path: root/000-services.patch
blob: e7de6a50c4a3efdfcdde0b23ef612213ef99bce5 (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
diff --git a/README.md b/README.md
index afad2b1..ff3e652 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@ These are the main applications nitro is designed for:
 - As unprivileged supervision daemon on generic POSIX systems
 
 nitro is configured by a directory of scripts, defaulting to
-`/etc/nitro` (or the first command line argument).
+`/etc/nitro/services (or the first command line argument).
 
 ## Requirements
 
@@ -39,7 +39,7 @@ nitro is configured by a directory of scripts, defaulting to
 
 ## Services
 
-Every directory inside `/etc/nitro` (or your custom service directory)
+Every directory inside `/etc/nitro/services` (or your custom service directory)
 can contain several files:
 
 - `setup`, an optional executable file that is run before the service starts.
@@ -195,7 +195,7 @@ Where COMMAND is one of:
 - t: send signal SIGTERM to SERVICE
 - k: send signal SIGKILL to SERVICE
 - pidof: print the PID of the SERVICE, or return 1 if it's not up
-- rescan: re-read `/etc/nitro`, start added daemons, stop removed daemons
+- rescan: re-read `/etc/nitro/services`, start added daemons, stop removed daemons
 - Shutdown: shutdown (poweroff) the system
 - Reboot: reboot the system
 
@@ -236,7 +236,7 @@ the appropriate target.
 You can add this line to `/etc/ttys` to run `nitro` supervised by
 FreeBSD `init`:
 
-	/etc/nitro "/usr/local/sbin/nitro" "" on
+	/etc/nitro/services "/usr/local/sbin/nitro" "" on
 
 ## Periodic jobs
 
diff --git a/nitro.8 b/nitro.8
index f484baa..391d0b1 100644
--- a/nitro.8
+++ b/nitro.8
@@ -13,7 +13,7 @@ is a service supervisor that can be used as a Unix init system.
 .Pp
 Every directory inside
 .Ar dir
-.Pq by default: Pa /etc/nitro
+.Pq by default: Pa /etc/nitro/services
 defines a service.
 .Pp
 When used as init, and the argument
diff --git a/nitro.c b/nitro.c
index 599071f..4b262bf 100644
--- a/nitro.c
+++ b/nitro.c
@@ -143,7 +143,8 @@ DIR *cwd;
 DIR *notifydir;
 char notifypath[256];
 const char *control_socket_path;
-const char *servicedir = "/etc/nitro";
+char servicedir[PATH_MAX];	/*the services directory eg realpath(/etc/nitro/services)*/
+const char *basedir;			/*the base directory eg /etc/nitro*/
 
 long total_reaps;
 long total_sv_reaps;
@@ -302,6 +303,13 @@ panic()
 #define dprn(...) {}
 #endif
 
+int
+services_realpath(char *tsdir){
+	char buf[PATH_MAX];
+	sprn(buf, buf + sizeof buf, "%s/services", basedir);
+	return realpath(buf,tsdir)!=NULL;
+}
+
 int
 stat_slash(const char *dir, const char *name, struct stat *st)
 {
@@ -1152,36 +1160,40 @@ refresh_log:
 	return i;
 }
 
-static void
-reopendir(DIR **d)
-{
-#ifdef REOPEN_USE_CLOSEDIR
-	DIR *newd = opendir(servicedir);
-	if (!newd)
-		return;
-	closedir(*d);
-	*d = newd;
-	(void)!fchdir(dirfd(newd));
-#else
-
-	// avoid OpenBSD bug: ensure we hit end, so rewinddir forces re-read.
-	while (readdir(*d))
-		;
-
-#ifdef REOPEN_USE_DUP_HACK
-	// this fiddles in the internals of DIR, use only if you vetted
-	// your libc to tolerate this!
-	int fd = open(servicedir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
-	if (fd >= 0) {
-		if (dup3(fd, dirfd(*d), O_CLOEXEC) < 0)
-			close(fd);
-		else
-			(void)!fchdir(dirfd(*d));
+int
+check_sdir(){
+	/*
+	 * check to see if the realpath of basedir has changed
+	 * and if yes fix up servicedir and cwd
+	 * */
+	char tsdir[PATH_MAX];
+	int rval = 0;
+	/*check to see if the real path changed*/
+	if (!services_realpath(tsdir)){
+		dprn("check_services: '%s/services' is invalid\n", basedir);
+		rval = 1;
+	} else if(strcmp(servicedir,tsdir)){
+		DIR	*tcwd=NULL;
+		dprn("services: '%s' --> '%s'\n", servicedir, tsdir);
+		/*new services*/
+		tcwd = opendir(tsdir);
+		if (!tcwd){
+			dprn("opendir '%s': errno=%d\n", tsdir, errno);
+			rval = 2;
+		} else if (fchdir(dirfd(tcwd)) < 0){
+				dprn("chdir to '%s': errno=%d\n", tsdir, errno);
+				rval = 3;
+		} else {
+			closedir(cwd);	/*close current*/
+			cwd = tcwd;		/*set the globals*/
+			strncpy(servicedir,tsdir,PATH_MAX);
+		}
+	} else {
+		// avoid OpenBSD bug: ensure we hit end, so rewinddir forces re-read.
+		while (readdir(cwd))
+			;
 	}
-#endif
-
-	rewinddir(*d);
-#endif
+	return rval;
 }
 
 void
@@ -1192,8 +1204,8 @@ rescan()
 	for (i = 0; i < max_service; i++)
 		services[i].seen = 0;
 
-	reopendir(&cwd);
-
+	check_sdir();
+	rewinddir(cwd);
 	struct dirent *ent;
 	while ((ent = readdir(cwd))) {
 		char *name = ent->d_name;
@@ -1541,16 +1553,9 @@ handle_control_sock()
 	case 'd':
 	case 'r':
 	{
-		struct stat st;
-
 		if (!buf[1])
 			goto fail;
 		int i = find_service(buf + 1);
-		if (i < 0 &&
-		    (buf[0] != 'd' || find_service("SYS") != -1) &&
-		    valid_service_name(buf + 1) &&
-		    stat_slash_to_at(buf + 1, ".", &st) == 0)
-			i = add_service(buf + 1);
 		if (i < 0)
 			goto fail;
 		services[i].seen = 1;
@@ -1858,10 +1863,13 @@ main(int argc, char *argv[])
 			close(voidfd);
 	}
 
-	if (argc == 2)
-		servicedir = argv[1];
-	if (real_pid1 && (strcmp(servicedir, "S") == 0 || strcmp(servicedir, "single") == 0))
-		servicedir = "/etc/nitro.single";
+	basedir = (argc == 2) ? argv[1] : "/etc/nitro";
+	if (real_pid1 && (strcmp(basedir, "S") == 0 || strcmp(basedir, "single") == 0))
+		basedir = "/etc/nitro.single";
+
+	if(!services_realpath(servicedir)){
+		fatal("bad realpath for %s/services: errno=%d\n", basedir, errno);
+		}
 
 	cwd = opendir(servicedir);
 	if (!cwd)
diff --git a/nitroctl.c b/nitroctl.c
index b2284d2..fdcef0e 100644
--- a/nitroctl.c
+++ b/nitroctl.c
@@ -384,7 +384,7 @@ main(int argc, char *argv[])
 			execvp("nitro", argv);
 			dprintf(2, "nitroctl: exec init failed: %s\n", strerror(errno));
 
-			if (chdir(argc == 2 ? argv[1] : "/etc/nitro") < 0)
+			if (chdir(argc == 2 ? argv[1] : "/etc/nitro/services") < 0)
 				dprintf(2, "nitroctl: chdir failed: %s\n", strerror(errno));
 			execl("SYS/fatal", "SYS/fatal", (char *)0);
 			dprintf(2, "nitroctl: exec SYS/fatal failed: %s\n", strerror(errno));