Check-in [770584ac09]
Overview
Comment:Tweaked hashing algorithm, added destructor for threads so that as threads terminate they are cleaned up
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 770584ac09a823c45b6bd0db3a097e8dc959045c
User & Date: rkeene on 2014-11-14 04:05:14
Other Links: manifest | tags
Context
2014-11-14
04:05
Updated to have a release and debug CFLAGS check-in: 9cc13bbc4c user: rkeene tags: trunk
04:05
Tweaked hashing algorithm, added destructor for threads so that as threads terminate they are cleaned up check-in: 770584ac09 user: rkeene tags: trunk
2014-11-12
15:23
Updated to fetch/update host index before fetching manifest check-in: d6262bea99 user: rkeene tags: trunk
Changes

Modified appfsd.c from [e49bb6021f] to [6216c87bde].

1
2
3
4
5
6

7
8
9
10
11
12
13
#define FUSE_USE_VERSION 26

#include <sys/fsuid.h>
#include <sys/types.h>
#include <pthread.h>
#include <signal.h>

#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>






>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define FUSE_USE_VERSION 26

#include <sys/fsuid.h>
#include <sys/types.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>
#include <stdio.h>
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

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

		return(NULL);
	}



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

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





		Tcl_DeleteInterp(interp);

		return(NULL);
	}

	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");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));

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





		Tcl_DeleteInterp(interp);

		return(NULL);
	}

	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");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));

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





		Tcl_DeleteInterp(interp);

		return(NULL);
	}

	/*
	 * Load "pki.tcl" in the same way as appfsd.tcl (see below)
	 */
	tcl_ret = Tcl_Eval(interp, ""
#include "pki.tcl.h"
	"");
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl PKI.  Aborting.\n");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));

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





		Tcl_DeleteInterp(interp);

		return(NULL);
	}

	/*
	 * Load the "appfsd.tcl" script, which is "compiled" into a C header







>
>










>
>
>
>














>
>
>
>














>
>
>
>



















>
>
>
>







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

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

		return(NULL);
	}

	Tcl_Preserve(interp);

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

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

		Tcl_Release(interp);

		APPFS_DEBUG("Terminating Tcl interpreter.");

		Tcl_DeleteInterp(interp);

		return(NULL);
	}

	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");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));

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

		Tcl_Release(interp);

		APPFS_DEBUG("Terminating Tcl interpreter.");

		Tcl_DeleteInterp(interp);

		return(NULL);
	}

	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");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));

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

		Tcl_Release(interp);

		APPFS_DEBUG("Terminating Tcl interpreter.");

		Tcl_DeleteInterp(interp);

		return(NULL);
	}

	/*
	 * Load "pki.tcl" in the same way as appfsd.tcl (see below)
	 */
	tcl_ret = Tcl_Eval(interp, ""
#include "pki.tcl.h"
	"");
	if (tcl_ret != TCL_OK) {
		fprintf(stderr, "Unable to initialize Tcl PKI.  Aborting.\n");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));

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

		Tcl_Release(interp);

		APPFS_DEBUG("Terminating Tcl interpreter.");

		Tcl_DeleteInterp(interp);

		return(NULL);
	}

	/*
	 * Load the "appfsd.tcl" script, which is "compiled" into a C header
195
196
197
198
199
200
201




202
203
204
205
206
207
208
209
210
211
212
213
214
215
216




217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234




235
236
237
238
239
240
241
242
243
244
245







246
247
248
249
250
251
252
		fprintf(stderr, "Unable to initialize Tcl AppFS script.  Aborting.\n");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));

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





		Tcl_DeleteInterp(interp);

		return(NULL);
	}

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

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





		Tcl_DeleteInterp(interp);

		return(NULL);
	}

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

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





		Tcl_DeleteInterp(interp);

		return(NULL);
	}

	/*
	 * Hide some Tcl commands that we do not care to use and which may
	 * slow down run-time operations.
	 */
	Tcl_HideCommand(interp, "auto_load_index", "auto_load_index");
	Tcl_HideCommand(interp, "unknown", "unknown");








	/*
	 * Return the completely initialized interpreter
	 */
	return(interp);
}








>
>
>
>















>
>
>
>


















>
>
>
>











>
>
>
>
>
>
>







