Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: umount.c,v 1.53 2020/04/23 04:21:13 christos Exp $	*/

/*-
 * Copyright (c) 1980, 1989, 1993
 *	The Regents of the University of California.  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.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
 */

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993\
 The Regents of the University of California.  All rights reserved.");
#endif /* not lint */

#ifndef lint
#if 0
static char sccsid[] = "@(#)umount.c	8.8 (Berkeley) 5/8/95";
#else
__RCSID("$NetBSD: umount.c,v 1.53 2020/04/23 04:21:13 christos Exp $");
#endif
#endif /* not lint */

#include <sys/param.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/time.h>
#ifndef SMALL
#include <sys/socket.h>

#include <netdb.h>
#include <rpc/rpc.h>
#include <rpc/pmap_clnt.h>
#include <rpc/pmap_prot.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsmount.h>
#endif /* !SMALL */

#include <err.h>
#include <errno.h>
#include <fstab.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>

typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;

#ifndef SMALL
#include "mountprog.h"

static int	 fake, verbose;
static char	*nfshost;
static struct addrinfo *nfshost_ai = NULL;

static int	 namematch(const struct addrinfo *);
static int	 sacmp(const struct sockaddr *, const struct sockaddr *);
static int	 xdr_dir(XDR *, char *);
static const char *getmntproto(const char *);
#endif /* !SMALL */

static int	 fflag;
static char	*getmntname(const char *, mntwhat, char **);
static int	 umountfs(const char *, const char **, int);
static void	 usage(void) __dead;

int
main(int argc, char *argv[])
{
	int ch, errs, all = 0, raw = 0;
	char mntfromname[MAXPATHLEN];
#ifndef SMALL
	int mnts;
	struct statvfs *mntbuf;
	struct addrinfo hints;
#endif /* SMALL */
	const char **typelist = NULL;

#ifdef SMALL
#define OPTS "fR"
#else
#define OPTS "AaFfh:Rt:v"
#endif
	while ((ch = getopt(argc, argv, OPTS)) != -1)
		switch (ch) {
		case 'f':
			fflag = MNT_FORCE;
			break;
		case 'R':
			raw = 1;
			break;
#ifndef SMALL
		case 'A':
		case 'a':
			all = 1;
			break;
		case 'F':
			fake = 1;
			break;
		case 'h':	/* -h implies -A. */
			all = 1;
			nfshost = optarg;
			break;
		case 't':
			if (typelist != NULL)
				errx(1, "only one -t option may be specified.");
			typelist = makevfslist(optarg);
			break;
		case 'v':
			verbose = 1;
			break;
#endif /* !SMALL */
		default:
			usage();
			/* NOTREACHED */
		}
	argc -= optind;
	argv += optind;

	if ((argc == 0 && !all) || (argc != 0 && all) || (all && raw))
		usage();

#ifndef SMALL
	/* -h implies "-t nfs" if no -t flag. */
	if ((nfshost != NULL) && (typelist == NULL))
		typelist = makevfslist("nfs");

	if (nfshost != NULL) {
		memset(&hints, 0, sizeof hints);
		if (getaddrinfo(nfshost, NULL, &hints, &nfshost_ai) != 0) {
			nfshost_ai = NULL;
		}
	}
		
	errs = 0;
	if (all) {
		if ((mnts = getmntinfo(&mntbuf, ST_NOWAIT)) == 0) {
			warn("getmntinfo");
			errs = 1;
		}
		for (errs = 0, mnts--; mnts > 0; mnts--) {
			if (checkvfsname(mntbuf[mnts].f_fstypename, typelist))
				continue;
			if (umountfs(mntbuf[mnts].f_mntonname, typelist,
			             1) != 0)
				errs = 1;
		}
	} else 
#endif /* !SMALL */
		for (errs = 0; *argv != NULL; ++argv) {
			if (getfsspecname(mntfromname, sizeof(mntfromname),
			    *argv) == NULL)
				err(EXIT_FAILURE, "%s", mntfromname);
			if (umountfs(mntfromname, typelist, raw) != 0)
				errs = 1;
		}
	return errs;
}

static int
umountfs(const char *name, const char **typelist, int raw)
{
#ifndef SMALL
	enum clnt_stat clnt_stat;
	struct timeval try;
	CLIENT *clp;
	char *hostp = NULL;
	struct addrinfo *ai = NULL, hints;
	const char *proto = NULL;
#endif /* !SMALL */
	const char *mntpt;
	char *type, rname[MAXPATHLEN], umountprog[MAXPATHLEN];
	mntwhat what;
	struct stat sb;

	if (raw) {
		mntpt = name;
	} else {

		what = MNTANY;
		if (realpath(name, rname) != NULL) {
			name = rname;

			if (stat(name, &sb) == 0) {
				if (S_ISBLK(sb.st_mode))
					what = MNTON;
				else if (S_ISDIR(sb.st_mode))
					what = MNTFROM;
			}
		}
#ifdef SMALL
		else {
 			warn("%s", name);
 			return 1;
		}
#endif /* SMALL */
		mntpt = name;

		switch (what) {
		case MNTON:
			if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
				warnx("%s: not currently mounted", name);
				return (1);
			}
			break;
		case MNTFROM:
			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
				warnx("%s: not currently mounted", mntpt);
				return (1);
			}
			break;
		default:
			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
				name = mntpt;
				if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
					warnx("%s: not currently mounted", name);
					return 1;
				}
			}
		}

