Check-in [fdf89fd103]
Overview
Comment:Cleaned up code in preparation for Windows build
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: fdf89fd103aeacbfb6114a7c8827763449d296b1
User & Date: rkeene on 2019-12-13 00:33:36
Other Links: manifest | tags
Context
2019-12-13
00:34
AppFS 1.12 check-in: d7fb4b713a user: rkeene tags: trunk, 1.12
00:33
Cleaned up code in preparation for Windows build check-in: fdf89fd103 user: rkeene tags: trunk
00:11
Updated root CA certificate, the previous one expired, and made errors about this more informative check-in: 7241c0986c user: rkeene tags: trunk
Changes

Modified appfsd.c from [b7dddfc0b8] to [0494c72e1d].

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
17
18
19
20
21
22
23

24
25
26
27

28
29
30
31
32
33
34







-




-







 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#define FUSE_USE_VERSION 26

#include <sys/resource.h>  
#include <sys/fsuid.h>
#include <sys/types.h>
#include <sys/time.h>
#include <pthread.h>
#include <signal.h>
#include <limits.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
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
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







-
+



-
-
+
+

+










+
-
+

+


+







 */
#ifndef APPFS_CACHEDIR
#define APPFS_CACHEDIR "/var/cache/appfs"
#endif

/* Debugging macros */
#ifdef DEBUG
int appfs_debug_fd = STDERR_FILENO;
FILE *appfs_debug_fd = NULL;
#define APPFS_DEBUG(x...) { \
	char buf[8192]; \
	int bufoff = 0; \
	if (appfs_debug_fd == -1) { \
		appfs_debug_fd = open("/tmp/appfsd.log", O_WRONLY | O_APPEND | O_CREAT, 0600); \
	if (appfs_debug_fd == NULL) { \
		appfs_debug_fd = fopen("/tmp/appfsd.log", "a"); \
	}; \
	if (appfs_debug_fd == NULL) { appfs_debug_fd = stderr; } \
	bufoff = snprintf(buf, sizeof(buf), "[debug] [t=%llx] %s:%i:%s: ", (unsigned long long) pthread_self(), __FILE__, __LINE__, __func__); \
	if (bufoff < sizeof(buf)) { \
		bufoff += snprintf(buf + bufoff, sizeof(buf) - bufoff, x); \
	}; \
	if (bufoff < sizeof(buf)) { \
		bufoff += snprintf(buf + bufoff, sizeof(buf) - bufoff, "\n");\
	} \
	if (bufoff > sizeof(buf)) { \
		bufoff = sizeof(buf); \
	}; \
	fprintf(appfs_debug_fd, "%.*s", bufoff, buf); \
	write(appfs_debug_fd, buf, bufoff); \
	fflush(appfs_debug_fd); \
}
#define APPFS_ERROR(x...) APPFS_DEBUG(x)
#else
#define APPFS_DEBUG(x...) /**/
#define APPFS_ERROR(x...) fprintf(stderr, x); fprintf(stderr, "\n");
#endif

/*
 * SHA1 Tcl Package initializer, from sha1.o
 */
int Sha1_Init(Tcl_Interp *interp);

93
94
95
96
97
98
99
100

101
102
103
104
105
106
107
95
96
97
98
99
100
101

102
103
104
105
106
107
108
109







-
+







/*
 * Global variables for AppFS caching
 */
pthread_mutex_t appfs_path_info_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
int appfs_path_info_cache_size = 8209;
struct appfs_pathinfo *appfs_path_info_cache = NULL;

#ifndef TCL_THREADS
#if !defined(TCL_THREADS) || TCL_THREADS != 1
/*
 * Handle unthreaded Tcl
 */
pthread_mutex_t appfs_tcl_big_global_lock = PTHREAD_MUTEX_INITIALIZER;
#define appfs_call_libtcl_enter pthread_mutex_lock(&appfs_tcl_big_global_lock);
#define appfs_call_libtcl_exit pthread_mutex_unlock(&appfs_tcl_big_global_lock);
#else
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
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







-
+














-
+

