mountall.c 96.4 KB
Newer Older
Scott James Remnant's avatar
Scott James Remnant committed
1
2
/* mountall
 *
3
 * Copyright © 2010 Canonical Ltd.
Scott James Remnant's avatar
Scott James Remnant committed
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
 * Author: Scott James Remnant <scott@netsplit.com>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */


#include <libudev.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mount.h>

#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <stdio.h>
#include <limits.h>
#include <mntent.h>
Scott James Remnant's avatar
Scott James Remnant committed
39
#include <stdarg.h>
Scott James Remnant's avatar
Scott James Remnant committed
40
41
42
43
44
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <fnmatch.h>
Johan Kiviniemi's avatar
Johan Kiviniemi committed
45
#include <dirent.h>
Scott James Remnant's avatar
Scott James Remnant committed
46
47
48
49

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/string.h>
Scott James Remnant's avatar
Scott James Remnant committed
50
#include <nih/list.h>
Scott James Remnant's avatar
Scott James Remnant committed
51
52
#include <nih/hash.h>
#include <nih/timer.h>
Scott James Remnant's avatar
Scott James Remnant committed
53
#include <nih/signal.h>
Scott James Remnant's avatar
Scott James Remnant committed
54
#include <nih/child.h>
Scott James Remnant's avatar
Scott James Remnant committed
55
56
#include <nih/io.h>
#include <nih/main.h>
Scott James Remnant's avatar
Scott James Remnant committed
57
#include <nih/option.h>
Scott James Remnant's avatar
Scott James Remnant committed
58
59
#include <nih/logging.h>
#include <nih/error.h>
Scott James Remnant's avatar
Scott James Remnant committed
60
#include <nih/errors.h>
Scott James Remnant's avatar
Scott James Remnant committed
61

Scott James Remnant's avatar
Scott James Remnant committed
62
#include <nih-dbus/dbus_error.h>
Scott James Remnant's avatar
Scott James Remnant committed
63
64
#include <nih-dbus/dbus_connection.h>
#include <nih-dbus/dbus_proxy.h>
Scott James Remnant's avatar
Scott James Remnant committed
65
#include <nih-dbus/errors.h>
Scott James Remnant's avatar
Scott James Remnant committed
66

67
68
69
#include <ply-event-loop.h>
#include <ply-boot-client.h>

70
71
#include "ioprio.h"

Scott James Remnant's avatar
Scott James Remnant committed
72
73
#include "dbus/upstart.h"
#include "com.ubuntu.Upstart.h"
74
#include "control.h"
Scott James Remnant's avatar
Scott James Remnant committed
75
76


77
78
#define BUILTIN_FSTAB   "/lib/init/fstab"

79
#define BOREDOM_TIMEOUT 3
80
#define ROOTDELAY 30
81

82
83
84
85
enum exit {
	EXIT_OK = 0,		/* Ok */
	EXIT_ERROR = 1,		/* General/OS error */
	EXIT_SHELL = 2,		/* Start maintenance shell */
86
	EXIT_SHELL_REBOOT = 3,	/* Start maintenance shell, reboot when done */
87
88
89
	EXIT_REBOOT = 4,	/* System must reboot */
};

90
typedef enum {
91
	TAG_UNKNOWN,
92
93
94
	TAG_LOCAL,
	TAG_REMOTE,
	TAG_VIRTUAL,
95
96
	TAG_SWAP,
	TAG_NOWAIT,
97
	TAG_SKIPPED,
98
	TAG_TIMEOUT,
99
100
} Tag;

101
102
103
104
105
106
107
108
109
typedef enum {
	ERROR_NONE,
	ERROR_BORED,
	ERROR_FSCK_FAILED,
	ERROR_FSCK_FAILED_HARD,
	ERROR_MOUNT_FAILED,
	ERROR_FSCK_IN_PROGRESS,
} Error;

Scott James Remnant's avatar
Scott James Remnant committed
110
111
112
typedef struct mount Mount;

struct mount {
113
114
	NihList             entry;

Scott James Remnant's avatar
Scott James Remnant committed
115
116
117
	char *              mountpoint;
	pid_t               mount_pid;
	int                 mounted;
118
	dev_t               mounted_dev;
Scott James Remnant's avatar
Scott James Remnant committed
119
120
121

	char *              device;
	struct udev_device *udev_device;
Johan Kiviniemi's avatar
Johan Kiviniemi committed
122
123
	NihHash *           physical_dev_ids;
	int                 physical_dev_ids_needed;
Scott James Remnant's avatar
Scott James Remnant committed
124
	pid_t               fsck_pid;
125
	int                 fsck_progress;
126
	int                 fsck_fix;
127
	int                 ready;
Scott James Remnant's avatar
Scott James Remnant committed
128

129
130
	DBusPendingCall *   pending_call;

131
132
	Error               error;

Scott James Remnant's avatar
Scott James Remnant committed
133
	char *              type;
Scott James Remnant's avatar
Scott James Remnant committed
134
	int                 nodev;
Scott James Remnant's avatar
Scott James Remnant committed
135
136
	char *              opts;
	char *              mount_opts;
137
138
139
140
141
	int                 check;

	Tag                 tag;
	int                 has_showthrough;
	Mount *             showthrough;
142
143
	int                 checked_link;
	Mount *             link_target;
144
	NihList             deps;
145
	int                 needs_mtab;
Scott James Remnant's avatar
Scott James Remnant committed
146
147
};

148
149
150
151
#define MOUNT_NAME(_mnt) (strcmp ((_mnt)->type, "swap")			\
			  && strcmp ((_mnt)->mountpoint, "none")	\
			  ? (_mnt)->mountpoint : (_mnt)->device)

Scott James Remnant's avatar
Scott James Remnant committed
152
153
154
155
156
157
158
159
160
161
162
163
typedef struct filesystem {
	char *name;
	int   nodev;
} Filesystem;

typedef struct process {
	Mount *       mnt;
	char * const *args;
	pid_t         pid;
	void (*handler) (Mount *mnt, pid_t pid, int status);
} Process;

164
165
166
167
typedef struct event_reply_data {
	Mount *          mnt;
	void (*handler) (void *data, NihDBusMessage *message);
} EventReplyData;
Scott James Remnant's avatar
Scott James Remnant committed
168

