-
Notifications
You must be signed in to change notification settings - Fork 1
/
appfsd.c
2511 lines (1934 loc) · 56.4 KB
/
appfsd.c
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
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
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
291
292
293
294
295
296
297
298
299
300
301
302
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014, 2015 Roy Keene
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* 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/fsuid.h>
#include <sys/types.h>
#include <sys/time.h>
#include <pthread.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>
#include <fuse.h>
#include <pwd.h>
#include <tcl.h>
/*
* Default cache directory
*/
#ifndef APPFS_CACHEDIR
#define APPFS_CACHEDIR "/var/cache/appfs"
#endif
/* Debugging macros */
#ifdef DEBUG
FILE *appfs_debug_fd = NULL;
#define APPFS_DEBUG(x...) { \
char buf[8192]; \
int bufoff = 0; \
char *appfs_debug_file = NULL; \
if (appfs_debug_fd == NULL) { \
appfs_debug_file = getenv("APPFS_DEBUG_FILE"); \
if (appfs_debug_file == NULL) { \
appfs_debug_file = "stderr"; \
}; \
if (strcmp(appfs_debug_file, "stderr") == 0) { \
appfs_debug_fd = stderr; \
} else { \
appfs_debug_fd = fopen(appfs_debug_file, "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); \
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);
/*
* Thread Specific Data (TSD) for Tcl Interpreter for the current thread
*/
static pthread_key_t interpKey;
/*
* Global variables, needed for all threads but only initialized before any
* FUSE threads are created
*/
const char *appfs_cachedir;
time_t appfs_boottime;
int appfs_fuse_started = 0;
int appfs_threaded_tcl;
/*
* 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;
#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
#define appfs_call_libtcl_enter /**/
#define appfs_call_libtcl_exit /**/
#endif
#define appfs_call_libtcl(x...) appfs_call_libtcl_enter x appfs_call_libtcl_exit
/*
* Global variables for AppFS Tcl Interpreter restarting
*/
int interp_reset_key = 0;
/*
* AppFS Path Type: Describes the type of path a given file is
*/
typedef enum {
APPFS_PATHTYPE_INVALID,
APPFS_PATHTYPE_DOES_NOT_EXIST,
APPFS_PATHTYPE_FILE,
APPFS_PATHTYPE_DIRECTORY,
APPFS_PATHTYPE_SYMLINK,
APPFS_PATHTYPE_SOCKET,
APPFS_PATHTYPE_FIFO,
} appfs_pathtype_t;
/*
* AppFS Path Information:
* Completely describes a specific path, how it should be returned to
* to the kernel
*/
struct appfs_pathinfo {
appfs_pathtype_t type;
time_t time;
char hostname[256];
int packaged;
unsigned long long inode;
union {
struct {
int childcount;
} dir;
struct {
int executable;
int suidRoot;
int worldaccessible;
off_t size;
} file;
struct {
off_t size;
char source[256];
} symlink;
} typeinfo;
/* Attributes used only for caching entries */
char *_cache_path;
uid_t _cache_uid;
};
/*
* Create a new Tcl interpreter and completely initialize it
*/
static Tcl_Interp *appfs_create_TclInterp(char **error_string) {
Tcl_Interp *interp;
int tcl_ret;
const char *tcl_setvar_ret;
APPFS_DEBUG("Creating new Tcl interpreter for TID = 0x%llx", (unsigned long long) pthread_self());
appfs_call_libtcl(
interp = Tcl_CreateInterp();
)
if (interp == NULL) {
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) {
APPFS_ERROR("Unable to initialize Tcl. Aborting.");
appfs_call_libtcl(
APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
)
if (error_string) {
appfs_call_libtcl(
*error_string = strdup(Tcl_GetStringResult(interp));
)
}
appfs_call_libtcl(Tcl_Release(interp);)
APPFS_DEBUG("Terminating Tcl interpreter.");
appfs_call_libtcl(Tcl_DeleteInterp(interp);)
return(NULL);
}
appfs_call_libtcl(
tcl_ret = Tcl_Eval(interp, "package ifneeded sha1 1.0 [list load {} sha1]");
)
if (tcl_ret != TCL_OK) {
APPFS_ERROR("Unable to initialize Tcl SHA1. Aborting.");
appfs_call_libtcl(
APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
)
if (error_string) {
appfs_call_libtcl(
*error_string = strdup(Tcl_GetStringResult(interp));
)
}
appfs_call_libtcl(Tcl_Release(interp);)
APPFS_DEBUG("Terminating Tcl interpreter.");
appfs_call_libtcl(Tcl_DeleteInterp(interp);)
return(NULL);
}
appfs_call_libtcl(
tcl_ret = Tcl_Eval(interp, "package ifneeded appfsd 1.0 [list load {} appfsd]");
)
if (tcl_ret != TCL_OK) {
APPFS_ERROR("Unable to initialize Tcl AppFS Package. Aborting.");
appfs_call_libtcl(
APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
)
if (error_string) {
appfs_call_libtcl(
*error_string = strdup(Tcl_GetStringResult(interp));
)
}
appfs_call_libtcl(Tcl_Release(interp);)
APPFS_DEBUG("Terminating Tcl interpreter.");
appfs_call_libtcl(Tcl_DeleteInterp(interp);)
return(NULL);
}
/*
* Load "pki.tcl" in the same way as appfsd.tcl (see below)
*/
appfs_call_libtcl_enter
tcl_ret = Tcl_Eval(interp, ""
#include "pki.tcl.h"
"");
appfs_call_libtcl_exit
if (tcl_ret != TCL_OK) {
APPFS_ERROR("Unable to initialize Tcl PKI. Aborting.");
appfs_call_libtcl(
APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
)
if (error_string) {
appfs_call_libtcl(
*error_string = strdup(Tcl_GetStringResult(interp));
)
}
appfs_call_libtcl(Tcl_Release(interp);)
APPFS_DEBUG("Terminating Tcl interpreter.");
appfs_call_libtcl(Tcl_DeleteInterp(interp);)
return(NULL);
}
/*
* Load the "appfsd.tcl" script, which is "compiled" into a C header
* so that it does not need to exist on the filesystem and can be
* directly evaluated.
*/
appfs_call_libtcl_enter
tcl_ret = Tcl_Eval(interp, ""
#include "appfsd.tcl.h"
"");
appfs_call_libtcl_exit
if (tcl_ret != TCL_OK) {
APPFS_ERROR("Unable to initialize Tcl AppFS script. Aborting.");
appfs_call_libtcl(
APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
)
if (error_string) {
appfs_call_libtcl(
*error_string = strdup(Tcl_GetStringResult(interp));
)
}
appfs_call_libtcl(Tcl_Release(interp);)
APPFS_DEBUG("Terminating Tcl interpreter.");
appfs_call_libtcl(Tcl_DeleteInterp(interp);)
return(NULL);
}
/*
* 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) {
APPFS_ERROR("Unable to set cache directory. This should never fail.");
if (error_string) {
appfs_call_libtcl(
*error_string = strdup(Tcl_GetStringResult(interp));
)
}
appfs_call_libtcl(Tcl_Release(interp);)
APPFS_DEBUG("Terminating Tcl interpreter.");
appfs_call_libtcl(Tcl_DeleteInterp(interp);)
return(NULL);
}
/*
* 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) {
APPFS_ERROR("Unable to initialize Tcl AppFS script (::appfs::init). Aborting.");
appfs_call_libtcl(
APPFS_ERROR("Tcl Error is: %s", Tcl_GetStringResult(interp));
)
if (error_string) {
appfs_call_libtcl(
*error_string = strdup(Tcl_GetStringResult(interp));
)
}
appfs_call_libtcl(Tcl_Release(interp);)
APPFS_DEBUG("Terminating Tcl interpreter.");
appfs_call_libtcl(Tcl_DeleteInterp(interp);)
return(NULL);
}
/*
* Hide some Tcl commands that we do not care to use and which may
* slow down run-time operations.
*/
appfs_call_libtcl(
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
*/
appfs_call_libtcl(Tcl_Release(interp);)
/*
* Return the completely initialized interpreter
*/
return(interp);
}
/*
* Return the thread-specific Tcl interpreter, creating it if needed
*/
static Tcl_Interp *appfs_TclInterp(void) {
Tcl_Interp *interp;
int pthread_ret;
static __thread int thread_interp_reset_key = 0;
int global_interp_reset_key;
global_interp_reset_key = __sync_fetch_and_add(&interp_reset_key, 0);
interp = pthread_getspecific(interpKey);
if (interp != NULL && thread_interp_reset_key != global_interp_reset_key) {
APPFS_DEBUG("Terminating old interpreter and restarting due to reset request.");
appfs_call_libtcl(Tcl_DeleteInterp(interp);)
interp = NULL;
pthread_ret = pthread_setspecific(interpKey, interp);
}
if (global_interp_reset_key == -1) {
APPFS_DEBUG("Returning NULL since we are in the process of terminating all threads.");
return(NULL);
}
thread_interp_reset_key = global_interp_reset_key;
if (interp == NULL) {
interp = appfs_create_TclInterp(NULL);
if (interp == NULL) {
APPFS_DEBUG("Create interp failed, returning in failure.");
return(NULL);
}
pthread_ret = pthread_setspecific(interpKey, interp);
if (pthread_ret != 0) {
APPFS_DEBUG("pthread_setspecific() failed. Terminating Tcl interpreter.");
appfs_call_libtcl(Tcl_DeleteInterp(interp);)
return(NULL);
}
}
return(interp);
}
/*
* Evaluate a Tcl script constructed by concatenating a bunch of C strings
* together.
*/
static int appfs_Tcl_Eval(Tcl_Interp *interp, int objc, const char *cmd, ...) {
Tcl_Obj **objv;
const char *arg;
va_list argp;
int retval;
int i;
if (interp == NULL) {
APPFS_DEBUG("Invalid interpreter passed in, returning in failure.");
return(TCL_ERROR);
}
objv = (void *) ckalloc(sizeof(*objv) * objc);
appfs_call_libtcl(
objv[0] = Tcl_NewStringObj(cmd, -1);
Tcl_IncrRefCount(objv[0]);
va_start(argp, cmd);
for (i = 1; i < objc; i++) {
arg = va_arg(argp, const char *);
objv[i] = Tcl_NewStringObj(arg, -1);
Tcl_IncrRefCount(objv[i]);
}
va_end(argp);
)
appfs_call_libtcl(
retval = Tcl_EvalObjv(interp, objc, objv, 0);
)
appfs_call_libtcl(
for (i = 0; i < objc; i++) {
Tcl_DecrRefCount(objv[i]);
}
)
ckfree((void *) objv);
if (retval != TCL_OK) {
appfs_call_libtcl(
APPFS_DEBUG("Tcl command failed, ::errorInfo contains: %s\n", Tcl_GetVar(interp, "::errorInfo", 0));
)
}
return(retval);
}
/*
* Request all Tcl interpreters restart
*/
static void appfs_tcl_ResetInterps(void) {
APPFS_DEBUG("Requesting reset of all interpreters.");
__sync_add_and_fetch(&interp_reset_key, 1);
return;
}
/*
* Determine the UID for the user making the current FUSE filesystem request.
* This will be used to lookup the user's home directory so we can search for
* locally modified files.
*/
static uid_t appfs_get_fsuid(void) {
struct fuse_context *ctx;
if (!appfs_fuse_started) {
return(getuid());
}
ctx = fuse_get_context();
if (ctx == NULL) {
/* Unable to lookup user for some reason */
/* Return an unprivileged user ID */
APPFS_DEBUG("Unable to lookup user for some reason, returninng user ID of 1");
return(1);
}
return(ctx->uid);
}
/*
* Determine the GID for the user making the current FUSE filesystem request.
*/
static gid_t appfs_get_fsgid(void) {
struct fuse_context *ctx;
if (!appfs_fuse_started) {
return(getgid());
}
ctx = fuse_get_context();
if (ctx == NULL) {
/* Unable to lookup user for some reason */
/* Return an unprivileged user ID */
APPFS_DEBUG("Unable to lookup group for some reason, returninng group ID of 1");
return(1);
}
return(ctx->gid);
}
static void appfs_simulate_user_fs_enter(void) {
setfsuid(appfs_get_fsuid());
setfsgid(appfs_get_fsgid());
}
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) {
struct passwd entry, *result;
struct stat stbuf;
char buf[1024], *retval;
int gpu_ret, stat_ret;
gpu_ret = getpwuid_r(fsuid, &entry, buf, sizeof(buf), &result);
if (gpu_ret != 0) {
APPFS_DEBUG("getpwuid_r(%llu, ...) returned in failure", (unsigned long long) fsuid);
return(NULL);
}
if (result == NULL) {
APPFS_DEBUG("getpwuid_r(%llu, ...) returned NULL result", (unsigned long long) fsuid);
return(NULL);
}
if (result->pw_dir == NULL) {
APPFS_DEBUG("getpwuid_r(%llu, ...) returned NULL home directory", (unsigned long long) fsuid);
return(NULL);
}
stat_ret = stat(result->pw_dir, &stbuf);
if (stat_ret != 0) {
APPFS_DEBUG("stat(%s) returned in failure", result->pw_dir);
return(NULL);
}
if (stbuf.st_uid != fsuid) {
APPFS_DEBUG("UID mis-match on user %llu's home directory (%s). It's owned by %llu.",
(unsigned long long) fsuid,
result->pw_dir,
(unsigned long long) stbuf.st_uid
);
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
*/
#if UINT_MAX < 4294967295
#error Integer size is too small
#endif
static unsigned long long appfs_get_path_inode(const char *path, int uid) {
unsigned int retval;
const unsigned char *p;
retval = 2166136261U; /* FNV-1a 32-bit offset_basis */
for (p = (unsigned char *) path; *p; p++) {
retval ^= (int) *p;
#if 0
retval *= 16777619; /* FNV-1a 32-bit prime */
#else
/* GCC Optimized replacement */
retval += (retval << 1) + (retval << 4) + (retval << 7) + (retval << 8) + (retval << 24);
#endif
}
if (uid >= 0) {
retval += uid;
retval++;
}
APPFS_DEBUG("Looked up inode number for path=%s,uid=%i: %u", path, uid, retval);
return(retval);
}
/*
* Cache Get Path Info lookups for speed
*/
static int appfs_get_path_info_cache_get(const char *path, uid_t uid, struct appfs_pathinfo *pathinfo) {
unsigned int hash_idx;
int pthread_ret;
int retval;
retval = 1;
pthread_ret = pthread_mutex_lock(&appfs_path_info_cache_mutex);
if (pthread_ret != 0) {
APPFS_DEBUG("Unable to lock path_info cache mutex !");
return(-1);
}
APPFS_DEBUG("Looking up cache entry for path=%s,uid=%lli...", path, (long long) uid);
if (appfs_path_info_cache != NULL) {
hash_idx = (appfs_get_path_inode(path, uid)) % appfs_path_info_cache_size;
if (appfs_path_info_cache[hash_idx]._cache_path != NULL) {
if (strcmp(appfs_path_info_cache[hash_idx]._cache_path, path) == 0 && appfs_path_info_cache[hash_idx]._cache_uid == uid) {
retval = 0;
memcpy(pathinfo, &appfs_path_info_cache[hash_idx], sizeof(*pathinfo));
pathinfo->_cache_path = NULL;
}
}
}
pthread_ret = pthread_mutex_unlock(&appfs_path_info_cache_mutex);
if (pthread_ret != 0) {
APPFS_DEBUG("Unable to unlock path_info cache mutex !");
return(-1);
}
if (retval == 0) {
APPFS_DEBUG("Cache hit on path=%s,uid=%lli", path, (long long) uid);
} else {
APPFS_DEBUG("Cache miss on path=%s,uid=%lli", path, (long long) uid);
}
return(retval);
}
static void appfs_get_path_info_cache_add(const char *path, uid_t uid, struct appfs_pathinfo *pathinfo) {
unsigned int hash_idx;
int pthread_ret;
pthread_ret = pthread_mutex_lock(&appfs_path_info_cache_mutex);
if (pthread_ret != 0) {
APPFS_DEBUG("Unable to lock path_info cache mutex !");
return;
}
if (appfs_path_info_cache == NULL) {
appfs_path_info_cache = calloc(appfs_path_info_cache_size, sizeof(*appfs_path_info_cache));
}
hash_idx = (appfs_get_path_inode(path, uid)) % appfs_path_info_cache_size;
if (appfs_path_info_cache[hash_idx]._cache_path != NULL) {
free(appfs_path_info_cache[hash_idx]._cache_path);
}
memcpy(&appfs_path_info_cache[hash_idx], pathinfo, sizeof(*pathinfo));
appfs_path_info_cache[hash_idx]._cache_path = strdup(path);
appfs_path_info_cache[hash_idx]._cache_uid = uid;
pthread_ret = pthread_mutex_unlock(&appfs_path_info_cache_mutex);
if (pthread_ret != 0) {
APPFS_DEBUG("Unable to unlock path_info cache mutex !");
return;
}
return;
}
static void appfs_get_path_info_cache_rm(const char *path, uid_t uid) {
unsigned int hash_idx;
int pthread_ret;
pthread_ret = pthread_mutex_lock(&appfs_path_info_cache_mutex);
if (pthread_ret != 0) {
APPFS_DEBUG("Unable to lock path_info cache mutex !");
return;
}
if (appfs_path_info_cache != NULL) {
hash_idx = (appfs_get_path_inode(path, uid)) % appfs_path_info_cache_size;
if (appfs_path_info_cache[hash_idx]._cache_path != NULL) {
free(appfs_path_info_cache[hash_idx]._cache_path);
appfs_path_info_cache[hash_idx]._cache_path = NULL;
}
}
pthread_ret = pthread_mutex_unlock(&appfs_path_info_cache_mutex);
if (pthread_ret != 0) {
APPFS_DEBUG("Unable to unlock path_info cache mutex !");
return;
}
return;
}
static void appfs_get_path_info_cache_flush(uid_t uid, int new_size) {
unsigned int idx;
int pthread_ret;
APPFS_DEBUG("Flushing AppFS cache (uid = %lli, new_size = %i)", (long long) uid, new_size);
pthread_ret = pthread_mutex_lock(&appfs_path_info_cache_mutex);
if (pthread_ret != 0) {
APPFS_DEBUG("Unable to lock path_info cache mutex !");
return;
}
if (appfs_path_info_cache != NULL) {
for (idx = 0; idx < appfs_path_info_cache_size; idx++) {
if (appfs_path_info_cache[idx]._cache_path != NULL) {
if (uid != ((uid_t) -1)) {
if (appfs_path_info_cache[idx]._cache_uid != uid) {
continue;
}
}
free(appfs_path_info_cache[idx]._cache_path);
appfs_path_info_cache[idx]._cache_path = NULL;
}
}
}
if (uid == ((uid_t) -1)) {
free(appfs_path_info_cache);
appfs_path_info_cache = NULL;
if (new_size != -1) {
appfs_path_info_cache_size = new_size;
}
}
pthread_ret = pthread_mutex_unlock(&appfs_path_info_cache_mutex);
if (pthread_ret != 0) {
APPFS_DEBUG("Unable to unlock path_info cache mutex !");
return;
}
return;
}
/* Get information about a path, and optionally list children */
static int appfs_get_path_info(const char *path, struct appfs_pathinfo *pathinfo) {
Tcl_Interp *interp;
Tcl_Obj *attrs_dict, *attr_value;
const char *attr_value_str, *attr_value_str_i;
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;
int retval;
uid_t fsuid;
retval = 0;
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) {
APPFS_DEBUG("Returning from cache: does not exist \"%s\"", path);
return(-ENOENT);
}
if (pathinfo->type == APPFS_PATHTYPE_INVALID) {
APPFS_DEBUG("Returning from cache: invalid object \"%s\"", path);
return(-EIO);
}
return(0);
}
interp = appfs_TclInterp();
if (interp == NULL) {
APPFS_DEBUG("error: Unable to get an interpreter");
return(-EIO);
}
appfs_call_libtcl(Tcl_Preserve(interp);)
tcl_ret = appfs_Tcl_Eval(interp, 2, "::appfs::getattr", path);
if (tcl_ret != TCL_OK) {
APPFS_DEBUG("::appfs::getattr(%s) failed.", path);
appfs_call_libtcl(
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);
appfs_call_libtcl(Tcl_Release(interp);)
return(-ENOENT);
}
if (attr_key_type == NULL) {
appfs_call_libtcl(
attr_key_type = Tcl_NewStringObj("type", -1);
attr_key_perms = Tcl_NewStringObj("perms", -1);
attr_key_size = Tcl_NewStringObj("size", -1);
attr_key_time = Tcl_NewStringObj("time", -1);
attr_key_source = Tcl_NewStringObj("source", -1);
attr_key_childcount = Tcl_NewStringObj("childcount", -1);
attr_key_packaged = Tcl_NewStringObj("packaged", -1);
Tcl_IncrRefCount(attr_key_type);
Tcl_IncrRefCount(attr_key_perms);
Tcl_IncrRefCount(attr_key_size);
Tcl_IncrRefCount(attr_key_time);
Tcl_IncrRefCount(attr_key_source);
Tcl_IncrRefCount(attr_key_childcount);
Tcl_IncrRefCount(attr_key_packaged);
)
}
appfs_call_libtcl(
attrs_dict = Tcl_GetObjResult(interp);
tcl_ret = Tcl_DictObjGet(interp, attrs_dict, attr_key_type, &attr_value);
)
if (tcl_ret != TCL_OK) {
APPFS_DEBUG("[dict get \"type\"] failed");
appfs_call_libtcl(
APPFS_DEBUG("Tcl Error is: %s", Tcl_GetStringResult(interp));
)
appfs_call_libtcl(Tcl_Release(interp);)
return(-EIO);
}
if (attr_value == NULL) {
APPFS_DEBUG("error: Unable to get type for \"%s\" from Tcl", path);
appfs_call_libtcl(Tcl_Release(interp);)
return(-EIO);
}
pathinfo->packaged = 0;
appfs_call_libtcl(
attr_value_str = Tcl_GetString(attr_value);
switch (attr_value_str[0]) {
case 'd': /* directory */
pathinfo->type = APPFS_PATHTYPE_DIRECTORY;
pathinfo->typeinfo.dir.childcount = 0;
Tcl_DictObjGet(interp, attrs_dict, attr_key_childcount, &attr_value);
if (attr_value != NULL) {
tcl_ret = Tcl_GetWideIntFromObj(NULL, attr_value, &attr_value_wide);
if (tcl_ret == TCL_OK) {
pathinfo->typeinfo.dir.childcount = attr_value_wide;
}
}
break;
case 'f': /* file */
pathinfo->type = APPFS_PATHTYPE_FILE;
pathinfo->typeinfo.file.size = 0;
pathinfo->typeinfo.file.executable = 0;
pathinfo->typeinfo.file.suidRoot = 0;
pathinfo->typeinfo.file.worldaccessible = 0;
Tcl_DictObjGet(interp, attrs_dict, attr_key_size, &attr_value);
if (attr_value != NULL) {
tcl_ret = Tcl_GetWideIntFromObj(NULL, attr_value, &attr_value_wide);
if (tcl_ret == TCL_OK) {
pathinfo->typeinfo.file.size = attr_value_wide;
}
}
Tcl_DictObjGet(interp, attrs_dict, attr_key_perms, &attr_value);
if (attr_value != NULL) {
attr_value_str = Tcl_GetString(attr_value);
for (attr_value_str_i = &attr_value_str[0]; *attr_value_str_i != '\0'; attr_value_str_i++) {
switch (*attr_value_str_i) {
case 'x':
pathinfo->typeinfo.file.executable = 1;
break;
case 'U':
pathinfo->typeinfo.file.suidRoot = 1;
break;
case '-':
pathinfo->typeinfo.file.worldaccessible = 1;
break;
}
}
}
break;
case 's': /* symlink */
pathinfo->type = APPFS_PATHTYPE_SYMLINK;
pathinfo->typeinfo.symlink.size = 0;
pathinfo->typeinfo.symlink.source[0] = '\0';