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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* The primary role of this file is to obtain a list of manifests that are
* located in a specified directory or one of its subdirectories. The
* find_manifests() function provides this service, and
* free_manifest_array() is used to free the memory associated with the
* returned list.
*
* The find_manifests() function can return an array consisting of all the
* .xml files in the directory and its subdirectories. Alternatively,
* find_manifests() can be asked to only return new manifests based on the
* return of mhash_test_file(). The list that is returned is an array of
* pointers to manifest_info structures.
*
* Implementation Notes:
* ====================
* This module makes use of the nftw(3C) function to scan the directory.
* nftw() calls a processing function for every file that it finds.
* Unfortunately, nftw does not allow us to pass in any structure pointers
* to the processing function, and that makes it hard to accumulate a list.
* Thus, we will use the thread specific data area to hold data that must
* be retained between calls to the processing function. This will allow
* this module to be used in multi-threaded applications if the need
* arises.
*/
#include <assert.h>
#include <errno.h>
#include <ftw.h>
#include <libscf.h>
#include <libuutil.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "manifest_find.h"
#include "manifest_hash.h"
#define MAX_DEPTH 24
/* Thread specific data */
typedef struct mftsd {
manifest_info_t ** tsd_array; /* Array of manifest_info structs */
int tsd_count; /* Number items in list */
int tsd_max; /* Number of pointers allocated */
/* at tsd_array. */
int tsd_flags; /* Check flags for hash and extension */
scf_handle_t *tsd_hndl; /* Handle for libscf. */
} mftsd_t;
static pthread_key_t tsd_key = PTHREAD_ONCE_KEY_NP;
/*
* Add the manifest info consisting of filename (fn), hash property name
* (pname) and hash to the array at tsd_array. If necessary, realloc()
* will be called to increase the size of the buffer at tsd_array.
*
* Returns 0 on success and -1 on failure. If a failure occurs, errno will
* be set.
*/
static int
add_pointer(mftsd_t *tsdp, const char *fn, const char *pname, uchar_t *hash)
{
manifest_info_t *info;
manifest_info_t **newblock;
int new_max;
if (tsdp->tsd_count >= (tsdp->tsd_max - 1)) {
/* Need more memory. */
new_max = (tsdp->tsd_max == 0) ? 16 : 2 * tsdp->tsd_max;
newblock = realloc(tsdp->tsd_array,
new_max * sizeof (*tsdp->tsd_array));
if (newblock == NULL)
return (-1);
tsdp->tsd_array = newblock;
/* NULL terminate list in case allocations fail below. */
*(tsdp->tsd_array + tsdp->tsd_count) = NULL;
tsdp->tsd_max = new_max;
}
info = uu_zalloc(sizeof (*info));
if (info == NULL) {
errno = ENOMEM;
return (-1);
}
info->mi_path = uu_strdup(fn);
if (info->mi_path == NULL) {
uu_free(info);
errno = ENOMEM;
return (-1);
}
info->mi_prop = pname;
if (hash != NULL)
(void) memcpy(info->mi_hash, hash, MHASH_SIZE);
*(tsdp->tsd_array + tsdp->tsd_count) = info;
tsdp->tsd_count++;
/* NULL terminate the list. */
*(tsdp->tsd_array + tsdp->tsd_count) = NULL;
return (0);
}
/*
* If necessary initialize the thread specific data key at tsd_key, and
* allocate a mftsd_t structure to hold our thread specific data. Upon
* success, the address the thread specific data is returned. On failure,
* NULL is returned and errno is set.
*/
static mftsd_t *
get_thread_specific_data()
{
mftsd_t *tsdp;
if (pthread_key_create_once_np(&tsd_key, NULL) != 0)
return (NULL);
tsdp = (mftsd_t *)pthread_getspecific(tsd_key);
if (tsdp == NULL) {
/*
* First time for this thread. We need to allocate memory
* for our thread specific data.
*/
tsdp = uu_zalloc(sizeof (*tsdp));
if (tsdp == NULL) {
errno = ENOMEM;
return (NULL);
}
errno = pthread_setspecific(tsd_key, tsdp);
if (errno != 0) {
/*
* EINVAL means that our key is invalid, which
* would be a coding error.
*/
assert(errno != EINVAL);
return (NULL);
}
}
return (tsdp);
}
/*
* This function is called by nftw(3C) every time that it finds an object
* in a directory of interest. If the object is a file, process() checks
* to see if it is a service bundle file by insuring that it has a .xml
* extension.
*
* If the file is a service bundle file, and the CHECKHASH flag is set process()
* calls mhash_test_file() to see if it is a new bundle. Bundle file data
* for selected bundles is added to tsd_array in our thread specific data.
*
* Assume given file is a manifest unless BUNDLE_PROF flag is set to indicate
* it's a profile. For profile bundles, call mhash_test_file() with the
* appropriate argument.
*
* The CHECKEXT flag may be set if this was not a directory search request
* but a single service bundle file check that was determined by the caller to
* be found based not on the extension of the file.
*/
/*ARGSUSED*/
static int
process(const char *fn, const struct stat *sp, int ftw_type,
struct FTW *ftws)
{
int is_profile;
char *suffix_match;
uchar_t hash[MHASH_SIZE];
char *pname;
mftsd_t *tsdp;
if (ftw_type != FTW_F)
return (0);
tsdp = get_thread_specific_data();
if (tsdp == NULL)
return (-1);
/*
* Only check the extension on the file when
* requested.
*/
if (tsdp->tsd_flags & CHECKEXT) {
suffix_match = strstr(fn, ".xml");
if (suffix_match == NULL || strcmp(suffix_match, ".xml") != 0)
return (0);
}
if (tsdp->tsd_flags & CHECKHASH) {
is_profile = (tsdp->tsd_flags & BUNDLE_PROF) ? 1 : 0;
if (mhash_test_file(tsdp->tsd_hndl, fn, is_profile, &pname,
hash) == MHASH_NEWFILE) {
return (add_pointer(tsdp, fn, pname, hash));
}
} else {
return (add_pointer(tsdp, fn, NULL, NULL));
}
return (0);
}
/*
* This function returns a pointer to an array of manifest_info_t pointers.
* There is one manifest_info_t pointer for each service bundle file in the
* directory, dir, that satifies the selection criteria. The array is
* returned to arrayp. The array will be terminated with a NULL pointer.
* It is the responsibility of the caller to free the memory associated
* with the array by calling free_manifest_array().
*
* flags :
* 0x1 - CHECKHASH - do the hash check and only return bundle
* files that do not have a hash entry in the smf/manifest table
* or the hash value has changed due to the bundle file having
* been modified. If not set then all service bundle files found
* are returned, regardless of the hash status.
*
* 0x2 - CHECKEXT - Check the extension of the file is .xml
*
* On success a count of the number of selected bundles is returned.
* Note, however, that *arrayp will be set to NULL if the selection is
* empty, and a count of 0 will be returned. In the case of failure, -1
* will be returned and errno will be set.
*
* This function takes a repository handle argument from the caller and saves
* that handle in a thread specific data structure. The thread specific
* repository handle is used in process() to communicate with the appropriate
* repository. Thus callers should take care of thread safety with respect to
* the repository handle. Currently, the two callers of find_manifests are both
* single threaded, i.e. svccfg and mfstscan, so thread safety not an issue.
*/
int
find_manifests(scf_handle_t *hndl, const char *dir,
manifest_info_t ***arrayp, int flags)
{
mftsd_t *tsdp;
int status = -1;
int count;
tsdp = get_thread_specific_data();
if (tsdp == NULL)
return (-1);
tsdp->tsd_flags = flags;
if (tsdp->tsd_flags & CHECKHASH) {
tsdp->tsd_hndl = hndl;
}
if (nftw(dir, process, MAX_DEPTH, FTW_MOUNT) == 0) {
status = 0;
}
out:
if (status == 0) {
*arrayp = tsdp->tsd_array;
count = tsdp->tsd_count;
} else {
*arrayp = NULL;
free_manifest_array(tsdp->tsd_array);
count = -1;
}
/* Reset thread specific data. */
(void) memset(tsdp, 0, sizeof (*tsdp));
return (count);
}
/*
* Free the memory associated with the array of manifest_info structures.
*/
void
free_manifest_array(manifest_info_t **array)
{
manifest_info_t **entry;
manifest_info_t *info;
if (array == NULL)
return;
for (entry = array; *entry != NULL; entry++) {
info = *entry;
uu_free((void *) info->mi_path);
uu_free((void *) info->mi_prop);
uu_free(info);
}
/*
* Array is allocated with realloc(3C), so it must be freed with
* free(3c) rather than uu_free().
*/
free(array);
}
|