-
+








	APPFS_DEBUG("Creating new Tcl interpreter for TID = 0x%llx", (unsigned long long) pthread_self());

	appfs_call_libtcl(
		interp = Tcl_CreateInterp();
	)
	if (interp == NULL) {
		fprintf(stderr, "Unable to create Tcl Interpreter.  Aborting.\n");
		APPFS_ERROR("Unable to create Tcl Interpreter.  Aborting.");

		if (error_string) {
			*error_string = strdup("Unable to create Tcl interpreter.");
		}

		return(NULL);
	}

	appfs_call_libtcl(Tcl_Preserve(interp);)

	appfs_call_libtcl(
		tcl_ret = Tcl_Init(interp);
	)
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl.  Aborting.\n");
		APPFS_ERROR("Unable to initialize Tcl.  Aborting.");
		appfs_call_libtcl(
			fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));
			APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
		)

		if (error_string) {
			appfs_call_libtcl(
				*error_string = strdup(Tcl_GetStringResult(interp));
			)
		}
209
210
211
212
213
214
215
216

217
218

219
220
221
222
223
224
225
211
212
213
214
215
216
217

218
219

220
221
222
223
224
225
226
227







-
+

-
+







		return(NULL);
	}

	appfs_call_libtcl(
		tcl_ret = Tcl_Eval(interp, "package ifneeded sha1 1.0 [list load {} sha1]");
	)
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl SHA1.  Aborting.\n");
		APPFS_ERROR("Unable to initialize Tcl SHA1.  Aborting.");
		appfs_call_libtcl(
			fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));
			APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
		)

		if (error_string) {
			appfs_call_libtcl(
				*error_string = strdup(Tcl_GetStringResult(interp));
			)
		}
233
234
235
236
237
238
239
240

241
242

243
244
245
246
247
248
249
235
236
237
238
239
240
241

242
243

244
245
246
247
248
249
250
251







-
+

-
+







		return(NULL);
	}

	appfs_call_libtcl(
		tcl_ret = Tcl_Eval(interp, "package ifneeded appfsd 1.0 [list load {} appfsd]");
	)
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl AppFS Package.  Aborting.\n");
		APPFS_ERROR("Unable to initialize Tcl AppFS Package.  Aborting.");
		appfs_call_libtcl(
			fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));
			APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
		)

		if (error_string) {
			appfs_call_libtcl(
				*error_string = strdup(Tcl_GetStringResult(interp));
			)
		}
262
263
264
265
266
267
268
269

270
271

272
273
274
275
276
277
278
264
265
266
267
268
269
270

271
272

273
274
275
276
277
278
279
280







-
+

-
+







	 */
	appfs_call_libtcl_enter
		tcl_ret = Tcl_Eval(interp, ""
#include "pki.tcl.h"
		"");
	appfs_call_libtcl_exit
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl PKI.  Aborting.\n");
		APPFS_ERROR("Unable to initialize Tcl PKI.  Aborting.");
		appfs_call_libtcl(
			fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));
			APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
		)

		if (error_string) {
			appfs_call_libtcl(
				*error_string = strdup(Tcl_GetStringResult(interp));
			)
		}
293
294
295
296
297
298
299
300

301
302

303
304
305
306
307
308
309
295
296
297
298
299
300
301

302
303

304
305
306
307
308
309
310
311







-
+

-
+







	 */
	appfs_call_libtcl_enter
		tcl_ret = Tcl_Eval(interp, ""
#include "appfsd.tcl.h"
		"");
	appfs_call_libtcl_exit
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl AppFS script.  Aborting.\n");
		APPFS_ERROR("Unable to initialize Tcl AppFS script.  Aborting.");
		appfs_call_libtcl(
			fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));
			APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
		)

		if (error_string) {
			appfs_call_libtcl(
				*error_string = strdup(Tcl_GetStringResult(interp));
			)
		}
320
321
322
323
324
325
326
327

328
329
330
331
332
333
334
322
323
324
325
326
327
328

329
330
331
332
333
334
335
336