169
170
171
172
173
174
175
176
177
178
Mount *new_mount             (const char *mountpoint, const char *device,
			      int check, const char *type, const char *opts);
Mount *find_mount            (const char *mountpoint);
void   update_mount          (Mount *mnt, const char *device, int check,
			      const char *type, const char *opts);

int    has_option            (Mount *mnt, const char *option, int current);
char * get_option            (const void *parent, Mount *mnt, const char *option,
			      int current);
char * cut_options           (const void *parent, Mount *mnt, ...);
Scott James Remnant's avatar
Scott James Remnant committed
179

180
181
void   parse_fstab           (const char *filename);
void   parse_mountinfo       (void);
Scott James Remnant's avatar
Scott James Remnant committed
182

183
void   parse_filesystems     (void);
184

185
void   mount_policy          (void);
186
void   tag_mount             (Mount *mnt, Tag tag);
187
void   mark_mounted          (void);
Scott James Remnant's avatar
Scott James Remnant committed
188

189
void   mounted               (Mount *mnt);
190
void   skip_mount            (Mount *mnt);
191
void   trigger_events        (void);
192

193
194
void   try_mounts            (void);
void   try_mount             (Mount *mnt, int force);
195
void   mounting_event_handled(void *data, NihDBusMessage *message);
196
void   mounted_event_handled (void *data, NihDBusMessage *message);
197

198
199
200
int    find_on_path          (const char *name);
int    find_fsck             (const char *type);

201
202
203
204
pid_t  spawn                 (Mount *mnt, char * const *args, int wait,
			      void (*handler) (Mount *mnt, pid_t pid, int status));
void   spawn_child_handler   (Process *proc, pid_t pid,
			      NihChildEvents event, int status);
Scott James Remnant's avatar
Scott James Remnant committed
205

206
207
void   run_mount             (Mount *mnt, int fake);
void   run_mount_finished    (Mount *mnt, pid_t pid, int status);
Scott James Remnant's avatar
Scott James Remnant committed
208

209
210
void   run_swapon            (Mount *mnt);
void   run_swapon_finished   (Mount *mnt, pid_t pid, int status);
Scott James Remnant's avatar
Scott James Remnant committed
211

212
int    missing_fsck          (Mount *mnt);
213
214
void   run_fsck              (Mount *mnt);
void   run_fsck_finished     (Mount *mnt, pid_t pid, int status);
Scott James Remnant's avatar
Scott James Remnant committed
215

216
void   write_mtab            (void);
Scott James Remnant's avatar
Scott James Remnant committed
217

218
void   mount_showthrough     (Mount *root);
Scott James Remnant's avatar
Scott James Remnant committed
219

220
void   upstart_disconnected  (DBusConnection *connection);
221
222
void   emit_event            (const char *name, Mount *mnt,
			      void (*cb) (void *, NihDBusMessage *message));
223
void   emit_event_error      (void *data, NihDBusMessage *message);
Scott James Remnant's avatar
Scott James Remnant committed
224

225
226
227
void   udev_monitor_watcher  (struct udev_monitor *udev_monitor,
			      NihIoWatch *watch, NihIoEvents events);
void   udev_catchup          (void);
Scott James Remnant's avatar
Scott James Remnant committed
228

229
void   fsck_update           (void);
Scott James Remnant's avatar
Scott James Remnant committed
230

231
232
void   fsck_reader           (Mount *mnt, NihIo *io,
			      const char *buf, size_t len);
233
234
void   boredom_timeout       (void *data, NihTimer *timer);

235
int    plymouth_connect      (void);
236
void   plymouth_disconnected (void *user_data, ply_boot_client_t *client);
Scott James Remnant's avatar
Scott James Remnant committed
237

238
void   plymouth_progress     (Mount *mnt, int progress);
239
240
241
242
243
244
245
void   plymouth_update       (int only_clear);

void   plymouth_response     (void *user_data, ply_boot_client_t *client);
void   plymouth_failed       (void *user_data, ply_boot_client_t *client);
void   plymouth_answer       (void *user_data, const char *keys,
			      ply_boot_client_t *client);

246
void   usr1_handler          (void *data, NihSignal *signal);
247
int    set_dev_wait_time        (NihOption *option, const char *arg);
248

Scott James Remnant's avatar
Scott James Remnant committed
249

250
251
252
253
254
255
256
/**
 * mounts:
 *
 * List of mounts that we need to process, parsed from /etc/fstab
 * (with built-ins and previously mounted filesystems added too).
 * Each list entry is a Mount structure.
 **/
257
NihList *mounts = NULL;
Scott James Remnant's avatar
Scott James Remnant committed
258

259
/**
260
 * Counters for the different tags.
261
262
263
264
265
266
267
268
269
 **/
size_t num_local = 0;
size_t num_local_mounted = 0;
size_t num_remote = 0;
size_t num_remote_mounted = 0;
size_t num_virtual = 0;
size_t num_virtual_mounted = 0;
size_t num_swap = 0;
size_t num_swap_mounted = 0;
270
271
size_t num_timeout = 0;
size_t num_timeout_mounted = 0;
272

273
274
275
276
277
278
/**
 * newly_mounted:
 *
 * Set to TRUE if we've successfully mounted something, informs the main loop
 * to try and mount non-mounted things again.
 **/
279
int newly_mounted = TRUE;
280

281
282
283
284
285
286
287
288
289
290
291
292
293
/**
 * fsck_in_progress:
 *
 * Set to TRUE while filesystem checks are in progress, this isn't quite the
 * same as fsck being running (which fsck_update) checks since fsck might
 * decide not to check the filesystems.
 *
 * Therefore this gets set to TRUE when we receive the first progress
 * information in fsck_progress(), and reset to FALSE in fsck_update() when
 * no fsck is running.
 **/
int fsck_in_progress = FALSE;

Scott James Remnant's avatar
docs    
Scott James Remnant committed
294
295
296
297
298
299
300
/**
 * filesystems:
 *
 * Array of filesystem information parsed from /proc/filesystems, primarily
 * used to eliminate arch-specific filesystems that don't exist when we
 * start and figure out which filesystems don't have devices.
 **/