214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
		fprintf(stderr, "Unable to initialize Tcl AppFS script.  Aborting.\n");
		fprintf(stderr, "Tcl Error is: %s\n", Tcl_GetStringResult(interp));

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

		Tcl_Release(interp);

		APPFS_DEBUG("Terminating Tcl interpreter.");

		Tcl_DeleteInterp(interp);

		return(NULL);
	}

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

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

		Tcl_Release(interp);

		APPFS_DEBUG("Terminating Tcl interpreter.");

		Tcl_DeleteInterp(interp);

		return(NULL);
	}

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

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

		Tcl_Release(interp);

		APPFS_DEBUG("Terminating Tcl interpreter.");

		Tcl_DeleteInterp(interp);

		return(NULL);
	}

	/*
	 * Hide some Tcl commands that we do not care to use and which may
	 * slow down run-time operations.
	 */
	Tcl_HideCommand(interp, "auto_load_index", "auto_load_index");
	Tcl_HideCommand(interp, "unknown", "unknown");
	Tcl_HideCommand(interp, "exit", "exit");

	/*
	 * Release the hold we have on the interpreter so that it may be
	 * deleted if needed
	 */
	Tcl_Release(interp);

	/*
	 * Return the completely initialized interpreter
	 */
	return(interp);
}

265
266
267
268
269
270
271
272
273


274
275
276
277
278
279
280
281
282
283


284
285
286
287
288
289
290
	if (interp != NULL && thread_interp_reset_key != global_interp_reset_key) {
		APPFS_DEBUG("Terminating old interpreter and restarting due to reset request.");

		Tcl_DeleteInterp(interp);

		interp = NULL;

		thread_interp_reset_key = global_interp_reset_key;
	}



	if (interp == NULL) {
		interp = appfs_create_TclInterp(NULL);

		if (interp == NULL) {
			return(NULL);
		}

		pthread_ret = pthread_setspecific(interpKey, interp);
		if (pthread_ret != 0) {


			Tcl_DeleteInterp(interp);

			return(NULL);
		}
	}

	return(interp);







|

>
>










>
>







303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
	if (interp != NULL && thread_interp_reset_key != global_interp_reset_key) {
		APPFS_DEBUG("Terminating old interpreter and restarting due to reset request.");

		Tcl_DeleteInterp(interp);

		interp = NULL;

		pthread_ret = pthread_setspecific(interpKey, interp);
	}

	thread_interp_reset_key = global_interp_reset_key;

	if (interp == NULL) {
		interp = appfs_create_TclInterp(NULL);

		if (interp == NULL) {
			return(NULL);
		}

		pthread_ret = pthread_setspecific(interpKey, interp);
		if (pthread_ret != 0) {
			APPFS_DEBUG("pthread_setspecific() failed.  Terminating Tcl interpreter.");

			Tcl_DeleteInterp(interp);

			return(NULL);
		}
	}

	return(interp);
451
452
453
454
455
456
457



458
459
460
461
462
463
464
465
466
467
468
469
470
471

472
473
474
475
476
477
478
}

/*
 * 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
 */



static long long appfs_get_path_inode(const char *path) {
	long long retval;
	const char *p;

	retval = 10;

	for (p = path; *p; p++) {
		retval %= 4290960290ULL;
		retval += *p;
		retval <<= 7;
	}

	retval += 10;
	retval %= 4294967296ULL;


	return(retval);
}

/*
 * Cache Get Path Info lookups for speed
 */







>
>
>

|







|



|
>







493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
}

/*
 * 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
 */
#if UINT_MAX < 4294967295
#error Integer size is too small 
#endif
static long long appfs_get_path_inode(const char *path) {
	int retval;
	const char *p;

	retval = 10;

	for (p = path; *p; p++) {
		retval %= 4290960290ULL;
		retval += *p;
		retval <<= 6;
	}

	retval += 10;
	retval %= 4294967286ULL;
	retval += 10;

	return(retval);
}

/*
 * Cache Get Path Info lookups for speed
 */
640
641
642
643
644
645
646

647