-
+







	/*
	 * Set global variables from C to Tcl
	 */
	appfs_call_libtcl(
		tcl_setvar_ret = Tcl_SetVar(interp, "::appfs::cachedir", appfs_cachedir, TCL_GLOBAL_ONLY);
	)
	if (tcl_setvar_ret == NULL) {
		fprintf(stderr, "Unable to set cache directory.  This should never fail.\n");
		APPFS_ERROR("Unable to set cache directory.  This should never fail.");

		if (error_string) {
			appfs_call_libtcl(
				*error_string = strdup(Tcl_GetStringResult(interp));
			)
		}

345
346
347
348
349
350
351
352

353
354

355
356
357
358
359
360
361
347
348
349
350
351
352
353

354
355

356
357
358
359
360
361
362
363







-
+

-
+







	 * Initialize the "appfsd.tcl" environment, which must be done after
	 * global variables are set.
	 */
	appfs_call_libtcl(
		tcl_ret = Tcl_Eval(interp, "::appfs::init");
	)
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl AppFS script (::appfs::init).  Aborting.\n");
		APPFS_ERROR("Unable to initialize Tcl AppFS script (::appfs::init).  Aborting.");
		appfs_call_libtcl(
			fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));
			APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
		)

		if (error_string) {
			appfs_call_libtcl(
				*error_string = strdup(Tcl_GetStringResult(interp));
			)
		}
562
563
564
565
566
567
568





569
570
571
572
573
574
575
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582







+
+
+
+
+







}

static void appfs_simulate_user_fs_leave(void) {
	setfsuid(0);
	setfsgid(0);
}

#ifdef APPFS_NO_GETPWUID
static char *appfs_get_homedir(uid_t fsuid) {
	return(NULL);
}
#else
/*
 * Look up the home directory for a given UID
 *        Returns a C string containing the user's home directory or NULL if
 *        the user's home directory does not exist or is not correctly
 *        configured
 */
static char *appfs_get_homedir(uid_t fsuid) {
614
615
616
617
618
619
620

621
622
623
624
625
626
627
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635







+







		return(NULL);
	}

	retval = strdup(result->pw_dir);

	return(retval);
}
#endif

/*
 * Generate an inode for a given path.  The inode should be computed in such
 * a way that it is unlikely to be duplicated and remains the same for a given
 * file
 *
 * Current implementation is an FNV-1a 32-bit
1517
1518
1519
1520
1521
1522
1523










1524

1525
1526
1527
1528
1529
1530
1531
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550







+
+
+
+
+
+
+
+
+
+

+







	int retval;

	APPFS_DEBUG("Enter (path = %s, buf, size = %lli, offset = %lli, fd = %lli)", path, (long long) size, (long long) offset, (long long) fi->fh);

	retval = 0;

	while (size != 0) {
#ifdef APPFS_NO_PREAD /* XXX:TODO: Write a wrapper function */
		off_t seek_ret;

		seek_ret = lseek(fi->fh, offset, SEEK_SET);
		if (seek_ret == offset) {
			read_ret = read(fi->fh, buf, size);
		} else {
			read_ret = -1;
		}
#else
		read_ret = pread(fi->fh, buf, size, offset);
#endif

		if (read_ret < 0) {
			APPFS_DEBUG("error: read failed");

			return(errno * -1);
		}

1563
1564
1565
1566
1567
1568
1569















1570

1571
1572
1573
1574
1575
1576
1577
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+







#endif

	appfs_get_path_info_cache_rm(path, appfs_get_fsuid());

	retval = 0;

	while (size != 0) {
#ifdef APPFS_NO_PWRITE /* XXX:TODO: Write a wrapper function */
#  if 1
		/* XXX:TODO: Still fails on win32 */
		write_ret = -1;
#else
		off_t seek_ret;

		seek_ret = lseek(fi->fh, offset, SEEK_SET);
		if (seek_ret == offset) {
			write_ret = write(fi->fh, buf, size); 
		} else {
			write_ret = -1;
		}
#  endif
#else
		write_ret = pwrite(fi->fh, buf, size, offset);
#endif

		if (write_ret < 0) {
			APPFS_DEBUG("error: write failed");

			return(errno * -1);
		}

1835
1836
1837
1838
1839
1840
1841
1842

1843
1844
1845
1846
1847
1848
1849
1850
1851

1852
1853
1854
1855
1856
1857
1858
1870
1871
1872
1873
1874
1875
1876

1877
1878
1879
1880
1881
1882
1883
1884
1885

1886
1887
1888
1889
1890
1891
1892
1893







-
+








-
+







static int appfs_sqlite3(const char *sql) {
	Tcl_Interp *interp;
	const char *sql_ret;
	int tcl_ret;

	interp = appfs_create_TclInterp(NULL);
	if (interp == NULL) {
		fprintf(stderr, "Unable to create a Tcl interpreter.  Aborting.\n");
		APPFS_ERROR("Unable to create a Tcl interpreter.  Aborting.");

		return(1);
	}

	tcl_ret = appfs_Tcl_Eval(interp, 5, "::appfs::db", "eval", sql, "row", "unset -nocomplain row(*); parray row; puts \"----\"");
	sql_ret = Tcl_GetStringResult(interp);

	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "[error] %s\n", sql_ret);
		APPFS_ERROR("[error] %s", sql_ret);

		return(1);
	}

	if (sql_ret && sql_ret[0] != '\0') {
		printf("%s\n", sql_ret);
	}