Scott James Remnant's avatar
Scott James Remnant committed
301
302
303
304
Filesystem *filesystems = NULL;
size_t num_filesystems = 0;


Scott James Remnant's avatar
docs    
Scott James Remnant committed
305
306
307
308
309
/**
 * written_mtab:
 *
 * TRUE once we've successfully written /etc/mtab.
 **/
Scott James Remnant's avatar
Scott James Remnant committed
310
311
int written_mtab = FALSE;

Scott James Remnant's avatar
docs    
Scott James Remnant committed
312
313
314
315
316
317
/**
 * exit_code:
 *
 * Rather than exit immediately in case of error, we delay the exit until
 * any background processes (fsck, mount, etc.) have finished.
 **/
318
int exit_code = -1;
Scott James Remnant's avatar
Scott James Remnant committed
319

Johan Kiviniemi's avatar
Johan Kiviniemi committed
320

Scott James Remnant's avatar
Scott James Remnant committed
321
322
323
324
325
326
327
/**
 * upstart:
 *
 * Proxy to Upstart daemon.
 **/
static NihDBusProxy *upstart = NULL;

328
329
330
331
332
333
334
/**
 * udev:
 *
 * libudev context.
 **/
static struct udev *udev = NULL;

335
336
337
338
339
340
341
/**
 * ply_event_loop:
 *
 * plymouth event loop
 **/
static ply_event_loop_t *ply_event_loop = NULL;

342
343
344
345
346
347
348
/**
 * ply_boot_client:
 *
 * Plymouth boot client.
 **/
static ply_boot_client_t *ply_boot_client = NULL;

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
 * plymouth_error:
 *
 * Error currently showing on Plymouth.
 **/
static Error plymouth_error = ERROR_NONE;

/**
 * plymouth_mnt:
 *
 * Mount point currently showing an error on Plymouth.
 **/
static Mount *plymouth_mnt = NULL;

/**
 * plymouth_keys:
 *
 * Keypresses currently being watched for from Plymouth.
 **/
static const char *plymouth_keys = NULL;

370
371
372
373
374
375
376
377
/**
 * boredom_timer:
 *
 * Having waited over BOREDOM_TIMEOUT seconds for certain mounts, print a
 * message to the user.
 **/
static NihTimer *boredom_timer = NULL;

378
379
380
381
382
383
384
385
/**
 * device_ready_timer:
 *
 * Having waited over ROOTDELAY seconds for certain devices to get ready,
 * exit mountall and trigger the upstart jobs for bringing up the device
 **/

NihTimer *device_ready_timer = NULL;
386

Scott James Remnant's avatar
Scott James Remnant committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
 * daemonise:
 *
 * Set to TRUE if we should become a daemon, rather than just running
 * in the foreground.
 **/
static int daemonise = FALSE;

/**
 * force_fsck:
 *
 * Set to TRUE if we should pass -f to fsck invocations.
 **/
static int force_fsck = FALSE;

/**
 * fsck_fix:
 *
 * Set to TRUE if we should pass -y to fsck invocations.
 **/
static int fsck_fix = FALSE;

409
410
411
412
413
/**
 * no_events:
 *
 * Set to TRUE if we should not emit events.
 **/
414
static int no_events = FALSE;
415

416
417
418
419
420
421
422
423
424
425
/**
 ** dev-wait-time
 **
 ** Set to ROOTDELAY if not set. 
 ** Legal values are between 1sec to 2147483647 seconds.
 **/
static int dev_wait_time = FALSE;



Scott James Remnant's avatar
Scott James Remnant committed
426

427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
static void
dequote (char *str)
{
	size_t len;

	nih_assert (str != NULL);

	if ((str[0] != '"') && (str[0] != '\''))
		return;

	len = strlen (str);
	len -= 2;

	memmove (str, str + 1, len);
	str[len] = '\0';
}

444
445
446
447
448
449
450
451
452
453
454
455
456
static void
strip_slashes (char *str)
{
	char *p;

	nih_assert (str != NULL);

	p = str + strlen (str) - 1;
	while ((p > str) && (*p == '/'))
		*(p--) = '\0';
}


Scott James Remnant's avatar
Scott James Remnant committed
457
458
459
460
461
Mount *
new_mount (const char *mountpoint,
	   const char *device,
	   int         check,
	   const char *type,
462
	   const char *opts)
Scott James Remnant's avatar
Scott James Remnant committed
463
464
465
466
{
	Mount *mnt;

	nih_assert (mountpoint != NULL);
467
468
469
	nih_assert (device != NULL);
	nih_assert (type != NULL);
	nih_assert (opts != NULL);
Scott James Remnant's avatar
Scott James Remnant committed
470

471
472
	mnt = NIH_MUST (nih_new (NULL, Mount));
	nih_list_init (&mnt->entry);
Scott James Remnant's avatar
Scott James Remnant committed
473
474

	mnt->mountpoint = NIH_MUST (nih_strdup (mounts, mountpoint));
475
	strip_slashes (mnt->mountpoint);
476
477
	mnt->checked_link = FALSE;
	mnt->link_target = NULL;
478

Scott James Remnant's avatar
Scott James Remnant committed
479
480
	mnt->mount_pid = -1;
	mnt->mounted = FALSE;
481
	mnt->mounted_dev = -1;
Scott James Remnant's avatar
Scott James Remnant committed
482

483
	mnt->device = NULL;
Scott James Remnant's avatar
Scott James Remnant committed
484
	mnt->udev_device = NULL;
Johan Kiviniemi's avatar
Johan Kiviniemi committed
485
	mnt->physical_dev_ids = NULL;
486
	mnt->physical_dev_ids_needed = TRUE;
Scott James Remnant's avatar
Scott James Remnant committed
487
	mnt->fsck_pid = -1;
488
	mnt->fsck_progress = -1;
489
	mnt->fsck_fix = FALSE;
490
	mnt->ready = FALSE;
Scott James Remnant's avatar
Scott James Remnant committed
491

492
493
	mnt->pending_call = NULL;

494
495
	mnt->error = ERROR_NONE;

496
	mnt->type = NULL;
Scott James Remnant's avatar
Scott James Remnant committed
497
	mnt->nodev = FALSE;
498
	mnt->opts = NULL;
Scott James Remnant's avatar
Scott James Remnant committed
499
	mnt->mount_opts = NULL;
500
501
	mnt->check = check;

502
	mnt->tag = TAG_UNKNOWN;
503
504
505
	mnt->has_showthrough = FALSE;
	mnt->showthrough = NULL;
	nih_list_init (&mnt->deps);
Scott James Remnant's avatar
Scott James Remnant committed
506

507
508
509
	nih_alloc_set_destructor (mnt, nih_list_destroy);
	nih_list_add (mounts, &mnt->entry);

510
511
	mnt->needs_mtab = FALSE;

512
	update_mount (mnt, device, check, type, opts);
Scott James Remnant's avatar
Scott James Remnant committed
513
514
515
516
517
518
519
520
521

	return mnt;
}