648
649
650
651
652
653
654
655
	Tcl_Obj *attrs_dict, *attr_value;
	const char *attr_value_str;
	Tcl_WideInt attr_value_wide;
	int attr_value_int;
	static __thread Tcl_Obj *attr_key_type = NULL, *attr_key_perms = NULL, *attr_key_size = NULL, *attr_key_time = NULL, *attr_key_source = NULL, *attr_key_childcount = NULL, *attr_key_packaged = NULL;
	int cache_ret;
	int tcl_ret;




	cache_ret = appfs_get_path_info_cache_get(path, appfs_get_fsuid(), pathinfo);
	if (cache_ret == 0) {
		if (pathinfo->type == APPFS_PATHTYPE_DOES_NOT_EXIST) {
			return(-ENOENT);
		}

		if (pathinfo->type == APPFS_PATHTYPE_INVALID) {
			return(-EIO);







>

>
>
|







686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
	Tcl_Obj *attrs_dict, *attr_value;
	const char *attr_value_str;
	Tcl_WideInt attr_value_wide;
	int attr_value_int;
	static __thread Tcl_Obj *attr_key_type = NULL, *attr_key_perms = NULL, *attr_key_size = NULL, *attr_key_time = NULL, *attr_key_source = NULL, *attr_key_childcount = NULL, *attr_key_packaged = NULL;
	int cache_ret;
	int tcl_ret;
	uid_t fsuid;

	fsuid = appfs_get_fsuid();

	cache_ret = appfs_get_path_info_cache_get(path, fsuid, pathinfo);
	if (cache_ret == 0) {
		if (pathinfo->type == APPFS_PATHTYPE_DOES_NOT_EXIST) {
			return(-ENOENT);
		}

		if (pathinfo->type == APPFS_PATHTYPE_INVALID) {
			return(-EIO);
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
	tcl_ret = appfs_Tcl_Eval(interp, 2, "::appfs::getattr", path);
	if (tcl_ret != TCL_OK) {
		APPFS_DEBUG("::appfs::getattr(%s) failed.", path);
		APPFS_DEBUG("Tcl Error is: %s", Tcl_GetStringResult(interp));

		pathinfo->type = APPFS_PATHTYPE_DOES_NOT_EXIST;

		appfs_get_path_info_cache_add(path, appfs_get_fsuid(), pathinfo);

		Tcl_Release(interp);

		return(-ENOENT);
	}

	if (attr_key_type == NULL) {







|







717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
	tcl_ret = appfs_Tcl_Eval(interp, 2, "::appfs::getattr", path);
	if (tcl_ret != TCL_OK) {
		APPFS_DEBUG("::appfs::getattr(%s) failed.", path);
		APPFS_DEBUG("Tcl Error is: %s", Tcl_GetStringResult(interp));

		pathinfo->type = APPFS_PATHTYPE_DOES_NOT_EXIST;

		appfs_get_path_info_cache_add(path, fsuid, pathinfo);

		Tcl_Release(interp);

		return(-ENOENT);
	}

	if (attr_key_type == NULL) {
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
		}
	} else {
		pathinfo->time = 0;
	}

	Tcl_Release(interp);

	appfs_get_path_info_cache_add(path, appfs_get_fsuid(), pathinfo);

	return(0);
}

static char *appfs_prepare_to_create(const char *path) {
	Tcl_Interp *interp;
	const char *real_path;







|







836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
		}
	} else {
		pathinfo->time = 0;
	}

	Tcl_Release(interp);

	appfs_get_path_info_cache_add(path, fsuid, pathinfo);

	return(0);
}

static char *appfs_prepare_to_create(const char *path) {
	Tcl_Interp *interp;
	const char *real_path;
1541
1542
1543
1544
1545
1546
1547























1548
1549
1550
1551
1552
1553
1554
	/* Request to perform a "hot" restart */
	if (sig == SIGHUP) {
		appfs_hot_restart();
	}

	return;
}
























/*
 * FUSE operations structure
 */
static struct fuse_operations appfs_operations = {
	.getattr   = appfs_fuse_getattr,
	.readdir   = appfs_fuse_readdir,







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
	/* Request to perform a "hot" restart */
	if (sig == SIGHUP) {
		appfs_hot_restart();
	}

	return;
}

/*
 * Terminate a thread
 */
static void appfs_terminate_interp(void *_interp) {
	Tcl_Interp *interp;

	APPFS_DEBUG("Called: _interp = %p", _interp);

	if (_interp == NULL) {
		APPFS_DEBUG("Terminating thread with no interpreter");

		return;
	}

	interp = _interp;

	APPFS_DEBUG("Terminating interpreter due to thread termination");

	Tcl_DeleteInterp(interp);

	return;
}

/*
 * FUSE operations structure
 */
static struct fuse_operations appfs_operations = {
	.getattr   = appfs_fuse_getattr,
	.readdir   = appfs_fuse_readdir,
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
	Tcl_StaticPackage(NULL, "appfsd", Appfsd_Init, NULL);

	/*
	 * 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, NULL);
	if (pthread_ret != 0) {
		fprintf(stderr, "Unable to create TSD key for Tcl.  Aborting.\n");

		return(1);
	}

	/*







|







1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
	Tcl_StaticPackage(NULL, "appfsd", Appfsd_Init, NULL);

	/*
	 * 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);
	if (pthread_ret != 0) {
		fprintf(stderr, "Unable to create TSD key for Tcl.  Aborting.\n");

		return(1);
	}

	/*