1866
1867
1868
1869
1870
1871
1872
1873

1874
1875
1876
1877
1878
1879
1880
1881
1882

1883
1884
1885
1886
1887
1888
1889
1901
1902
1903
1904
1905
1906
1907

1908
1909
1910
1911
1912
1913
1914
1915
1916

1917
1918
1919
1920
1921
1922
1923
1924







-
+








-
+







static int appfs_tcl(const char *tcl) {
	Tcl_Interp *interp;
	const char *tcl_result;
	int tcl_ret;

	interp = appfs_create_TclInterp(NULL);
	if (interp == NULL) {
		fprintf(stderr, "Unable to create a Tcl interpreter.  Aborting.\n");
		APPFS_ERROR("Unable to create a Tcl interpreter.  Aborting.");

		return(1);
	}

	tcl_ret = Tcl_Eval(interp, tcl);
	tcl_result = Tcl_GetStringResult(interp);

	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "[error] %s\n", Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY));
		APPFS_ERROR("[error] %s", Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY));

		return(1);
	}

	if (tcl_result && tcl_result[0] != '\0') {
		printf("%s\n", tcl_result);
	}
2016
2017
2018
2019
2020
2021
2022







2023
2024
2025
2026
2027
2028
2029
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071







+
+
+
+
+
+
+







	Tcl_CreateObjCommand(interp, "appfsd::get_path_info_cache_flush", tcl_appfs_get_path_info_cache_flush, NULL, NULL);

	Tcl_PkgProvide(interp, "appfsd", "1.0");

	return(TCL_OK);
}

#ifdef APPFS_NO_SIGNALS
static void appfs_set_sighandler(void) {
	return;
}
#else
#include <signal.h>

/*
 * Hot-restart support
 */
/* Initiate a hot-restart */
static void appfs_hot_restart(void) {
	APPFS_DEBUG("Asked to initiate hot restart");

2048
2049
2050
2051
2052
2053
2054













2055
2056
2057
2058
2059
2060
2061
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116







+
+
+
+
+
+
+
+
+
+
+
+
+







	if (sig == SIGHUP) {
		appfs_hot_restart();
	}

	return;
}

static void appfs_set_sighandler(void) {
	void *signal_ret;

	/*
	 * Register a signal handler for hot-restart requests
	 */
	signal_ret = signal(SIGHUP, appfs_signal_handler);
	if (signal_ret == SIG_ERR) {
		APPFS_ERROR("Unable to install signal handler for hot-restart");
		APPFS_ERROR("Hot-restart will not be available.");
	}
}
#endif
/*
 * Terminate a thread
 */
static void appfs_terminate_interp_and_thread(void *_interp) {
	Tcl_Interp *interp;

	APPFS_DEBUG("Called: _interp = %p", _interp);
2101
2102
2103
2104
2105
2106
2107
2108

2109
2110
2111
2112
2113
2114
2115
2156
2157
2158
2159
2160
2161
2162

2163
2164
2165
2166
2167
2168
2169
2170







-
+







	int ch;
	char *optstr, *optstr_next, *optstr_s;
	char fake_arg[3] = {'-', 0, 0};

	/*
	 * Default values
	 */
#ifdef TCL_THREADS
#if defined(TCL_THREADS) && TCL_THREADS == 1
	appfs_threaded_tcl = 1;
#else
	appfs_threaded_tcl = 0;
#endif

	/**
	 ** Add FUSE arguments which we always supply
2160
2161
2162
2163
2164
2165
2166
2167

2168
2169
2170
2171
2172
2173
2174
2215
2216
2217
2218
2219
2220
2221

2222
2223
2224
2225
2226
2227
2228
2229







-
+







						APPFS_DEBUG("Passing option to FUSE: -o allow_other");

						fuse_opt_parse(args, NULL, NULL, NULL);
						fuse_opt_add_arg(args, "-oallow_other");
					} else if (strcmp(optstr, "rw") == 0) {
						/* Ignored */
					} else {
						fprintf(stderr, "appfsd: invalid option: \"-o %s\"\n", optstr);
						APPFS_ERROR("appfsd: invalid option: \"-o %s\"", optstr);

						free(optstr_s);

						return(1);
					}
				}

