1004388ebScasper /*
2*159d09a2SMark Phalan  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3004388ebScasper  * Use is subject to license terms.
4004388ebScasper  */
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate  * prof_file.c ---- routines that manipulate an individual profile file.
77c478bd9Sstevel@tonic-gate  */
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate #include <autoconf.h>
10505d05c7Sgtb #include "prof_int.h"
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include <stdio.h>
137c478bd9Sstevel@tonic-gate #ifdef HAVE_STDLIB_H
147c478bd9Sstevel@tonic-gate #include <stdlib.h>
157c478bd9Sstevel@tonic-gate #endif
167c478bd9Sstevel@tonic-gate #ifdef HAVE_UNISTD_H
177c478bd9Sstevel@tonic-gate #include <unistd.h>
187c478bd9Sstevel@tonic-gate #endif
197c478bd9Sstevel@tonic-gate #include <string.h>
20505d05c7Sgtb #include <stddef.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <sys/types.h>
237c478bd9Sstevel@tonic-gate #include <sys/stat.h>
247c478bd9Sstevel@tonic-gate #include <errno.h>
257c478bd9Sstevel@tonic-gate 
26505d05c7Sgtb #ifdef HAVE_PWD_H
27505d05c7Sgtb #include <pwd.h>
28505d05c7Sgtb #endif
297c478bd9Sstevel@tonic-gate 
30505d05c7Sgtb #if defined(_WIN32)
317c478bd9Sstevel@tonic-gate #include <io.h>
327c478bd9Sstevel@tonic-gate #define HAVE_STAT
337c478bd9Sstevel@tonic-gate #define stat _stat
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
36505d05c7Sgtb #include "k5-platform.h"
37505d05c7Sgtb 
38505d05c7Sgtb struct global_shared_profile_data {
39505d05c7Sgtb 	/* This is the head of the global list of shared trees */
40505d05c7Sgtb 	prf_data_t trees;
41505d05c7Sgtb 	/* Lock for above list.  */
42505d05c7Sgtb 	k5_mutex_t mutex;
43505d05c7Sgtb };
44505d05c7Sgtb #define g_shared_trees		(krb5int_profile_shared_data.trees)
45505d05c7Sgtb #define g_shared_trees_mutex	(krb5int_profile_shared_data.mutex)
46505d05c7Sgtb 
47505d05c7Sgtb static struct global_shared_profile_data krb5int_profile_shared_data = {
48505d05c7Sgtb     0,
49505d05c7Sgtb     K5_MUTEX_PARTIAL_INITIALIZER
50505d05c7Sgtb };
51505d05c7Sgtb 
52505d05c7Sgtb MAKE_INIT_FUNCTION(profile_library_initializer);
53505d05c7Sgtb MAKE_FINI_FUNCTION(profile_library_finalizer);
54505d05c7Sgtb 
profile_library_initializer(void)55505d05c7Sgtb int profile_library_initializer(void)
56505d05c7Sgtb {
57*159d09a2SMark Phalan #ifdef SHOW_INITFINI_FUNCS
58*159d09a2SMark Phalan     printf("profile_library_initializer\n");
59*159d09a2SMark Phalan #endif
60505d05c7Sgtb #if !USE_BUNDLE_ERROR_STRINGS
61505d05c7Sgtb     add_error_table(&et_prof_error_table);
62505d05c7Sgtb #endif
63505d05c7Sgtb     return k5_mutex_finish_init(&g_shared_trees_mutex);
64505d05c7Sgtb }
profile_library_finalizer(void)65505d05c7Sgtb void profile_library_finalizer(void)
66505d05c7Sgtb {
67*159d09a2SMark Phalan     if (! INITIALIZER_RAN(profile_library_initializer) || PROGRAM_EXITING()) {
68*159d09a2SMark Phalan #ifdef SHOW_INITFINI_FUNCS
69*159d09a2SMark Phalan 	printf("profile_library_finalizer: skipping\n");
70*159d09a2SMark Phalan #endif
71505d05c7Sgtb 	return;
72*159d09a2SMark Phalan     }
73*159d09a2SMark Phalan #ifdef SHOW_INITFINI_FUNCS
74*159d09a2SMark Phalan     printf("profile_library_finalizer\n");
75*159d09a2SMark Phalan #endif
76505d05c7Sgtb     k5_mutex_destroy(&g_shared_trees_mutex);
77505d05c7Sgtb #if !USE_BUNDLE_ERROR_STRINGS
78505d05c7Sgtb     remove_error_table(&et_prof_error_table);
79505d05c7Sgtb #endif
80505d05c7Sgtb }
81505d05c7Sgtb 
82505d05c7Sgtb static void profile_free_file_data(prf_data_t);
83505d05c7Sgtb 
84505d05c7Sgtb #if 0
85505d05c7Sgtb 
86505d05c7Sgtb #define scan_shared_trees_locked()				\
87505d05c7Sgtb 	{							\
88505d05c7Sgtb 	    prf_data_t d;					\
89505d05c7Sgtb 	    k5_mutex_assert_locked(&g_shared_trees_mutex);	\
90505d05c7Sgtb 	    for (d = g_shared_trees; d; d = d->next) {		\
91505d05c7Sgtb 		assert(d->magic == PROF_MAGIC_FILE_DATA);	\
92505d05c7Sgtb 		assert((d->flags & PROFILE_FILE_SHARED) != 0);	\
93505d05c7Sgtb 		assert(d->filespec[0] != 0);			\
94505d05c7Sgtb 		assert(d->fslen <= 1000); /* XXX */		\
95505d05c7Sgtb 		assert(d->filespec[d->fslen] == 0);		\
96505d05c7Sgtb 		assert(d->fslen = strlen(d->filespec));		\
97*159d09a2SMark Phalan 		assert(d->root != NULL);			\
98505d05c7Sgtb 	    }							\
99505d05c7Sgtb 	}
100505d05c7Sgtb 
101505d05c7Sgtb #define scan_shared_trees_unlocked()			\
102