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
|
/*
* Copyright (c) 2004 Alfred Perlstein <alfred@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
* $Id: libautofs.c,v 1.5 2004/09/08 08:44:12 bright Exp $
*/
#include <err.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#ifdef AUTOFSSTANDALONE
#include "../autofs/autofs.h"
#else
#include <fs/autofs/autofs.h>
#endif
#include "libautofs.h"
struct auto_handle {
char ah_mp[MNAMELEN];
fsid_t ah_fsid;
int ah_fd;
};
static int autofs_sysctl(int, fsid_t *, void *, size_t *, void *, size_t);
static void safe_free(void *ptr);
static int getmntlst(struct statfs **sfsp, int *cntp);
static void
safe_free(void *ptr)
{
int saved_errno;
saved_errno = errno;
free(ptr);
errno = saved_errno;
}
int
getmntlst(struct statfs **sfsp, int *cntp)
{
int cnt;
long bufsize;
*sfsp = NULL;
cnt = getfsstat(NULL, 0, MNT_NOWAIT);
bufsize = cnt * sizeof(**sfsp);
/*fprintf(stderr, "getmntlst bufsize %ld, cnt %d\n", bufsize, cnt);*/
*sfsp = malloc(bufsize);
if (sfsp == NULL)
goto err;
cnt = getfsstat(*sfsp, bufsize, MNT_NOWAIT);
if (cnt == -1)
goto err;
*cntp = cnt;
/*fprintf(stderr, "getmntlst ok, cnt %d\n", cnt);*/
return (0);
err:
safe_free(sfsp);
*sfsp = NULL;
/*fprintf(stderr, "getmntlst bad\n");*/
return (-1);
}
/* get a handle based on a path. */
int
autoh_get(const char *path, autoh_t *ahp)
{
struct statfs *sfsp, *sp;
int cnt, fd, i;
autoh_t ret;
ret = NULL;
/*
* We use getfsstat to prevent avoid the lookups on the mountpoints
* that statfs(2) would do.
*/
if (getmntlst(&sfsp, &cnt))
goto err;
for (i = 0; i < cnt; i++) {
if (strcmp(sfsp[i].f_mntonname, path) == 0)
break;
}
if (i == cnt) {
/*fprintf(stderr, "autoh_get bad %d %d\n", i, cnt);*/
errno = ENOENT;
goto err;
}
sp = &sfsp[i];
if (strcmp(sp->f_fstypename, "autofs")) {
errno = ENOTTY;
goto err;
}
fd = open(sp->f_mntonname, O_RDONLY);
if (fd == -1)
goto err;
ret = malloc(sizeof(*ret));
if (ret == NULL)
goto err;
ret->ah_fsid = sp->f_fsid;
ret->ah_fd = fd;
strlcpy(ret->ah_mp, sp->f_mntonname, sizeof(ret->ah_mp));
safe_free(sfsp);
*ahp = ret;
return (0);
err:
safe_free(ret);
safe_free(sfsp);
return (-1);
}
/* release. */
void
autoh_free(autoh_t ah)
{
int saved_errno;
saved_errno = errno;
close(ah->ah_fd);
free(ah);
errno = saved_errno;
}
/*
* Get an array of pointers to all the currently mounted autofs
* instances.
*/
int
autoh_getall(autoh_t **arrayp, int *cntp)
{
struct statfs *sfsp;
int cnt, i, pos;
autoh_t *array;
array = NULL;
/*
* We use getfsstat to prevent avoid the lookups on the mountpoints
* that statfs(2) would do.
*/
if (getmntlst(&sfsp, &cnt))
goto err;
array = *arrayp = calloc(cnt + 1, sizeof(**arrayp));
if (array == NULL)
goto err;
for (i = 0, pos = 0; i < cnt; i++) {
if (autoh_get(sfsp[i].f_mntonname, &array[pos]) == -1) {
/* not an autofs entry, that's ok, otherwise bail */
if (errno == ENOTTY)
continue;
goto err;
}
pos++;
}
if (pos == 0) {
errno = ENOENT;
goto err;
}
*arrayp = array;
*cntp = pos;
safe_free(sfsp);
return (0);
err:
safe_free(sfsp);
if (array)
autoh_freeall(array);
return (-1);
}
/* release. */
void
autoh_freeall(autoh_t *ah)
{
autoh_t *ahp;
ahp = ah;
while (*ahp != NULL) {
autoh_free(*ahp);
ahp++;
}
safe_free(ah);
}
/* return fd to select on. */
int
autoh_fd(autoh_t ah)
{
return (ah->ah_fd);
}
const char *
autoh_mp(autoh_t ah)
{
return (ah->ah_mp);
}
static int do_autoreq_get(autoh_t ah, autoreq_t *reqp, int *cntp);
/* get an array of pending requests */
int
autoreq_get(autoh_t ah, autoreq_t **reqpp, int *cntp)
{
int cnt, i;
autoreq_t req, *reqp;
if (do_autoreq_get(ah, &req, &cnt))
return (-1);
reqp = calloc(cnt + 1, sizeof(*reqp));
if (reqp == NULL) {
safe_free(req);
return (-1);
}
for (i = 0; i < cnt; i++)
reqp[i] = &req[i];
*reqpp = reqp;
*cntp = cnt;
return (0);
}
int
do_autoreq_get(autoh_t ah, autoreq_t *reqp, int *cntp)
{
size_t olen;
struct autofs_userreq *reqs;
int cnt, error;
int vers;
vers = AUTOFS_PROTOVERS;
error = 0;
reqs = NULL;
olen = 0;
cnt = 0;
error = autofs_sysctl(AUTOFS_CTL_GETREQS, &ah->ah_fsid, NULL, &olen,
&vers, sizeof(vers));
if (error == -1)
goto out;
if (olen == 0)
goto out;
reqs = malloc(olen);
if (reqs == NULL)
goto out;
error = autofs_sysctl(AUTOFS_CTL_GETREQS, &ah->ah_fsid, reqs, &olen,
&vers, sizeof(vers));
if (error == -1)
goto out;
out:
if (error) {
safe_free(reqs);
return (-1);
}
cnt = olen / sizeof(*reqs);
*cntp = cnt;
*reqp = reqs;
return (0);
}
/* free an array of requests */
void
autoreq_free(autoh_t ah __unused, autoreq_t *req)
{
free(*req);
free(req);
}
/* serve a request */
int
autoreq_serv(autoh_t ah, autoreq_t req)
{
int error;
error = autofs_sysctl(AUTOFS_CTL_SERVREQ, &ah->ah_fsid, NULL, NULL,
req, sizeof(*req));
return (error);
}
enum autoreq_op
autoreq_getop(autoreq_t req)
{
switch (req->au_op) {
case AREQ_LOOKUP:
return (AUTOREQ_OP_LOOKUP);
case AREQ_STAT:
return (AUTOREQ_OP_STAT);
case AREQ_READDIR:
return (AUTOREQ_OP_READDIR);
default:
return (AUTOREQ_OP_UNKNOWN);
}
}
/* get a request's file name. */
const char *
autoreq_getpath(autoreq_t req)
{
return (req->au_name);
}
/* get a request's inode. a indirect mount may return AUTO_INODE_NONE. */
autoino_t
autoreq_getino(autoreq_t req)
{
return (req->au_ino);
}
void
autoreq_setino(autoreq_t req, autoino_t ino)
{
req->au_ino = ino;
}
/* get a request's directory inode. */
autoino_t
autoreq_getdirino(autoreq_t req)
{
return (req->au_dino);
}
void
autoreq_seterrno(autoreq_t req, int error)
{
req->au_errno = error;
}
void
autoreq_setaux(autoreq_t req, void *auxdata, size_t auxlen)
{
req->au_auxdata = auxdata;
req->au_auxlen = auxlen;
}
void
autoreq_getaux(autoreq_t req, void **auxdatap, size_t *auxlenp)
{
*auxdatap = req->au_auxdata;
*auxlenp = req->au_auxlen;
}
void
autoreq_seteof(autoreq_t req, int eof)
{
req->au_eofflag = eof;
}
void
autoreq_getoffset(autoreq_t req, off_t *offp)
{
*offp = req->au_offset - AUTOFS_USEROFF;
}
void
autoreq_getxid(autoreq_t req, int *xid)
{
*xid = req->au_xid;
}
/* toggle by path. args = handle, AUTO_?, pid (-1 to disable), path. */
int
autoh_togglepath(autoh_t ah, int op, pid_t pid, const char *path)
{
int fd, ret;
fd = open(path, O_RDONLY);
if (fd == -1)
return (-1);
ret = autoh_togglefd(ah, op, pid, fd);
close(fd);
return (ret);
}
/* toggle by fd. args = handle, AUTO_?, pid (-1 to disable), fd. */
int
autoh_togglefd(autoh_t ah, int op, pid_t pid, int fd)
{
struct stat sb;
struct autofs_mounterreq mr;
int error, realop;
switch (op) {
case AUTO_DIRECT:
realop = AUTOFS_CTL_TRIGGER;
break;
case AUTO_INDIRECT:
realop = AUTOFS_CTL_SUBTRIGGER;
break;
case AUTO_MOUNTER:
realop = AUTOFS_CTL_MOUNTER;
break;
case AUTO_BROWSE:
realop = AUTOFS_CTL_BROWSE;
break;
default:
errno = ENOTTY;
return (-1);
}
if (fstat(fd, &sb))
return (-1);
bzero(&mr, sizeof(mr));
mr.amu_ino = sb.st_ino;
mr.amu_pid = pid;
error = autofs_sysctl(realop, &ah->ah_fsid, NULL, NULL,
&mr, sizeof(mr));
return (error);
}
int
autofs_sysctl(op, fsid, oldp, oldlenp, newp, newlen)
int op;
fsid_t *fsid;
void *oldp;
size_t *oldlenp;
void *newp;
size_t newlen;
{
struct vfsidctl vc;
bzero(&vc, sizeof(vc));
vc.vc_op = op;
strcpy(vc.vc_fstypename, "*");
vc.vc_vers = VFS_CTL_VERS1;
vc.vc_fsid = *fsid;
vc.vc_ptr = newp;
vc.vc_len = newlen;
return (sysctlbyname("vfs.autofs.ctl", oldp, oldlenp, &vc, sizeof(vc)));
}
|