2200
2201
2202
2203
2204
2205
2206
2207

2208
2209

2210
2211
2212
2213
2214
2215
2216
2255
2256
2257
2258
2259
2260
2261

2262
2263

2264
2265
2266
2267
2268
2269
2270
2271







-
+

-
+








				return(1);
		}
	}

	if ((optind + 2) != argc) {
		if ((optind + 2) < argc) {
			fprintf(stderr, "Too many arguments\n");
			APPFS_ERROR("Too many arguments");
		} else {
			fprintf(stderr, "Missing cachedir or mountpoint\n");
			APPFS_ERROR("Missing cachedir or mountpoint");
		}

		appfs_print_help(stderr);

		return(1);
	}

2246
2247
2248
2249
2250
2251
2252

















































2253
2254
2255
2256
2257
2258
2259
2260
2261

2262
2263
2264

2265
2266
2267
2268
2269
2270
2271
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363


2364

2365

2366
2367
2368
2369
2370
2371
2372
2373







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







-
-
+
-

-
+







	.unlink    = appfs_fuse_unlink_rmdir,
	.rmdir     = appfs_fuse_unlink_rmdir,
	.mkdir     = appfs_fuse_mkdir,
	.chmod     = appfs_fuse_chmod,
	.symlink   = appfs_fuse_symlink,
};


#ifdef APPFS_NO_RLIMIT
static void appfs_set_resource_limits(void) {
	return;
}
#else
#include <sys/resource.h>  

static void appfs_set_resource_limits(void) {
	struct rlimit number_open_files;
	rlim_t number_open_files_max;
	int rlimit_ret;

	/*
	 * Increase resource limits for number of open files
	 * to the maximum values, since we may be asked to
	 * hold open many files on behalf of many other processes
	 */
	number_open_files.rlim_cur = number_open_files.rlim_max = RLIM_INFINITY;

	rlimit_ret = setrlimit(RLIMIT_NOFILE, &number_open_files);

	if (rlimit_ret != 0) {
		rlimit_ret = getrlimit(RLIMIT_NOFILE, &number_open_files);
		if (rlimit_ret == 0) {
			number_open_files_max = number_open_files.rlim_max;

			if (number_open_files_max < (1024 * 1024)) {
				number_open_files.rlim_cur = number_open_files.rlim_max = 1024 * 1024;

				rlimit_ret = setrlimit(RLIMIT_NOFILE, &number_open_files);
			} else {
				number_open_files.rlim_cur = number_open_files.rlim_max;
			}

			rlimit_ret = setrlimit(RLIMIT_NOFILE, &number_open_files);

			if (rlimit_ret != 0 && number_open_files.rlim_cur != number_open_files_max) {
				number_open_files.rlim_cur = number_open_files.rlim_max = number_open_files_max;

				setrlimit(RLIMIT_NOFILE, &number_open_files);
			}
		}
	}

	return;
}
#endif

/*
 * Entry point into this program.
 */