Mount *
find_mount (const char *mountpoint)
{
	nih_assert (mountpoint != NULL);

522
523
524
525
526
527
	NIH_LIST_FOREACH (mounts, iter) {
		Mount *mnt = (Mount *)iter;

		if (! strcmp (mnt->mountpoint, mountpoint))
			return mnt;
	}
Scott James Remnant's avatar
Scott James Remnant committed
528
529
530
531
532
533
534
535
536

	return NULL;
}

void
update_mount (Mount *     mnt,
	      const char *device,
	      int         check,
	      const char *type,
537
	      const char *opts)
Scott James Remnant's avatar
Scott James Remnant committed
538
539
540
541
542
543
544
{
	nih_assert (mnt != NULL);

	if (device) {
		if (mnt->device)
			nih_unref (mnt->device, mounts);
		mnt->device = NIH_MUST (nih_strdup (mounts, device));
545
546
547
548
549
550

		if (! strncmp (mnt->device, "UUID=", 5)) {
			dequote (mnt->device + 5);
		} else if (! strncmp (mnt->device, "LABEL=", 6)) {
			dequote (mnt->device + 6);
		} else {
551
			char *colon;
552
			dequote (mnt->device);
553
554
555
556
557
558
559
560
561
562
563
564
			/* If our device name is in host:/path format, as is
			 * commonly used for network filesystems, don't strip
			 * trailing slashes if this is the entire path.  We
			 * look for the colon starting from the end, so that
			 * we correctly handle IPv6 addresses for the host
			 * part.
			 */
			if ((colon = strrchr (mnt->device,':')) != NULL
			    && colon[1] == '/')
				strip_slashes (colon + 2);
			else
				strip_slashes (mnt->device);
565
		}
Scott James Remnant's avatar
Scott James Remnant committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
	}

	if (check >= 0)
		mnt->check = check;

	if (type) {
		if (mnt->type)
			nih_unref (mnt->type, mounts);
		mnt->type = NIH_MUST (nih_strdup (mounts, type));
	}

	if (opts) {
		if (mnt->opts)
			nih_unref (mnt->opts, mounts);
		mnt->opts = NIH_MUST (nih_strdup (mounts, opts));
	}


584
585
	nih_debug ("%s: %s %s %s %s%s",
		   MOUNT_NAME (mnt),
Scott James Remnant's avatar
Scott James Remnant committed
586
		   mnt->mountpoint,
587
588
589
		   mnt->device,
		   mnt->type,
		   mnt->opts,
590
		   mnt->check ? " check" : "");
Scott James Remnant's avatar
Scott James Remnant committed
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
}


int
has_option (Mount *     mnt,
	    const char *option,
	    int         current)
{
	const char *opts;
	size_t      i;

	nih_assert (mnt != NULL);
	nih_assert (option != NULL);

	opts = current ? mnt->mount_opts : mnt->opts;
	if (! opts)
		return FALSE;

	i = 0;
	while (i < strlen (opts)) {
		size_t j;
		size_t k;

		j = strcspn (opts + i, ",=");
		k = strcspn (opts + i + j, ",");

		if (! strncmp (opts + i, option, j))
			return TRUE;

		i += j + k + 1;
	}

	return FALSE;
}

char *
get_option (const void *parent,
	    Mount *     mnt,
	    const char *option,
	    int         current)
{
	const char *opts;
	size_t      i;

	nih_assert (mnt != NULL);
	nih_assert (option != NULL);

	opts = current ? mnt->mount_opts : mnt->opts;
	if (! opts)
		return NULL;

	i = 0;
	while (i < strlen (opts)) {
		size_t j;
		size_t k;

		j = strcspn (opts + i, ",=");
		k = strcspn (opts + i + j, ",");

		if (! strncmp (opts + i, option, j))
			return nih_strndup (parent, opts + i + j + 1,
					    k ? k - 1 : 0);

		i += j + k + 1;
	}

	return NULL;
}

char *
cut_options (const void *parent,
	     Mount *     mnt,
	     ...)
{
	va_list args;
	char *  opts;
	size_t  i;

	nih_assert (mnt != NULL);

	va_start (args, mnt);

	opts = NIH_MUST (nih_strdup (parent, mnt->opts));

	i = 0;
	while (i < strlen (opts)) {
		size_t      j;
		size_t      k;
		va_list     options;
		const char *option;

		j = strcspn (opts + i, ",=");
		k = strcspn (opts + i + j, ",");

		va_copy (options, args);
		while ((option = va_arg (options, const char *)) != NULL) {
687
			if (j && ! strncmp (opts + i, option, j))
Scott James Remnant's avatar
Scott James Remnant committed
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
				break;
		}
		va_end (options);

		if (option) {
			memmove (opts + (i ? i - 1 : 0), opts + i + j + k,
				 strlen (opts) - i - j - k + 1);
		} else {
			i += j + k + 1;
		}
	}

	va_end (args);

	return opts;
}