#ifndef SMALL
		if (checkvfsname(type, typelist))
			return 1;

		(void)memset(&hints, 0, sizeof hints);
		if (!strncmp(type, MOUNT_NFS,
		    sizeof(((struct statvfs *)NULL)->f_fstypename))) {
			char *delimp;
			proto = getmntproto(mntpt);
			/* look for host:mountpoint */
			if ((delimp = strrchr(name, ':')) != NULL) {
				int len = delimp - name;
				hostp = malloc(len + 1);
				if (hostp == NULL)
				    	return 1;
				memcpy(hostp, name, len);
				hostp[len] = 0;
				name += len + 1;
				if (getaddrinfo(hostp, NULL, &hints, &ai) != 0)
					ai = NULL;
			}
		}

		if (!namematch(ai))
			return 1;
#endif /* ! SMALL */
		snprintf(umountprog, sizeof(umountprog), "umount_%s", type);
	}

#ifndef SMALL
	if (verbose) {
		(void)printf("%s: unmount from %s\n", name, mntpt);
		/* put this before the test of FAKE */ 
		if (!raw) {
			(void)printf("Trying unmount program %s\n",
			    umountprog);
		}
	}
	if (fake)
		return 0;
#endif /* ! SMALL */

	if (!raw) {
		/*
		 * The only options that need to be passed on are -f
		 * and -v.
		 */
		char *args[3];
		unsigned nargs = 0;

		args[nargs++] = umountprog;
		if (fflag == MNT_FORCE) {
			args[nargs++] = __UNCONST("-f");
		}
#ifndef SMALL
		if (verbose) {
			args[nargs++] = __UNCONST("-v");
		}
#endif
		execvp(umountprog, args);
		if (errno != ENOENT) {
			warn("%s: execvp", umountprog);
		}
	}

#ifndef SMALL
	if (verbose)
		(void)printf("(No separate unmount program.)\n");
#endif

	if (unmount(mntpt, fflag) == -1) {
		warn("%s", mntpt);
		return 1;
	}

#ifndef SMALL
	if (ai != NULL && !(fflag & MNT_FORCE)) {
		clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, proto);
		if (clp  == NULL) {
			clnt_pcreateerror("Cannot MNT PRC");
			return 1;
		}
		clp->cl_auth = authsys_create_default();
		try.tv_sec = 20;
		try.tv_usec = 0;
		clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
		    __UNCONST(name), xdr_void, NULL, try);
		if (clnt_stat != RPC_SUCCESS) {
			clnt_perror(clp, "Bad MNT RPC");
			return 1;
		}
		auth_destroy(clp->cl_auth);
		clnt_destroy(clp);
	}
#endif /* ! SMALL */
	return 0;
}

static char *
getmntname(const char *name, mntwhat what, char **type)
{
	static struct statvfs *mntbuf;
	static int mntsize;
	static char mntfromname[MAXPATHLEN];
	int i;

	if (mntbuf == NULL &&
	    (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
		warn("getmntinfo");
		return (NULL);
	}
	for (i = mntsize - 1; i >= 0; i--) {
		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
			if (type)
				*type = mntbuf[i].f_fstypename;
			return (mntbuf[i].f_mntonname);
		}
		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
			if (type)
				*type = mntbuf[i].f_fstypename;
			if (getfsspecname(mntfromname, sizeof(mntfromname),
			    mntbuf[i].f_mntfromname) == NULL)
				err(EXIT_FAILURE, "%s", mntfromname);
			return mntfromname;
		}
	}
	return (NULL);
}

#ifndef SMALL
static int
sacmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
{
	const void *p1, *p2;
	size_t len;

	if (sa1->sa_family != sa2->sa_family)
		return 1;

	switch (sa1->sa_family) {
	case AF_INET:
		p1 = &((const struct sockaddr_in *)sa1)->sin_addr;
		p2 = &((const struct sockaddr_in *)sa2)->sin_addr;
		len = 4;
		break;
	case AF_INET6:
		p1 = &((const struct sockaddr_in6 *)sa1)->sin6_addr;
		p2 = &((const struct sockaddr_in6 *)sa2)->sin6_addr;
		len = 16;
		if (((const struct sockaddr_in6 *)sa1)->sin6_scope_id !=
		    ((const struct sockaddr_in6 *)sa2)->sin6_scope_id)
			return 1;
		break;
	default:
		return 1;
	}

	return memcmp(p1, p2, len);
}

static int
namematch(const struct addrinfo *ai)
{
	struct addrinfo *aip;

	if (nfshost == NULL || nfshost_ai == NULL)
		return (1);

	while (ai != NULL) {
		aip = nfshost_ai;
		while (aip != NULL) {
			if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
				return 1;
			aip = aip->ai_next;
		}
		ai = ai->ai_next;
	}

	return 0;
}

/*
 * xdr routines for mount rpc's
 */
static int
xdr_dir(XDR *xdrsp, char *dirp)
{
	return xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN);
}

static const char *
getmntproto(const char *name)
{
	struct nfs_args nfsargs;
	struct sockaddr_storage ss;

	nfsargs.sotype = SOCK_DGRAM;
	nfsargs.addr = (struct sockaddr *)&ss; 
	nfsargs.addrlen = sizeof(ss);
	(void)mount("nfs", name, MNT_GETARGS, &nfsargs, sizeof(nfsargs));
	return nfsargs.sotype == SOCK_STREAM ? "tcp" : "udp";
}
#endif /* !SMALL */

static void
usage(void)
{
#ifdef SMALL
	(void)fprintf(stderr,
	    "Usage: %s [-fR]  special | node\n", getprogname());
#else
	(void)fprintf(stderr,
	    "Usage: %s [-fvFR] [-t fstypelist] special | node\n"
	    "\t %s -a[fvF] [-h host] [-t fstypelist]\n", getprogname(),
	    getprogname());
#endif /* SMALL */
	exit(1);
}