int main(int argc, char **argv) {
	Tcl_Interp *test_interp;
	char *test_interp_error;
	struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
	struct rlimit number_open_files;
	int pthread_ret, aop_ret, rlimit_ret;
	int pthread_ret, aop_ret;
	void *signal_ret;
	char *argv0;
	rlim_t number_open_files_max;
int i;

	/*
	 * Skip passed program name
	 */
	if (argc == 0 || argv == NULL) {
		return(1);
	}
2302
2303
2304
2305
2306
2307
2308
2309

2310
2311
2312
2313
2314
2315
2316
2404
2405
2406
2407
2408
2409
2410

2411
2412
2413
2414
2415
2416
2417
2418







-
+







	/*
	 * Create a thread-specific-data (TSD) key for each thread to refer
	 * to its own Tcl interpreter.  Tcl interpreters must be unique per
	 * thread and new threads are dynamically created by FUSE.
	 */
	pthread_ret = pthread_key_create(&interpKey, appfs_terminate_interp_and_thread);
	if (pthread_ret != 0) {
		fprintf(stderr, "Unable to create TSD key for Tcl.  Aborting.\n");
		APPFS_ERROR("Unable to create TSD key for Tcl.  Aborting.");

		return(1);
	}

	/*
	 * Manually specify cache directory, without FUSE callback
	 * This option only works when not using FUSE, since we
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2438
2439
2440
2441
2442
2443
2444









2445
2446
2447
2448
2449
2450
2451







-
-
-
-
-
-
-
-
-







	 * Tcl mode, for running raw Tcl in the same environment AppFSd would
	 * run code.
	 */
	if (argc == 2 && strcmp(argv[0], "--tcl") == 0) {
		return(appfs_tcl(argv[1]));
	}

	/*
	 * Register a signal handler for hot-restart requests
	 */
	signal_ret = signal(SIGHUP, appfs_signal_handler);
	if (signal_ret == SIG_ERR) {
		fprintf(stderr, "Unable to install signal handler for hot-restart\n");
		fprintf(stderr, "Hot-restart will not be available.\n");
	}

	/*
	 * Parse command line arguments
	 */
	/**
	 ** Restore argc/argv to original values, replacing argv[0] in case
	 ** it was moified by --cachedir option.
	 **/
2378
2379
2380
2381
2382
2383
2384
2385
2386


2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403

2404
2405

2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2471
2472
2473
2474
2475
2476
2477


2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489







2490


2491























2492
2493
2494
2495
2496
2497
2498
2499







-
-
+
+










-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-








	 */
	test_interp = appfs_create_TclInterp(&test_interp_error);
	if (test_interp == NULL) {
		if (test_interp_error == NULL) {
			test_interp_error = "Unknown error";
		}

		fprintf(stderr, "Unable to initialize Tcl interpreter for AppFSd:\n");
		fprintf(stderr, "%s\n", test_interp_error);
		APPFS_ERROR("Unable to initialize Tcl interpreter for AppFSd:");
		APPFS_ERROR("%s", test_interp_error);

		return(1);
	}

	Tcl_DeleteInterp(test_interp);

	if (appfs_threaded_tcl) {
		Tcl_FinalizeNotifier(NULL);
	}

	/*
	 * Increase resource limits for number of open files
	 * to the maximum values, since we may be asked to
	 * hold open many files on behalf of many other processes
	 */
	number_open_files.rlim_cur = number_open_files.rlim_max = RLIM_INFINITY;

	appfs_set_resource_limits();
	rlimit_ret = setrlimit(RLIMIT_NOFILE, &number_open_files);

	appfs_set_sighandler();
	if (rlimit_ret != 0) {
		rlimit_ret = getrlimit(RLIMIT_NOFILE, &number_open_files);
		if (rlimit_ret == 0) {
			number_open_files_max = number_open_files.rlim_max;

			if (number_open_files_max < (1024 * 1024)) {
				number_open_files.rlim_cur = number_open_files.rlim_max = 1024 * 1024;

				rlimit_ret = setrlimit(RLIMIT_NOFILE, &number_open_files);
			} else {
				number_open_files.rlim_cur = number_open_files.rlim_max;
			}

			rlimit_ret = setrlimit(RLIMIT_NOFILE, &number_open_files);

			if (rlimit_ret != 0 && number_open_files.rlim_cur != number_open_files_max) {
				number_open_files.rlim_cur = number_open_files.rlim_max = number_open_files_max;

				setrlimit(RLIMIT_NOFILE, &number_open_files);
			}
		}
	}


	/*
	 * Enter the FUSE main loop -- this will process any arguments
	 * and start servicing requests.
	 */
	appfs_fuse_started = 1;
	return(fuse_main(args.argc, args.argv, &appfs_operations, NULL));
}