void
707
parse_fstab (const char *filename)
Scott James Remnant's avatar
Scott James Remnant committed
708
709
710
711
{
	FILE *         fstab;
	struct mntent *mntent;

712
713
	nih_assert (filename != NULL);

Scott James Remnant's avatar
Scott James Remnant committed
714
	nih_debug ("updating from %s", filename);
Scott James Remnant's avatar
Scott James Remnant committed
715

716
	fstab = setmntent (filename, "r");
Scott James Remnant's avatar
Scott James Remnant committed
717
	if (! fstab) {
718
		nih_fatal ("%s: %s", filename, strerror (errno));
719
		exit (EXIT_ERROR);
Scott James Remnant's avatar
Scott James Remnant committed
720
721
722
	}

	while ((mntent = getmntent (fstab)) != NULL) {
723
724
		Mount *         mnt;
		nih_local char *fsname = NULL;
Scott James Remnant's avatar
Scott James Remnant committed
725

726
727
728
729
730
731
732
733
734
		/* These are always symlinks into /run now.  Ignore any
		 * explicit attempts to mount them because it vastly
		 * complicates matters when trying to ensure that /run and
		 * /var/run point to the same place.
		 */
		if (! strcmp (mntent->mnt_dir, "/var/run") ||
		    ! strcmp (mntent->mnt_dir, "/var/lock"))
			continue;

Scott James Remnant's avatar
Scott James Remnant committed
735
		mnt = find_mount (mntent->mnt_dir);
736
737
		if (mnt
		    && strcmp (mntent->mnt_type, "swap")) {
Scott James Remnant's avatar
Scott James Remnant committed
738
739
740
741
			update_mount (mnt,
				      mntent->mnt_fsname,
				      mntent->mnt_passno != 0,
				      mntent->mnt_type,
742
				      mntent->mnt_opts);
Scott James Remnant's avatar
Scott James Remnant committed
743
744
745
746
747
		} else {
			mnt = new_mount (mntent->mnt_dir,
					 mntent->mnt_fsname,
					 mntent->mnt_passno != 0,
					 mntent->mnt_type,
748
					 mntent->mnt_opts);
Scott James Remnant's avatar
Scott James Remnant committed
749
750
751
752
753
754
		}
	}

	endmntent (fstab);
}

755
756
757
758
759
static int
needs_remount (Mount *mnt)
{
	nih_assert (mnt != NULL);

760
761
762
	if (mnt->mounted
	    && has_option (mnt, "ro", TRUE)
	    && (! has_option (mnt, "ro", FALSE))) {
763
764
765
766
767
768
		return TRUE;
	} else {
		return FALSE;
	}
}

769
770
771
772
773
774
775
776
777
778
779
static void
mount_proc (void)
{
	Mount *mnt;

	nih_debug ("mounting /proc");
	if (mount ("none", "/proc", "proc",
		   MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL) < 0) {
		nih_fatal ("%s: %s: %s", "/proc",
			   _("unable to mount"),
			   strerror (errno));
780
		exit (EXIT_ERROR);
781
782
783
784
785
786
787
	}

	mnt = find_mount ("/proc");
	if (mnt)
		mnt->mounted = TRUE;
}

788
789
static FILE *mountinfo = NULL;

790
static void
791
parse_mountinfo_file (int reparsed)
Scott James Remnant's avatar
Scott James Remnant committed
792
793
794
795
{
	nih_local char *buf = NULL;
	size_t          bufsz;

796
	nih_assert (mountinfo != NULL);
Scott James Remnant's avatar
Scott James Remnant committed
797

798
799
800
	if (reparsed)
		rewind (mountinfo);

801
	nih_debug ("updating mounts");
Scott James Remnant's avatar
Scott James Remnant committed
802
803
804
805
806
807
808

	bufsz = 4096;
	buf = NIH_MUST (nih_alloc (NULL, bufsz));

	while (fgets (buf, bufsz, mountinfo) != NULL) {
		char * saveptr;
		char * ptr;
809
		char * dev;
Scott James Remnant's avatar
Scott James Remnant committed
810
811
812
		char * mountpoint;
		char * type;
		char * device;
813
		int    maj, min;
Scott James Remnant's avatar
Scott James Remnant committed
814
815
		char * mount_opts;
		char * super_opts;
816
		char * opts;
Scott James Remnant's avatar
Scott James Remnant committed
817
818
819
820
		Mount *mnt;

		while ((! strchr (buf, '\n')) && (! feof (mountinfo))) {
			buf = NIH_MUST (nih_realloc (buf, NULL, bufsz + 4096));
Scott James Remnant's avatar
Scott James Remnant committed
821
822
			if (! fgets (buf + bufsz - 1, 4097, mountinfo))
				break;
Scott James Remnant's avatar
Scott James Remnant committed
823
824
825
826
827
828
829
830
831
832
833
834
835
836
			bufsz += 4096;
		}

		/* mount ID */
		ptr = strtok_r (buf, " \t\n", &saveptr);
		if (! ptr)
			continue;

		/* parent ID */
		ptr = strtok_r (NULL, " \t\n", &saveptr);
		if (! ptr)
			continue;

		/* major:minor */
837
838
		dev = strtok_r (NULL, " \t\n", &saveptr);
		if (! dev)
Scott James Remnant's avatar
Scott James Remnant committed
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
			continue;

		/* root */
		ptr = strtok_r (NULL, " \t\n", &saveptr);
		if (! ptr)
			continue;

		/* mountpoint */
		mountpoint = strtok_r (NULL, " \t\n", &saveptr);
		if (! mountpoint)
			continue;

		/* mount opts */
		mount_opts = strtok_r (NULL, " \t\n", &saveptr);
		if (! mount_opts)
			continue;

		/* optional fields */
		while (((ptr = strtok_r (NULL, " \t\n", &saveptr)) != NULL)
		       && strcmp (ptr, "-"))
			;
		if (! ptr)
			continue;

		/* type */
		type = strtok_r (NULL, " \t\n", &saveptr);
		if (! type)
			continue;

		/* device */
		device = strtok_r (NULL, " \t\n", &saveptr);
		if (! device)
			continue;

		/* superblock opts */
		super_opts = strtok_r (NULL, " \t\n", &saveptr);
		if (! super_opts)
			continue;

878
879
880
		/* ignore ureadahead's mounting of debugfs */
		if (! strcmp (mountpoint, "/var/lib/ureadahead/debugfs"))
			continue;
Scott James Remnant's avatar
Scott James Remnant committed
881

882
883
884
		opts = NIH_MUST (nih_sprintf (mounts, "%s,%s",
					      mount_opts, super_opts));

Scott James Remnant's avatar
Scott James Remnant committed
885
		mnt = find_mount (mountpoint);
886
887
		if (mnt
		    && strcmp (type, "swap")) {
888
889
890

			int needed_remount = needs_remount (mnt);

891
892
893
894
895
896
897
			if (! strcmp (type, "rootfs"))
				type = NULL;

			if ((! strcmp (device, "/dev/root"))
			    || (! strcmp (device, "none")))
				device = NULL;

898
			update_mount (mnt, device, -1, type, NULL);
Scott James Remnant's avatar
Scott James Remnant committed
899
900
901

			if (mnt->mount_opts)
				nih_unref (mnt->mount_opts, mounts);
902
			mnt->mount_opts = opts;
903
904
905
906
907

			/* If remounted, treat as newly done mount below. */
			if (needed_remount && !needs_remount (mnt))
				mnt->mounted = FALSE;

Scott James Remnant's avatar
Scott James Remnant committed
908
		} else {
909
910
			mnt = new_mount (mountpoint, device, FALSE, type, opts);
			mnt->mount_opts = opts;
Scott James Remnant's avatar
Scott James Remnant committed
911
912
		}

913
914
915
		if (sscanf (dev, "%d:%d", &maj, &min) == 2)
			mnt->mounted_dev = makedev (maj, min);

916
917
918
919
920
921
922
		if (mnt->mounted)
			continue;

		/* Fully update mounted state if reparsed.
		 * If invoked first time, just set the flag and leave complete
		 * state update (including event trigger) to mark_mounted ().
		 */
923
		if (reparsed && ! mnt->pending_call) {
924
			mounted (mnt);
925
926
927
		} else {
			mnt->mounted = TRUE;
		}
Scott James Remnant's avatar
Scott James Remnant committed
928
	}
929
930
931
932
933
934
935
}

static void
mountinfo_watcher (void *      data,
		   NihIoWatch *watch,
		   NihIoEvents events)
{
936
	nih_assert (mountinfo != NULL);
937
938

	nih_debug ("mountinfo changed, reparsing");
939
	parse_mountinfo_file (TRUE);
940
941
942
943
944
}

void
parse_mountinfo (void)
{
945
946
947
	if (mountinfo) {
		parse_mountinfo_file (TRUE);
	} else {
948
		mountinfo = fopen ("/proc/self/mountinfo", "r");
949
950
951
952
953
954
955
		if ((! mountinfo) && (errno == ENOENT)) {
			mount_proc ();
			mountinfo = fopen ("/proc/self/mountinfo", "r");
		}
		if (! mountinfo) {
			nih_fatal ("%s: %s", "/proc/self/mountinfo",
				   strerror (errno));
956
			exit (EXIT_ERROR);
957
		}
958

959
		parse_mountinfo_file (FALSE);
960

961
962
963
		NIH_MUST (nih_io_add_watch (NULL, fileno (mountinfo), NIH_IO_EXCEPT,
					    mountinfo_watcher, NULL));
	}
Scott James Remnant's avatar
Scott James Remnant committed
964
965
}

966

Scott James Remnant's avatar
Scott James Remnant committed
967
968
969
970
971
972
973
974
975
976
void
parse_filesystems (void)
{
	FILE *          fs;
	nih_local char *buf = NULL;
	size_t          bufsz;

	nih_debug ("reading filesystems");

	fs = fopen ("/proc/filesystems", "r");
977
978
979
980
	if ((! fs) && (errno == ENOENT)) {
		mount_proc ();
		fs = fopen ("/proc/filesystems", "r");
	}
Scott James Remnant's avatar
Scott James Remnant committed
981
982
983
	if (! fs) {
		nih_fatal ("%s: %s", "/proc/filesystems",
			   strerror (errno));
984
		exit (EXIT_ERROR);
Scott James Remnant's avatar
Scott James Remnant committed
985
986
987
988
989
990
991
992
993
994
995
996
	}

	bufsz = 4096;
	buf = NIH_MUST (nih_alloc (NULL, bufsz));

	while (fgets (buf, bufsz, fs) != NULL) {
		char *      ptr;
		int         nodev;
		Filesystem *filesystem;

		while (((ptr = strchr (buf, '\n')) == NULL) && (! feof (fs))) {
			buf = NIH_MUST (nih_realloc (buf, NULL, bufsz + 4096));
Scott James Remnant's avatar
Scott James Remnant committed
997
998
			if (! fgets (buf + bufsz - 1, 4097, fs))
				break;
Scott James Remnant's avatar
Scott James Remnant committed
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
			bufsz += 4096;
		}

		*ptr = '\0';
		if (! strncmp (buf, "nodev\t", 6)) {
			nodev = TRUE;
			ptr = buf + 6;
			nih_debug ("%s (nodev)", ptr);
		} else if (buf[0] == '\t') {
			nodev = FALSE;
			ptr = buf + 1;
			nih_debug ("%s", ptr);
		} else
			continue;

		filesystems = NIH_MUST (nih_realloc (filesystems, NULL,
						     sizeof (Filesystem) * (num_filesystems + 1)));
		filesystem = &filesystems[num_filesystems++];

		filesystem->name = NIH_MUST (nih_strdup (filesystems, ptr));
		filesystem->nodev = nodev;
	}

	if (fclose (fs) < 0) {
		nih_fatal ("%s: %s", "/proc/filesystems",
			  strerror (errno));
1025
		exit (EXIT_ERROR);
Scott James Remnant's avatar
Scott James Remnant committed
1026
1027
1028
	}
}

1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053

static int
is_parent (char *root,
	   char *path)
{
	size_t len;

	nih_assert (root != NULL);
	nih_assert (path != NULL);

	len = strlen (root);
	if ((! strncmp (path, root, len))
	    && ((path[len] == '\0')
		|| (path[len] == '/')
		|| (len && path[len-1] == '/')))
		return TRUE;

	return FALSE;
}

static int
is_remote (Mount *mnt)
{
	nih_assert (mnt != NULL);

1054
1055
1056
1057
1058
1059
1060
1061
1062
	if (has_option (mnt, "_netdev", FALSE)
	    || (! strcmp (mnt->type, "nfs"))
	    || (! strcmp (mnt->type, "nfs4"))
	    || (! strcmp (mnt->type, "smbfs"))
	    || (! strcmp (mnt->type, "cifs"))
	    || (! strcmp (mnt->type, "coda"))
	    || (! strcmp (mnt->type, "ncp"))
	    || (! strcmp (mnt->type, "ncpfs"))
	    || (! strcmp (mnt->type, "ocfs2"))
1063
	    || (! strcmp (mnt->type, "gfs"))
1064
1065
	    || (! strcmp (mnt->type, "gfs2"))
	    || (! strcmp (mnt->type, "ceph"))) {
1066
1067
		return TRUE;
	} else {
1068
		return FALSE;
1069
1070
1071
	}
}

Scott James Remnant's avatar
Scott James Remnant committed
1072
void
1073
mount_policy (void)
Scott James Remnant's avatar
Scott James Remnant committed
1074
{
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
	nih_local NihList  *already_mounted = NULL;
	NihListEntry       *entry;

	/* Build a list of mountpoints of already mounted
	 * file systems. Used later to avoid hiding existing
	 * mounted file systems.
	 */
	already_mounted = NIH_MUST (nih_list_new (NULL));
 	NIH_LIST_FOREACH (mounts, iter) {
		Mount *mnt = (Mount *)iter;
		if (mnt->mountpoint && mnt->mounted) {
			entry = NIH_MUST (nih_list_entry_new (already_mounted));
			entry->str = NIH_MUST (nih_strdup (entry, mnt->mountpoint));
			nih_list_add (already_mounted, &entry->entry);
		}
	}

1092
 	NIH_LIST_FOREACH_SAFE (mounts, iter) {
1093
		Mount *mnt = (Mount *)iter;
1094
		size_t j;
1095

1096
1097
1098
1099
		/* Check through the known filesystems, if this is a nodev
		 * filesystem then mark the mount as such so we don't wait
		 * for any device to be ready.
		 */
1100
1101
1102
1103
1104
		for (j = 0; j < num_filesystems; j++) {
			if ((! strcmp (mnt->type, filesystems[j].name))
			    && filesystems[j].nodev) {
				mnt->nodev = TRUE;
				break;
1105
			}
1106
		}
1107

1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
		/* If there's no device spec for this filesystem,
		 * and it's optional, we ignore it.
		 */
		if ((! strcmp (mnt->device, "none"))
		    && has_option (mnt, "optional", FALSE)
		    && (j == num_filesystems)) {
			nih_debug ("%s: dropping unknown filesystem",
				   MOUNT_NAME (mnt));
			nih_free (mnt);
			continue;
		}
1119

1120
1121
1122
		/* Otherwise if there's no device and the type is also "none",
		 * special-case it as a "nodev" virtual filesystem so our
		 * placeholder handling kicks in.
1123
1124
		 */
		if (! strcmp (mnt->device, "none")
1125
		    && ! strcmp (mnt->type, "none"))
1126
1127
			mnt->nodev = TRUE;

1128
1129
1130
1131
1132
1133
		/* Drop anything with ignore as its type. */
		if (! strcmp (mnt->type, "ignore")) {
			nih_debug ("%s: dropping ignored filesystem",
				   MOUNT_NAME (mnt));
			nih_free (mnt);
			continue;
1134
1135
1136
1137
1138
		}

		/* Drop anything that's not auto-mounted which isn't already
		 * mounted.
		 */
1139
1140
		if (has_option (mnt, "noauto", FALSE)
		    && (! mnt->mounted)) {
1141
			nih_debug ("%s: dropping noauto filesystem",
1142
				   MOUNT_NAME (mnt));
1143
			nih_free (mnt);
1144
			continue;
1145
		}
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162

		/* Check if our current mount entry would hide
		 * an existing mountpoint. If so, skip it.
		 */
		NIH_LIST_FOREACH (already_mounted, iter) {
			NihListEntry *e = (NihListEntry *)iter;
			if (mnt->mountpoint
			    && mnt->mountpoint != e->str
			    && ! mnt->mounted
			    && is_parent (mnt->mountpoint, e->str)) {
				nih_debug (
				    "%s: dropping filesystem because it has an already mounted child (%s)\n",
				    mnt->mountpoint, e->str);
				nih_free (mnt);
				break;
			}
		}
1163
1164
1165
1166
1167
1168
	}

 	NIH_LIST_FOREACH (mounts, iter) {
		Mount *mnt = (Mount *)iter;
		Mount *mount_parent = NULL;
		Mount *device_parent = NULL;
Scott James Remnant's avatar
Scott James Remnant committed
1169

1170
1171
1172
1173
1174
1175
1176
1177
		/* Iterate through the list of mounts, we're looking for the
		 * parentof the mountpoint (e.g. /var/tmp's parent is /var)
		 * and the parent mountpoint of the device spec
		 * (e.g. /usr/loop.img's parent is /usr).
		 */
		NIH_LIST_FOREACH (mounts, iter) {
			Mount *other = (Mount *)iter;

1178
1179
			/* Skip this mount entry */
			if (other == mnt)
1180
1181
1182
				continue;

			/* Is this a parent of our mountpoint? */
1183
1184
			if (mnt->mountpoint
			    && other->mountpoint
1185
1186
1187
1188
1189
1190
1191
			    && is_parent (other->mountpoint, mnt->mountpoint)){
				if ((! mount_parent)
				    || (strlen (mount_parent->mountpoint) < strlen (other->mountpoint)))
					mount_parent = other;
			}

			/* Is this a parent mountpoint of our device? */
1192
1193
			if ((! mnt->nodev)
			    && other->mountpoint
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
			    && is_parent (other->mountpoint, mnt->device)){
				if ((! device_parent)
				    || (strlen (device_parent->mountpoint) < strlen (other->mountpoint)))
					device_parent = other;
			}
		}

		/* We nearly always depend on our mountpoint's parent, the only
		 * exceptions are swap devices (which aren't mounted),
		 * showthrough entries (which explicitly don't need to wait)
		 * and virtual filesystems directly under the root.
		 */
		if (mount_parent
1207
		    && strcmp (mnt->type, "swap")) {
1208
1209
1210
1211
1212
			if (has_option (mnt, "showthrough", FALSE)
			    && strcmp (mount_parent->mountpoint, "/")) {
				mount_parent->has_showthrough = TRUE;
				mnt->showthrough = mount_parent;
				nih_debug ("%s shows through parent %s",
1213
1214
					   MOUNT_NAME (mnt),
					   MOUNT_NAME (mount_parent));
1215
1216
				mount_parent = NULL;
			} else if ((! strcmp (mount_parent->mountpoint, "/"))
1217
1218
				   && mnt->nodev
				   && strcmp (mnt->type, "none")) {
1219
				nih_debug ("%s can be mounted while root readonly",
1220
					   MOUNT_NAME (mnt));
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
				mount_parent = NULL;
			} else {
				NihListEntry *dep;

				dep = NIH_MUST (nih_list_entry_new (mnt));
				dep->data = mount_parent;
				nih_ref (mount_parent, dep);

				nih_list_add (&mnt->deps, &dep->entry);
				nih_debug ("%s parent is %s",
1231
1232
					   MOUNT_NAME (mnt),
					   MOUNT_NAME (mount_parent));
Scott James Remnant's avatar
Scott James Remnant committed
1233
1234
1235
			}
		}

1236
1237
1238
		/* It's also a pretty good idea to wait for the mountpoint on
		 * which our device exists to appear first, assuming we have
		 * one and that it's a valid path.
Scott James Remnant's avatar
Scott James Remnant committed
1239
		 */
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
		if (device_parent
		    && (! mnt->nodev)
		    && (mnt->device[0] == '/')
		    && strncmp (mnt->device, "/dev/", 5)) {
			NihListEntry *dep;

			dep = NIH_MUST (nih_list_entry_new (mnt));
			dep->data = device_parent;
			nih_ref (device_parent, dep);

			nih_list_add (&mnt->deps, &dep->entry);
			nih_debug ("%s parent is %s (mount %s)",
1252
1253
				   MOUNT_NAME (mnt),
				   MOUNT_NAME (device_parent),
1254
				   mnt->device);
Scott James Remnant's avatar
Scott James Remnant committed
1255
1256
		}

1257
		/* Kernel filesystems can require rather more to mount than
1258
1259
		 * first appears, if a device spec has been given then we
		 * also wait for the previous fstab entry - whatever it was.
1260
1261
		 */
		if (mnt->nodev
1262
		    && strcmp (mnt->device, "none")
1263
		    && strcmp (mnt->device, mnt->type)
1264
		    && (mnt->entry.prev != mounts)
1265
		    && strcmp (((Mount *)mnt->entry.prev)->device, "none")) {
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
			NihListEntry *dep;
			Mount *       prior;

			prior = (Mount *)mnt->entry.prev;

			dep = NIH_MUST (nih_list_entry_new (mnt));
			dep->data = prior;
			nih_ref (prior, dep);

			nih_list_add (&mnt->deps, &dep->entry);
			nih_debug ("%s prior fstab entry %s",
1277
				   MOUNT_NAME (mnt), MOUNT_NAME (prior));
1278
1279
		}

1280
		/* Tag the filesystem so we know which event it blocks */
1281
1282
1283
1284
1285
1286
1287
1288
		tag_mount (mnt, TAG_UNKNOWN);
	}

	NIH_LIST_FOREACH (mounts, iter) {
		Mount *mnt = (Mount *)iter;

		switch (mnt->tag) {
		case TAG_VIRTUAL:
1289
			nih_info (_("%s is %s"), MOUNT_NAME (mnt), "virtual");
1290
1291
1292
			num_virtual++;
			break;
		case TAG_LOCAL:
1293
			nih_info (_("%s is %s"), MOUNT_NAME (mnt), "local");
1294
1295
			num_local++;
			break;
1296
1297
1298
1299
1300
		case TAG_TIMEOUT:
			nih_info (_("%s is %s"), MOUNT_NAME (mnt), "local with timeout");
			num_local++;
			num_timeout++;
			break;
1301
		case TAG_REMOTE:
1302
			nih_info (_("%s is %s"), MOUNT_NAME (mnt), "remote");
1303
1304
1305
			num_remote++;
			break;
		case TAG_SWAP:
1306
			nih_info (_("%s is %s"), MOUNT_NAME (mnt), "swap");
1307
1308
1309
			num_swap++;
			break;
		case TAG_NOWAIT:
1310
			nih_info (_("%s is %s"), MOUNT_NAME (mnt), "nowait");
1311
			break;
1312
1313
1314
		case TAG_SKIPPED:
			nih_info (_("%s is %s"), MOUNT_NAME (mnt), "skipped");
			break;
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
		default:
			nih_assert_not_reached ();
		}
	}
}

void
tag_mount (Mount *mnt,
	   Tag    tag)
{
	nih_assert (mnt != NULL);

	/* If no tag is given, work it out from the device type */
	if (tag == TAG_UNKNOWN) {
		if (! strcmp (mnt->type, "swap")) {
			tag = TAG_SWAP;
1331
		} else if (! strcmp (mnt->mountpoint, "/")) {
1332
1333
			nih_debug ("%s is root filesystem", MOUNT_NAME (mnt));
			tag = TAG_LOCAL;
1334
		} else if (is_remote (mnt)) {
1335
1336
1337
1338
1339
1340
			if (((! strcmp (mnt->mountpoint, "/usr"))
			     || (! strcmp (mnt->mountpoint, "/var"))
			     || (! strncmp (mnt->mountpoint, "/usr/", 5))
			     || (! strncmp (mnt->mountpoint, "/var/", 5))
			     || (has_option (mnt, "bootwait", FALSE)))
			    && ! has_option (mnt, "nobootwait", FALSE))
1341
			{
1342
				tag = TAG_REMOTE;
1343
			} else {
1344
				tag = TAG_NOWAIT;
1345
			}
1346
		} else if (mnt->nodev
1347
			   && strcmp (mnt->type, "fuse")) {
1348
1349
1350
1351
1352
			if (! has_option (mnt, "nobootwait", FALSE)) {
				tag = TAG_VIRTUAL;
			} else {
				tag = TAG_NOWAIT;
			}
Scott James Remnant's avatar