~ubuntu-branches/ubuntu/quantal/mountall/quantal

« back to all changes in this revision

Viewing changes to src/mountall.c

  • Committer: Scott James Remnant
  • Date: 2009-09-14 22:31:57 UTC
  • Revision ID: scott@netsplit.com-20090914223157-8tbjkr6qsi1pqe2r
Tags: 0.1.0
releasing version 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mountall
 
2
 *
 
3
 * Copyright © 2009 Canonical Ltd.
 
4
 * Author: Scott James Remnant <scott@netsplit.com>.
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License version 2, as
 
8
 * published by the Free Software Foundation.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
 */
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
# include <config.h>
 
22
#endif /* HAVE_CONFIG_H */
 
23
 
 
24
 
 
25
#include <libudev.h>
 
26
 
 
27
#include <sys/types.h>
 
28
#include <sys/stat.h>
 
29
#include <sys/wait.h>
 
30
#include <sys/mount.h>
 
31
#include <sys/utsname.h>
 
32
#include <sys/sendfile.h>
 
33
 
 
34
#include <ftw.h>
 
35
#include <grp.h>
 
36
#include <time.h>
 
37
#include <errno.h>
 
38
#include <fcntl.h>
 
39
#include <paths.h>
 
40
#include <stdio.h>
 
41
#include <limits.h>
 
42
#include <mntent.h>
 
43
#include <stdlib.h>
 
44
#include <string.h>
 
45
#include <syslog.h>
 
46
#include <unistd.h>
 
47
#include <fnmatch.h>
 
48
 
 
49
#include <nih/macros.h>
 
50
#include <nih/alloc.h>
 
51
#include <nih/string.h>
 
52
#include <nih/option.h>
 
53
#include <nih/child.h>
 
54
#include <nih/signal.h>
 
55
#include <nih/io.h>
 
56
#include <nih/main.h>
 
57
#include <nih/logging.h>
 
58
#include <nih/error.h>
 
59
 
 
60
#include <nih-dbus/dbus_connection.h>
 
61
#include <nih-dbus/dbus_proxy.h>
 
62
 
 
63
#include "dbus/upstart.h"
 
64
#include "com.ubuntu.Upstart.h"
 
65
 
 
66
 
 
67
/* FIXME
 
68
 *
 
69
 * fsck queuing
 
70
 * fsck progress (usplash activation)
 
71
 */
 
72
 
 
73
 
 
74
typedef struct mount Mount;
 
75
 
 
76
typedef int (*MountHook) (Mount *mnt);
 
77
 
 
78
struct mount {
 
79
        char *              mountpoint;
 
80
        int                 mountpoint_ready;
 
81
        pid_t               mount_pid;
 
82
        int                 mounted;
 
83
 
 
84
        char *              device;
 
85
        struct udev_device *udev_device;
 
86
        int                 check;
 
87
        pid_t               fsck_pid;
 
88
        int                 device_ready;
 
89
 
 
90
        char *              type;
 
91
        int                 virtual;
 
92
        char *              opts;
 
93
        char *              mount_opts;
 
94
 
 
95
        MountHook           hook;
 
96
};
 
97
 
 
98
typedef struct path {
 
99
        const char *path;
 
100
        Mount *     mnt;
 
101
        int         device;
 
102
} Path;
 
103
 
 
104
typedef struct filesystem {
 
105
        char *name;
 
106
        int   nodev;
 
107
} Filesystem;
 
108
 
 
109
typedef struct process {
 
110
        NihList       entry;
 
111
        Mount *       mnt;
 
112
        char * const *args;
 
113
        pid_t         pid;
 
114
        void (*handler) (Mount *mnt, pid_t pid, int status);
 
115
} Process;
 
116
 
 
117
 
 
118
enum exit {
 
119
        EXIT_OK,        /* Ok */
 
120
        EXIT_ERROR,     /* General/OS error */
 
121
        EXIT_FSCK,      /* Filesystem check failed */
 
122
        EXIT_MOUNT,     /* Failed to mount a filesystem */
 
123
        EXIT_REBOOT,    /* Require a reboot */
 
124
};
 
125
 
 
126
 
 
127
Mount *new_mount            (const char *mountpoint, const char *device,
 
128
                             int check, const char *type, const char *opts,
 
129
                             MountHook hook);
 
130
Mount *find_mount           (const char *mountpoint);
 
131
void   update_mount         (Mount *mnt, const char *device, int check,
 
132
                             const char *type, const char *opts,
 
133
                             MountHook hook);
 
134
 
 
135
int    has_option           (Mount *mnt, const char *option, int current);
 
136
char * get_option           (const void *parent, Mount *mnt, const char *option,
 
137
                             int current);
 
138
char * cut_options          (const void *parent, Mount *mnt, ...);
 
139
 
 
140
int    is_swap              (Mount *mnt);
 
141
int    is_virtual           (Mount *mnt);
 
142
int    is_remote            (Mount *mnt);
 
143
int    is_fhs               (Mount *mnt);
 
144
int    needs_remount        (Mount *mnt);
 
145
 
 
146
void   build_paths          (void);
 
147
int    path_compar          (const void *a, const void *b);
 
148
 
 
149
void   parse_fstab          (void);
 
150
void   parse_mountinfo      (void);
 
151
void   parse_filesystems    (void);
 
152
void   cleanup              (void);
 
153
 
 
154
void   device_ready         (Mount *mnt);
 
155
void   mountpoint_ready     (Mount *mnt);
 
156
void   mounted              (Mount *mnt);
 
157
void   children_ready       (Mount *mnt, int pass);
 
158
 
 
159
pid_t  spawn                (Mount *mnt, char * const *args, int wait,
 
160
                             void (*handler) (Mount *mnt, pid_t pid, int status));
 
161
void   spawn_child_handler  (Process *proc, pid_t pid,
 
162
                             NihChildEvents event, int status);
 
163
 
 
164
void   run_mount            (Mount *mnt, int fake);
 
165
void   run_mount_finished   (Mount *mnt, pid_t pid, int status);
 
166
 
 
167
void   run_swapon           (Mount *mnt);
 
168
void   run_swapon_finished  (Mount *mnt, pid_t pid, int status);
 
169
 
 
170
void   run_fsck             (Mount *mnt);
 
171
void   run_fsck_finished    (Mount *mnt, pid_t pid, int status);
 
172
 
 
173
void   write_mtab           (void);
 
174
 
 
175
int    has_showthrough      (Mount *root);
 
176
void   mount_showthrough    (Mount *root);
 
177
 
 
178
void   upstart_disconnected (DBusConnection *connection);
 
179
void   emit_event           (const char *name);
 
180
void   emit_event_error     (void *data, NihDBusMessage *message);
 
181
 
 
182
void   udev_monitor_watcher (struct udev_monitor *udev_monitor,
 
183
                             NihIoWatch *watch, NihIoEvents events);
 
184
void   usr1_handler         (void *data, NihSignal *signal);
 
185
 
 
186
int    dev_hook             (Mount *mnt);
 
187
int    tmp_hook             (Mount *mnt);
 
188
int    var_run_hook         (Mount *mnt);
 
189
 
 
190
 
 
191
static const struct {
 
192
        const char *mountpoint;
 
193
        const char *device;
 
194
        int         check;
 
195
        const char *type;
 
196
        const char *opts;
 
197
        MountHook   hook;
 
198
} builtins[] = {
 
199
        { "/",                        "/dev/root", TRUE,  "rootfs",      NULL,                              NULL         },
 
200
        { "/proc",                    NULL,        FALSE, "proc",        "nodev,noexec,nosuid",             NULL         },
 
201
        { "/proc/sys/fs/binfmt_misc", NULL,        FALSE, "binfmt_misc", NULL,                              NULL         },
 
202
        { "/sys",                     NULL,        FALSE, "sysfs",       "nodev,noexec,nosuid",             NULL         },
 
203
        { "/sys/fs/fuse/connections", NULL,        FALSE, "fusectl",     NULL,                              NULL         },
 
204
        { "/sys/kernel/debug",        NULL,        FALSE, "debugfs",     NULL,                              NULL         },
 
205
        { "/sys/kernel/security",     NULL,        FALSE, "securityfs",  NULL,                              NULL         },
 
206
        { "/spu",                     NULL,        FALSE, "spufs",       "gid=spu",                         NULL         },
 
207
        { "/dev",                     NULL,        FALSE, "tmpfs",       "mode=0755",                       dev_hook     },
 
208
        { "/dev/pts",                 NULL,        FALSE, "devpts",      "noexec,nosuid,gid=tty,mode=0620", NULL         },
 
209
        { "/dev/shm",                 NULL,        FALSE, "tmpfs",       "nosuid,nodev",                    NULL         },
 
210
        { "/tmp",                     NULL,        FALSE, NULL,          NULL,                              tmp_hook     },
 
211
        { "/var/run",                 NULL,        FALSE, "tmpfs",       "mode=0755,nosuid,showthrough",    var_run_hook },
 
212
        { "/var/lock",                NULL,        FALSE, "tmpfs",       "nodev,noexec,nosuid,showthrough", NULL         },
 
213
        { "/lib/init/rw",             NULL,        FALSE, "tmpfs",       "mode=0755,nosuid",                NULL         },
 
214
        { NULL }
 
215
}, *builtin;
 
216
 
 
217
static const char *fhs[] = {
 
218
        "/",
 
219
        "/boot",                        /* Often separate */
 
220
        "/dev",                         /* udev */
 
221
        "/dev/pts",                     /* Built-in */
 
222
        "/dev/shm",                     /* Built-in */
 
223
        "/home",
 
224
        "/lib/init/rw",                 /* Built-in */
 
225
        "/opt",
 
226
        "/proc",                        /* Linux appendix */
 
227
        "/proc/sys/fs/binfmt_misc",     /* Built-in */
 
228
        "/spu",                         /* Built-in */
 
229
        "/srv"
 
230
        "/sys",                         /* Not in FHS yet */
 
231
        "/sys/fs/fuse/connections",     /* Built-in */
 
232
        "/sys/kernel/debug",            /* Built-in */
 
233
        "/sys/kernel/security",         /* Built-in */
 
234
        "/tmp",
 
235
        "/usr",
 
236
        "/usr/local",
 
237
        "/usr/var",                     /* Recommendation for /var symlink */
 
238
        "/var",
 
239
        "/var/cache/man",
 
240
        "/var/cache/fonts",
 
241
        "/var/lib",
 
242
        "/var/lock",
 
243
        "/var/log",
 
244
        "/var/mail",
 
245
        "/var/opt",
 
246
        "/var/run",
 
247
        "/var/spool",
 
248
        "/var/tmp",
 
249
        "/var/yp",
 
250
        NULL
 
251
};
 
252
 
 
253
 
 
254
Mount *mounts = NULL;
 
255
size_t num_mounts = 0;
 
256
 
 
257
Path *paths = NULL;
 
258
size_t num_paths = 0;
 
259
 
 
260
Filesystem *filesystems = NULL;
 
261
size_t num_filesystems = 0;
 
262
 
 
263
NihList *procs = NULL;
 
264
 
 
265
int written_mtab = FALSE;
 
266
 
 
267
int exit_code = EXIT_OK;
 
268
 
 
269
/**
 
270
 * upstart:
 
271
 *
 
272
 * Proxy to Upstart daemon.
 
273
 **/
 
274
static NihDBusProxy *upstart = NULL;
 
275
 
 
276
 
 
277
/**
 
278
 * daemonise:
 
279
 *
 
280
 * Set to TRUE if we should become a daemon, rather than just running
 
281
 * in the foreground.
 
282
 **/
 
283
static int daemonise = FALSE;
 
284
 
 
285
/**
 
286
 * force_fsck:
 
287
 *
 
288
 * Set to TRUE if we should pass -f to fsck invocations.
 
289
 **/
 
290
static int force_fsck = FALSE;
 
291
 
 
292
/**
 
293
 * fsck_fix:
 
294
 *
 
295
 * Set to TRUE if we should pass -y to fsck invocations.
 
296
 **/
 
297
static int fsck_fix = FALSE;
 
298
 
 
299
/**
 
300
 * no_remote:
 
301
 *
 
302
 * Set to TRUE if we should ignore remote filesystems.
 
303
 **/
 
304
static int no_remote = FALSE;
 
305
 
 
306
/**
 
307
 * tmptime:
 
308
 *
 
309
 * Set to the number of hours grace files and directories in /tmp
 
310
 * are given before being removed.
 
311
 **/
 
312
static int tmptime = 0;
 
313
 
 
314
 
 
315
Mount *
 
316
new_mount (const char *mountpoint,
 
317
           const char *device,
 
318
           int         check,
 
319
           const char *type,
 
320
           const char *opts,
 
321
           MountHook   hook)
 
322
{
 
323
        Mount *mnt;
 
324
 
 
325
        nih_assert (mountpoint != NULL);
 
326
 
 
327
        mounts = NIH_MUST (nih_realloc (mounts, NULL,
 
328
                                        sizeof (Mount) * (num_mounts + 1)));
 
329
        mnt = &mounts[num_mounts++];
 
330
 
 
331
        mnt->mountpoint = NIH_MUST (nih_strdup (mounts, mountpoint));
 
332
        mnt->mountpoint_ready = FALSE;
 
333
        mnt->mount_pid = -1;
 
334
        mnt->mounted = FALSE;
 
335
 
 
336
        mnt->device = device ? NIH_MUST (nih_strdup (mounts, device)) : NULL;
 
337
        mnt->udev_device = NULL;
 
338
        mnt->check = check;
 
339
        mnt->fsck_pid = -1;
 
340
        mnt->device_ready = FALSE;
 
341
 
 
342
        mnt->type = type ? NIH_MUST (nih_strdup (mounts, type)) : NULL;
 
343
        mnt->virtual = FALSE;
 
344
        mnt->opts = opts ? NIH_MUST (nih_strdup (mounts, opts)) : NULL;
 
345
        mnt->mount_opts = NULL;
 
346
 
 
347
        mnt->hook = hook;
 
348
 
 
349
        nih_debug ("%s: %s %s %s%s%s",
 
350
                   mnt->mountpoint,
 
351
                   mnt->device ?: "-",
 
352
                   mnt->type ?: "-",
 
353
                   mnt->opts ?: "-",
 
354
                   mnt->check ? "*" : "",
 
355
                   mnt->hook ? "+" : "");
 
356
 
 
357
        return mnt;
 
358
}
 
359
 
 
360
Mount *
 
361
find_mount (const char *mountpoint)
 
362
{
 
363
        nih_assert (mountpoint != NULL);
 
364
 
 
365
        for (size_t i = 0; i < num_mounts; i++)
 
366
                if (! strcmp (mounts[i].mountpoint, mountpoint))
 
367
                        return &mounts[i];
 
368
 
 
369
        return NULL;
 
370
}
 
371
 
 
372
void
 
373
update_mount (Mount *     mnt,
 
374
              const char *device,
 
375
              int         check,
 
376
              const char *type,
 
377
              const char *opts,
 
378
              MountHook   hook)
 
379
{
 
380
        nih_assert (mnt != NULL);
 
381
 
 
382
        if (device) {
 
383
                if (mnt->device)
 
384
                        nih_unref (mnt->device, mounts);
 
385
                mnt->device = NIH_MUST (nih_strdup (mounts, device));
 
386
        }
 
387
 
 
388
        if (check >= 0)
 
389
                mnt->check = check;
 
390
 
 
391
        if (type) {
 
392
                /* Remove the current hook if we change the type */
 
393
                if ((! mnt->type)
 
394
                    || strcmp (type, mnt->type))
 
395
                        mnt->hook = NULL;
 
396
 
 
397
                if (mnt->type)
 
398
                        nih_unref (mnt->type, mounts);
 
399
                mnt->type = NIH_MUST (nih_strdup (mounts, type));
 
400
        }
 
401
 
 
402
        if (opts) {
 
403
                if (mnt->opts)
 
404
                        nih_unref (mnt->opts, mounts);
 
405
                mnt->opts = NIH_MUST (nih_strdup (mounts, opts));
 
406
        }
 
407
 
 
408
        if (hook)
 
409
                mnt->hook = hook;
 
410
 
 
411
        nih_debug ("%s: %s %s %s%s%s",
 
412
                   mnt->mountpoint,
 
413
                   mnt->device ?: "-",
 
414
                   mnt->type ?: "-",
 
415
                   mnt->opts ?: "-",
 
416
                   mnt->check ? "*" : "",
 
417
                   mnt->hook ? "+" : "");
 
418
}
 
419
 
 
420
 
 
421
int
 
422
has_option (Mount *     mnt,
 
423
            const char *option,
 
424
            int         current)
 
425
{
 
426
        const char *opts;
 
427
        size_t      i;
 
428
 
 
429
        nih_assert (mnt != NULL);
 
430
        nih_assert (option != NULL);
 
431
 
 
432
        opts = current ? mnt->mount_opts : mnt->opts;
 
433
        if (! opts)
 
434
                return FALSE;
 
435
 
 
436
        i = 0;
 
437
        while (i < strlen (opts)) {
 
438
                size_t j;
 
439
                size_t k;
 
440
 
 
441
                j = strcspn (opts + i, ",=");
 
442
                k = strcspn (opts + i + j, ",");
 
443
 
 
444
                if (! strncmp (opts + i, option, j))
 
445
                        return TRUE;
 
446
 
 
447
                i += j + k + 1;
 
448
        }
 
449
 
 
450
        return FALSE;
 
451
}
 
452
 
 
453
char *
 
454
get_option (const void *parent,
 
455
            Mount *     mnt,
 
456
            const char *option,
 
457
            int         current)
 
458
{
 
459
        const char *opts;
 
460
        size_t      i;
 
461
 
 
462
        nih_assert (mnt != NULL);
 
463
        nih_assert (option != NULL);
 
464
 
 
465
        opts = current ? mnt->mount_opts : mnt->opts;
 
466
        if (! opts)
 
467
                return NULL;
 
468
 
 
469
        i = 0;
 
470
        while (i < strlen (opts)) {
 
471
                size_t j;
 
472
                size_t k;
 
473
 
 
474
                j = strcspn (opts + i, ",=");
 
475
                k = strcspn (opts + i + j, ",");
 
476
 
 
477
                if (! strncmp (opts + i, option, j))
 
478
                        return nih_strndup (parent, opts + i + j + 1,
 
479
                                            k ? k - 1 : 0);
 
480
 
 
481
                i += j + k + 1;
 
482
        }
 
483
 
 
484
        return NULL;
 
485
}
 
486
 
 
487
char *
 
488
cut_options (const void *parent,
 
489
             Mount *     mnt,
 
490
             ...)
 
491
{
 
492
        va_list args;
 
493
        char *  opts;
 
494
        size_t  i;
 
495
 
 
496
        nih_assert (mnt != NULL);
 
497
 
 
498
        va_start (args, mnt);
 
499
 
 
500
        opts = NIH_MUST (nih_strdup (parent, mnt->opts));
 
501
 
 
502
        i = 0;
 
503
        while (i < strlen (opts)) {
 
504
                size_t      j;
 
505
                size_t      k;
 
506
                va_list     options;
 
507
                const char *option;
 
508
 
 
509
                j = strcspn (opts + i, ",=");
 
510
                k = strcspn (opts + i + j, ",");
 
511
 
 
512
                va_copy (options, args);
 
513
                while ((option = va_arg (options, const char *)) != NULL) {
 
514
                        if (! strncmp (opts + i, option, j))
 
515
                                break;
 
516
                }
 
517
                va_end (options);
 
518
 
 
519
                if (option) {
 
520
                        memmove (opts + (i ? i - 1 : 0), opts + i + j + k,
 
521
                                 strlen (opts) - i - j - k + 1);
 
522
                        if (i)
 
523
                                i--;
 
524
                } else {
 
525
                        i += j + k + 1;
 
526
                }
 
527
        }
 
528
 
 
529
        va_end (args);
 
530
 
 
531
        return opts;
 
532
}
 
533
 
 
534
 
 
535
int
 
536
is_swap (Mount *mnt)
 
537
{
 
538
        nih_assert (mnt != NULL);
 
539
 
 
540
        if (! mnt->type) {
 
541
                return FALSE;
 
542
        } else if (strcmp (mnt->type, "swap")) {
 
543
                return FALSE;
 
544
        } else if (! mnt->device) {
 
545
                return FALSE;
 
546
        } else {
 
547
                return TRUE;
 
548
        }
 
549
}
 
550
 
 
551
int
 
552
is_virtual (Mount *mnt)
 
553
{
 
554
        nih_assert (mnt != NULL);
 
555
 
 
556
        return mnt->virtual;
 
557
}
 
558
 
 
559
int
 
560
is_remote (Mount *mnt)
 
561
{
 
562
        nih_assert (mnt != NULL);
 
563
 
 
564
        if (has_option (mnt, "_netdev", FALSE)) {
 
565
                return TRUE;
 
566
        } else if (! mnt->type) {
 
567
                return FALSE;
 
568
        } else if (strcmp (mnt->type, "nfs")
 
569
                   && strcmp (mnt->type, "nfs4")
 
570
                   && strcmp (mnt->type, "smbfs")
 
571
                   && strcmp (mnt->type, "cifs")
 
572
                   && strcmp (mnt->type, "coda")
 
573
                   && strcmp (mnt->type, "ncp")
 
574
                   && strcmp (mnt->type, "ncpfs")
 
575
                   && strcmp (mnt->type, "ocfs2")
 
576
                   && strcmp (mnt->type, "gfs")) {
 
577
                return FALSE;
 
578
        } else {
 
579
                return TRUE;
 
580
        }
 
581
}
 
582
 
 
583
int
 
584
is_fhs (Mount *mnt)
 
585
{
 
586
        nih_assert (mnt != NULL);
 
587
 
 
588
        for (const char * const *path = fhs; path && *path; path++)
 
589
                if (! strcmp (*path, mnt->mountpoint))
 
590
                        return TRUE;
 
591
 
 
592
        return FALSE;
 
593
}
 
594
 
 
595
int
 
596
needs_remount (Mount *mnt)
 
597
{
 
598
        nih_assert (mnt != NULL);
 
599
 
 
600
        if (mnt->mounted && has_option (mnt, "ro", TRUE)) {
 
601
                return TRUE;
 
602
        } else {
 
603
                return FALSE;
 
604
        }
 
605
}
 
606
 
 
607
 
 
608
void
 
609
build_paths (void)
 
610
{
 
611
        for (size_t i = 0; i < num_mounts; i++) {
 
612
                Path *path;
 
613
 
 
614
                if (mounts[i].mountpoint[0] == '/') {
 
615
                        paths = NIH_MUST (nih_realloc (paths, NULL,
 
616
                                                       sizeof (Path) * (num_paths + 1)));
 
617
                        path = &paths[num_paths++];
 
618
 
 
619
                        path->path = mounts[i].mountpoint;
 
620
                        path->mnt = &mounts[i];
 
621
                        path->device = FALSE;
 
622
                }
 
623
 
 
624
                if (mounts[i].device
 
625
                    && strncmp (mounts[i].device, "/dev/", 5)
 
626
                    && (! mounts[i].virtual)) {
 
627
                        paths = NIH_MUST (nih_realloc (paths, NULL,
 
628
                                                       sizeof (Path) * (num_paths + 1)));
 
629
                        path = &paths[num_paths++];
 
630
 
 
631
                        path->path = mounts[i].device;
 
632
                        path->mnt = &mounts[i];
 
633
                        path->device = TRUE;
 
634
                }
 
635
        }
 
636
 
 
637
        qsort (paths, num_paths, sizeof (Path),
 
638
               path_compar);
 
639
}
 
640
 
 
641
int
 
642
path_compar (const void *a,
 
643
             const void *b)
 
644
{
 
645
        const Path *path_a;
 
646
        const Path *path_b;
 
647
        int         ret;
 
648
 
 
649
        path_a = a;
 
650
        path_b = b;
 
651
 
 
652
        ret = strcmp (path_a->path, path_b->path);
 
653
        if (ret) {
 
654
                return ret;
 
655
        } else if (path_a->device && (! path_b->device)) {
 
656
                return 1;
 
657
        } else if (path_b->device && (! path_a->device)) {
 
658
                return -1;
 
659
        } else {
 
660
                return 0;
 
661
        }
 
662
}
 
663
 
 
664
 
 
665
void
 
666
parse_fstab (void)
 
667
{
 
668
        FILE *         fstab;
 
669
        struct mntent *mntent;
 
670
 
 
671
        nih_debug ("updating mounts");
 
672
 
 
673
        fstab = setmntent (_PATH_MNTTAB, "r");
 
674
        if (! fstab) {
 
675
                nih_fatal ("%s: %s", _PATH_MNTTAB, strerror (errno));
 
676
                exit (EXIT_ERROR);
 
677
        }
 
678
 
 
679
        while ((mntent = getmntent (fstab)) != NULL) {
 
680
                Mount *mnt;
 
681
 
 
682
                mnt = find_mount (mntent->mnt_dir);
 
683
                if (mnt) {
 
684
                        update_mount (mnt,
 
685
                                      mntent->mnt_fsname,
 
686
                                      mntent->mnt_passno != 0,
 
687
                                      mntent->mnt_type,
 
688
                                      mntent->mnt_opts,
 
689
                                      NULL);
 
690
                } else {
 
691
                        mnt = new_mount (mntent->mnt_dir,
 
692
                                         mntent->mnt_fsname,
 
693
                                         mntent->mnt_passno != 0,
 
694
                                         mntent->mnt_type,
 
695
                                         mntent->mnt_opts,
 
696
                                         NULL);
 
697
                }
 
698
        }
 
699
 
 
700
        endmntent (fstab);
 
701
}
 
702
 
 
703
void
 
704
parse_mountinfo (void)
 
705
{
 
706
        FILE *          mountinfo;
 
707
        nih_local char *buf = NULL;
 
708
        size_t          bufsz;
 
709
 
 
710
        nih_debug ("updating mounts");
 
711
 
 
712
        mountinfo = fopen ("/proc/self/mountinfo", "r");
 
713
        if ((! mountinfo) && (errno == ENOENT)) {
 
714
                Mount *mnt;
 
715
 
 
716
                nih_debug ("mounting /proc");
 
717
                if (mount ("none", "/proc", "proc",
 
718
                           MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL) < 0) {
 
719
                        nih_fatal ("%s: %s: %s", "/proc",
 
720
                                   _("unable to mount"),
 
721
                                   strerror (errno));
 
722
                        exit (EXIT_MOUNT);
 
723
                }
 
724
 
 
725
                mnt = find_mount ("/proc");
 
726
                if (mnt)
 
727
                        mnt->mounted = 1;
 
728
 
 
729
                mountinfo = fopen ("/proc/self/mountinfo", "r");
 
730
        }
 
731
        if (! mountinfo) {
 
732
                nih_fatal ("%s: %s", "/proc/self/mountinfo",
 
733
                           strerror (errno));
 
734
                exit (EXIT_ERROR);
 
735
        }
 
736
 
 
737
        bufsz = 4096;
 
738
        buf = NIH_MUST (nih_alloc (NULL, bufsz));
 
739
 
 
740
        while (fgets (buf, bufsz, mountinfo) != NULL) {
 
741
                char * saveptr;
 
742
                char * ptr;
 
743
                char * mountpoint;
 
744
                char * type;
 
745
                char * device;
 
746
                char * mount_opts;
 
747
                char * super_opts;
 
748
                Mount *mnt;
 
749
 
 
750
                while ((! strchr (buf, '\n')) && (! feof (mountinfo))) {
 
751
                        buf = NIH_MUST (nih_realloc (buf, NULL, bufsz + 4096));
 
752
                        fgets (buf + bufsz - 1, 4097, mountinfo);
 
753
                        bufsz += 4096;
 
754
                }
 
755
 
 
756
                /* mount ID */
 
757
                ptr = strtok_r (buf, " \t\n", &saveptr);
 
758
                if (! ptr)
 
759
                        continue;
 
760
 
 
761
                /* parent ID */
 
762
                ptr = strtok_r (NULL, " \t\n", &saveptr);
 
763
                if (! ptr)
 
764
                        continue;
 
765
 
 
766
                /* major:minor */
 
767
                ptr = strtok_r (NULL, " \t\n", &saveptr);
 
768
                if (! ptr)
 
769
                        continue;
 
770
 
 
771
                /* root */
 
772
                ptr = strtok_r (NULL, " \t\n", &saveptr);
 
773
                if (! ptr)
 
774
                        continue;
 
775
 
 
776
                /* mountpoint */
 
777
                mountpoint = strtok_r (NULL, " \t\n", &saveptr);
 
778
                if (! mountpoint)
 
779
                        continue;
 
780
 
 
781
                /* mount opts */
 
782
                mount_opts = strtok_r (NULL, " \t\n", &saveptr);
 
783
                if (! mount_opts)
 
784
                        continue;
 
785
 
 
786
                /* optional fields */
 
787
                while (((ptr = strtok_r (NULL, " \t\n", &saveptr)) != NULL)
 
788
                       && strcmp (ptr, "-"))
 
789
                        ;
 
790
                if (! ptr)
 
791
                        continue;
 
792
 
 
793
                /* type */
 
794
                type = strtok_r (NULL, " \t\n", &saveptr);
 
795
                if (! type)
 
796
                        continue;
 
797
                if (! strcmp (type, "rootfs"))
 
798
                        type = NULL;
 
799
 
 
800
                /* device */
 
801
                device = strtok_r (NULL, " \t\n", &saveptr);
 
802
                if (! device)
 
803
                        continue;
 
804
                if (! strcmp (device, "/dev/root"))
 
805
                        device = NULL;
 
806
 
 
807
                /* superblock opts */
 
808
                super_opts = strtok_r (NULL, " \t\n", &saveptr);
 
809
                if (! super_opts)
 
810
                        continue;
 
811
 
 
812
 
 
813
                mnt = find_mount (mountpoint);
 
814
                if (mnt) {
 
815
                        update_mount (mnt, device, -1, type, NULL, NULL);
 
816
 
 
817
                        if (mnt->mount_opts)
 
818
                                nih_unref (mnt->mount_opts, mounts);
 
819
                } else {
 
820
                        mnt = new_mount (mountpoint, device, FALSE, type, NULL, NULL);
 
821
                }
 
822
 
 
823
                mnt->mounted = 1;
 
824
                mnt->mount_opts = NIH_MUST (nih_sprintf (mounts, "%s,%s",
 
825
                                                         mount_opts, super_opts));
 
826
        }
 
827
 
 
828
        if (fclose (mountinfo) < 0) {
 
829
                nih_fatal ("%s: %s", "/proc/self/mountinfo",
 
830
                           strerror (errno));
 
831
                exit (EXIT_ERROR);
 
832
        }
 
833
}
 
834
 
 
835
void
 
836
parse_filesystems (void)
 
837
{
 
838
        FILE *          fs;
 
839
        nih_local char *buf = NULL;
 
840
        size_t          bufsz;
 
841
 
 
842
        nih_debug ("reading filesystems");
 
843
 
 
844
        fs = fopen ("/proc/filesystems", "r");
 
845
        if (! fs) {
 
846
                nih_fatal ("%s: %s", "/proc/filesystems",
 
847
                           strerror (errno));
 
848
                exit (EXIT_ERROR);
 
849
        }
 
850
 
 
851
        bufsz = 4096;
 
852
        buf = NIH_MUST (nih_alloc (NULL, bufsz));
 
853
 
 
854
        while (fgets (buf, bufsz, fs) != NULL) {
 
855
                char *      ptr;
 
856
                int         nodev;
 
857
                Filesystem *filesystem;
 
858
 
 
859
                while (((ptr = strchr (buf, '\n')) == NULL) && (! feof (fs))) {
 
860
                        buf = NIH_MUST (nih_realloc (buf, NULL, bufsz + 4096));
 
861
                        fgets (buf + bufsz - 1, 4097, fs);
 
862
                        bufsz += 4096;
 
863
                }
 
864
 
 
865
                *ptr = '\0';
 
866
                if (! strncmp (buf, "nodev\t", 6)) {
 
867
                        nodev = TRUE;
 
868
                        ptr = buf + 6;
 
869
                        nih_debug ("%s (nodev)", ptr);
 
870
                } else if (buf[0] == '\t') {
 
871
                        nodev = FALSE;
 
872
                        ptr = buf + 1;
 
873
                        nih_debug ("%s", ptr);
 
874
                } else
 
875
                        continue;
 
876
 
 
877
                filesystems = NIH_MUST (nih_realloc (filesystems, NULL,
 
878
                                                     sizeof (Filesystem) * (num_filesystems + 1)));
 
879
                filesystem = &filesystems[num_filesystems++];
 
880
 
 
881
                filesystem->name = NIH_MUST (nih_strdup (filesystems, ptr));
 
882
                filesystem->nodev = nodev;
 
883
        }
 
884
 
 
885
        if (fclose (fs) < 0) {
 
886
                nih_fatal ("%s: %s", "/proc/filesystems",
 
887
                          strerror (errno));
 
888
                exit (EXIT_ERROR);
 
889
        }
 
890
}
 
891
 
 
892
void
 
893
cleanup (void)
 
894
{
 
895
        for (size_t i = 0; i < num_mounts; i++) {
 
896
                int    drop = FALSE;
 
897
                size_t j;
 
898
 
 
899
                /* Check through the known filesystems, if this is a virtual
 
900
                 * filesystem then mark it as such so we don't wait for any
 
901
                 * device to be ready.  Otherwise if we didn't find the
 
902
                 * filesystem type, and no device is specified, drop as this
 
903
                 * is platform-dependent (e.g. spufs).
 
904
                 */
 
905
                if (mounts[i].type) {
 
906
                        for (j = 0; j < num_filesystems; j++)
 
907
                                if (! strcmp (mounts[i].type, filesystems[j].name))
 
908
                                        break;
 
909
 
 
910
                        if (j < num_filesystems) {
 
911
                                if (filesystems[j].nodev && (! is_remote (&mounts[i])))
 
912
                                        mounts[i].virtual = TRUE;
 
913
                        } else if ((! mounts[i].device) && (! mounts[i].mounted)) {
 
914
                                nih_debug ("%s: dropping unknown filesystem",
 
915
                                           mounts[i].mountpoint);
 
916
                                drop = TRUE;
 
917
                        } else if (! strcmp (mounts[i].type, "ignore")) {
 
918
                                nih_debug ("%s: dropping ignored filesystem",
 
919
                                           mounts[i].mountpoint);
 
920
                                drop = TRUE;
 
921
                        }
 
922
                }
 
923
 
 
924
                /* Drop anything that's not auto-mounted which isn't already
 
925
                 * mounted.
 
926
                 */
 
927
                if (has_option (&mounts[i], "noauto", FALSE) && (! mounts[i].mounted)) {
 
928
                        nih_debug ("%s: dropping noauto filesystem",
 
929
                                   mounts[i].mountpoint);
 
930
                        drop = TRUE;
 
931
                }
 
932
 
 
933
                if (drop) {
 
934
                        memmove (&mounts[i], &mounts[i+1],
 
935
                                 sizeof (Mount) * (num_mounts - i - 1));
 
936
                        num_mounts--;
 
937
                        i--;
 
938
                } else {
 
939
                        if (is_virtual (&mounts[i])) {
 
940
                                nih_debug ("virtual %s", mounts[i].mountpoint);
 
941
                        } else if (is_swap (&mounts[i])) {
 
942
                                nih_debug ("swap    %s", mounts[i].device);
 
943
                        } else if (is_remote (&mounts[i])) {
 
944
                                nih_debug ("remote  %s", mounts[i].mountpoint);
 
945
                        } else {
 
946
                                nih_debug ("local   %s", mounts[i].mountpoint);
 
947
                        }
 
948
 
 
949
                        if (is_fhs (&mounts[i]))
 
950
                                nih_debug ("FHS     %s", mounts[i].mountpoint);
 
951
                }
 
952
        }
 
953
}
 
954
 
 
955
 
 
956
void
 
957
device_ready (Mount *mnt)
 
958
{
 
959
        nih_assert (mnt != NULL);
 
960
        nih_assert (mnt->device != NULL);
 
961
        nih_assert (mnt->type != NULL);
 
962
 
 
963
        nih_debug ("%s", mnt->mountpoint);
 
964
 
 
965
        /* Activate swap devices as soon as the device is ready;
 
966
         * don't mount filesystems until the mountpoint is ready
 
967
         * as well.
 
968
         */
 
969
        mnt->device_ready = TRUE;
 
970
        if (is_swap (mnt)) {
 
971
                run_swapon (mnt);
 
972
        } else if (mnt->mountpoint_ready)
 
973
                run_mount (mnt, FALSE);
 
974
}
 
975
 
 
976
void
 
977
mountpoint_ready (Mount *mnt)
 
978
{
 
979
        nih_assert (mnt != NULL);
 
980
 
 
981
        nih_debug ("%s", mnt->mountpoint);
 
982
 
 
983
        /* Activate virtual filesystems as soon as the mountpoint
 
984
         * is ready, otherwise don't mount filesystems until the
 
985
         * device is ready.  Be sure to ignore swap partitions
 
986
         * since they're mounted in device_ready() itself.
 
987
         */
 
988
        mnt->mountpoint_ready = TRUE;
 
989
        if (is_virtual (mnt)
 
990
            || (! mnt->type)
 
991
            || (mnt->device_ready && (! is_swap (mnt))))
 
992
                run_mount (mnt, FALSE);
 
993
}
 
994
 
 
995
void
 
996
mounted (Mount *mnt)
 
997
{
 
998
        size_t     num_virtual = 0;
 
999
        size_t     num_virtual_mounted = 0;
 
1000
        static int virtual_triggered = FALSE;
 
1001
        size_t     num_swap = 0;
 
1002
        size_t     num_swap_mounted = 0;
 
1003
        static int swap_triggered = FALSE;
 
1004
        size_t     num_remote = 0;
 
1005
        size_t     num_remote_mounted = 0;
 
1006
        static int remote_triggered = FALSE;
 
1007
        size_t     num_local = 0;
 
1008
        size_t     num_local_mounted = 0;
 
1009
        static int local_triggered = FALSE;
 
1010
        size_t     num_fhs = 0;
 
1011
        size_t     num_fhs_mounted = 0;
 
1012
        static int fhs_triggered = FALSE;
 
1013
 
 
1014
        nih_assert (mnt != NULL);
 
1015
 
 
1016
        nih_debug ("%s", mnt->mountpoint);
 
1017
 
 
1018
        if (mnt->hook) {
 
1019
                if (mnt->hook (mnt) < 0) {
 
1020
                        exit_code = nih_max (exit_code, EXIT_ERROR);
 
1021
                        if (NIH_LIST_EMPTY (procs))
 
1022
                                exit (exit_code);
 
1023
                }
 
1024
        }
 
1025
 
 
1026
        mnt->mounted = 1;
 
1027
 
 
1028
        /* Any previous mount options no longer apply
 
1029
         * (ie. we're not read-only anymore)
 
1030
         */
 
1031
        if (mnt->mount_opts)
 
1032
                nih_unref (mnt->mount_opts, mounts);
 
1033
        mnt->mount_opts = NULL;
 
1034
 
 
1035
        if (! written_mtab)
 
1036
                write_mtab ();
 
1037
 
 
1038
        /* Look at the current table state */
 
1039
        for (size_t i = 0; i < num_mounts; i++) {
 
1040
                int mounted = FALSE;
 
1041
 
 
1042
                if (is_virtual (&mounts[i])) {
 
1043
                        num_virtual++;
 
1044
                        if (mounts[i].mounted) {
 
1045
                                num_virtual_mounted++;
 
1046
                                mounted = TRUE;
 
1047
                        }
 
1048
                } else if (is_swap (&mounts[i])) {
 
1049
                        num_swap++;
 
1050
                        if (mounts[i].mounted) {
 
1051
                                num_swap_mounted++;
 
1052
                                mounted = TRUE;
 
1053
                        }
 
1054
                } else if (is_remote (&mounts[i])) {
 
1055
                        num_remote++;
 
1056
                        if (mounts[i].mounted) {
 
1057
                                num_remote_mounted++;
 
1058
                                mounted = TRUE;
 
1059
                        }
 
1060
                } else {
 
1061
                        num_local++;
 
1062
                        if (mounts[i].mounted && (! needs_remount (&mounts[i]))) {
 
1063
                                num_local_mounted++;
 
1064
                                mounted = TRUE;
 
1065
                        }
 
1066
                }
 
1067
 
 
1068
                if (is_fhs (&mounts[i])) {
 
1069
                        num_fhs++;
 
1070
                        if (mounted)
 
1071
                                num_fhs_mounted++;
 
1072
                }
 
1073
        }
 
1074
 
 
1075
        nih_debug ("virtual %zi/%zi swap %zi/%zi remote %zi/%zi local %zi/%zi",
 
1076
                   num_virtual_mounted, num_virtual,
 
1077
                   num_swap_mounted, num_swap,
 
1078
                   num_remote_mounted, num_remote,
 
1079
                   num_local_mounted, num_local);
 
1080
 
 
1081
        if ((! virtual_triggered) && (num_virtual_mounted == num_virtual)) {
 
1082
                nih_info ("virtual filesystems finished");
 
1083
                emit_event ("virtual-filesystems");
 
1084
                virtual_triggered = TRUE;
 
1085
        }
 
1086
        if ((! swap_triggered) && (num_swap_mounted == num_swap)) {
 
1087
                nih_info ("swap finished");
 
1088
                emit_event ("all-swaps");
 
1089
                swap_triggered = TRUE;
 
1090
        }
 
1091
        if ((! remote_triggered) && (num_remote_mounted == num_remote)) {
 
1092
                nih_info ("remote finished");
 
1093
                emit_event ("remote-filesystems");
 
1094
                remote_triggered = TRUE;
 
1095
        }
 
1096
        if ((! local_triggered) && (num_local_mounted == num_local)) {
 
1097
                nih_info ("local finished");
 
1098
                emit_event ("local-filesystems");
 
1099
                local_triggered = TRUE;
 
1100
        }
 
1101
 
 
1102
        nih_debug ("fhs %zi/%zi", num_fhs_mounted, num_fhs);
 
1103
        if ((! fhs_triggered) && (num_fhs_mounted == num_fhs)) {
 
1104
                nih_info ("fhs mounted");
 
1105
                emit_event ("filesystem");
 
1106
                fhs_triggered = TRUE;
 
1107
        }
 
1108
 
 
1109
        if ((num_virtual_mounted == num_virtual)
 
1110
            && (num_swap_mounted == num_swap)
 
1111
            && (no_remote || (num_remote_mounted == num_remote))
 
1112
            && (num_local_mounted == num_local)) {
 
1113
                nih_info ("finished");
 
1114
                emit_event ("all-filesystems");
 
1115
                nih_main_loop_exit (EXIT_OK);
 
1116
        }
 
1117
 
 
1118
        /* Mount points underneath this are now ready for mounting,
 
1119
         * unless this was the root filesystem in which case they
 
1120
         * were already ready anyway so don't mark them again.
 
1121
         */
 
1122
        if (! is_swap (mnt)) {
 
1123
                if (strcmp (mnt->mountpoint, "/")) {
 
1124
                        children_ready (mnt, 0);
 
1125
                } else {
 
1126
                        children_ready (mnt, 2);
 
1127
                }
 
1128
        }
 
1129
}
 
1130
 
 
1131
void
 
1132
children_ready (Mount *root,
 
1133
                int    pass)
 
1134
{
 
1135
        size_t i = 0;
 
1136
 
 
1137
        nih_assert (root != NULL);
 
1138
 
 
1139
        while ((i < num_paths)
 
1140
               && ((paths[i].mnt != root) || paths[i].device))
 
1141
                i++;
 
1142
        nih_assert (i++ < num_paths);
 
1143
 
 
1144
        while ((i < num_paths)
 
1145
               && (! strncmp (paths[i].path, root->mountpoint,
 
1146
                              strlen (root->mountpoint)))
 
1147
               && ((! strcmp (root->mountpoint, "/"))
 
1148
                   || (paths[i].path[strlen (root->mountpoint)] == '/')
 
1149
                   || (paths[i].path[strlen (root->mountpoint)] == '\0'))) {
 
1150
                Path *      path;
 
1151
                struct stat statbuf;
 
1152
 
 
1153
                path = &paths[i++];
 
1154
                if (lstat (path->path, &statbuf) < 0) {
 
1155
                        nih_debug ("ignored %s: %s", path->path,
 
1156
                                   strerror (errno));
 
1157
                        continue;
 
1158
                }
 
1159
 
 
1160
                if (path->device) {
 
1161
                        /* Do not consider devices ready in the "mounted ro"
 
1162
                         * pass, only when writable.
 
1163
                         */
 
1164
                        if (pass != 1) {
 
1165
                                if ((S_ISREG (statbuf.st_mode)
 
1166
                                     || S_ISDIR (statbuf.st_mode))) {
 
1167
                                        device_ready (path->mnt);
 
1168
                                } else {
 
1169
                                        nih_debug ("ignored %s: not regular",
 
1170
                                                   path->path);
 
1171
                                }
 
1172
                        }
 
1173
                } else {
 
1174
                        /* If there is no type to mount, then this is a hook
 
1175
                         * placeholder and we want don't want to run it in
 
1176
                         * the initial "mounted ro" pass.  Otherwise
 
1177
                         * we don't don't want to run mount in the "remount"
 
1178
                         * pass.
 
1179
                         */
 
1180
                        if (pass != (path->mnt->type ? 2 : 1))
 
1181
                                mountpoint_ready (path->mnt);
 
1182
 
 
1183
                        /* Children of this mount point aren't ready until
 
1184
                         * this mountpoint is mounted, unless they're always
 
1185
                         * expected to show through.
 
1186
                         */
 
1187
                        while ((i < num_paths)
 
1188
                               && (! strncmp (paths[i].path, path->path,
 
1189
                                              strlen (path->path)))
 
1190
                               && ((paths[i].path[strlen (path->path)] == '/')
 
1191
                                   || (paths[i].path[strlen (path->path)] == '\0')))
 
1192
                        {
 
1193
                                if ((! paths[i].device)
 
1194
                                    && paths[i].mnt->type
 
1195
                                    && has_option (paths[i].mnt, "showthrough", FALSE)
 
1196
                                    && (lstat (paths[i].path, &statbuf) == 0)
 
1197
                                    && S_ISDIR (statbuf.st_mode)) {
 
1198
                                        mountpoint_ready (paths[i].mnt);
 
1199
                                } else {
 
1200
                                        nih_debug ("skipped %s", paths[i].path);
 
1201
                                }
 
1202
 
 
1203
                                i++;
 
1204
                        }
 
1205
                }
 
1206
        }
 
1207
}
 
1208
 
 
1209
 
 
1210
pid_t
 
1211
spawn (Mount *         mnt,
 
1212
       char * const *  args,
 
1213
       int             wait,
 
1214
       void (*handler) (Mount *mnt, pid_t pid, int status))
 
1215
{
 
1216
        pid_t    pid;
 
1217
        int      fds[2];
 
1218
        char     flag = '!';
 
1219
        Process *proc;
 
1220
 
 
1221
        nih_assert (mnt != NULL);
 
1222
        nih_assert (args != NULL);
 
1223
        nih_assert (args[0] != NULL);
 
1224
 
 
1225
        NIH_ZERO (pipe2 (fds, O_CLOEXEC));
 
1226
 
 
1227
        pid = fork ();
 
1228
        if (pid < 0) {
 
1229
                close (fds[0]);
 
1230
                close (fds[1]);
 
1231
 
 
1232
                nih_fatal ("%s %s: %s", args[0],
 
1233
                           is_swap (mnt) ? mnt->device : mnt->mountpoint,
 
1234
                           strerror (errno));
 
1235
                exit (EXIT_ERROR);
 
1236
        } else if (! pid) {
 
1237
                nih_local char *msg = NULL;
 
1238
 
 
1239
                for (char * const *arg = args; arg && *arg; arg++)
 
1240
                        NIH_MUST (nih_strcat_sprintf (&msg, NULL, msg ? " %s" : "%s", *arg));
 
1241
 
 
1242
                nih_debug ("%s", msg);
 
1243
 
 
1244
                execvp (args[0], args);
 
1245
                nih_fatal ("%s %s [%d]: %s", args[0],
 
1246
                           is_swap (mnt) ? mnt->device : mnt->mountpoint,
 
1247
                           getpid (), strerror (errno));
 
1248
                write (fds[1], &flag, 1);
 
1249
                exit (0);
 
1250
        } else {
 
1251
                int ret;
 
1252
 
 
1253
                close (fds[1]);
 
1254
                ret = read (fds[0], &flag, 1);
 
1255
                close (fds[0]);
 
1256
 
 
1257
                if (ret > 0) {
 
1258
                        exit_code = nih_max (exit_code, EXIT_ERROR);
 
1259
                        if (NIH_LIST_EMPTY (procs))
 
1260
                                exit (exit_code);
 
1261
 
 
1262
                        return -1;
 
1263
                }
 
1264
        }
 
1265
 
 
1266
        nih_debug ("%s %s [%d]", args[0],
 
1267
                   is_swap (mnt) ? mnt->device : mnt->mountpoint,
 
1268
                   pid);
 
1269
 
 
1270
        proc = NIH_MUST (nih_new (NULL, Process));
 
1271
        nih_list_init (&proc->entry);
 
1272
 
 
1273
        proc->mnt = mnt;
 
1274
 
 
1275
        proc->args = args;
 
1276
        if (! wait)
 
1277
                nih_ref (proc->args, proc);
 
1278
 
 
1279
        proc->pid = pid;
 
1280
 
 
1281
        proc->handler = handler;
 
1282
 
 
1283
        nih_list_add (procs, &proc->entry);
 
1284
 
 
1285
        if (wait) {
 
1286
                siginfo_t info;
 
1287
 
 
1288
                NIH_ZERO (waitid (P_PID, pid, &info, WEXITED));
 
1289
                spawn_child_handler (proc, pid, info.si_code == CLD_EXITED ? NIH_CHILD_EXITED : NIH_CHILD_KILLED,
 
1290
                                     info.si_status);
 
1291
                return 0;
 
1292
        } else {
 
1293
                NIH_MUST (nih_child_add_watch (NULL, pid, NIH_CHILD_EXITED|NIH_CHILD_KILLED|NIH_CHILD_DUMPED,
 
1294
                                               (NihChildHandler)spawn_child_handler, proc));
 
1295
                return pid;
 
1296
        }
 
1297
}
 
1298
 
 
1299
void
 
1300
spawn_child_handler (Process *      proc,
 
1301
                     pid_t          pid,
 
1302
                     NihChildEvents event,
 
1303
                     int            status)
 
1304
{
 
1305
        nih_assert (proc != NULL);
 
1306
        nih_assert (pid == proc->pid);
 
1307
 
 
1308
        nih_list_remove (&proc->entry);
 
1309
 
 
1310
        if (event != NIH_CHILD_EXITED) {
 
1311
                const char *sig;
 
1312
 
 
1313
                sig = nih_signal_to_name (status);
 
1314
                if (sig) {
 
1315
                        nih_fatal ("%s %s [%d] killed by %s signal", proc->args[0],
 
1316
                                   is_swap (proc->mnt) ? proc->mnt->device : proc->mnt->mountpoint,
 
1317
                                   pid, sig);
 
1318
                } else {
 
1319
                        nih_fatal ("%s %s [%d] killed by signal %d", proc->args[0],
 
1320
                                   is_swap (proc->mnt) ? proc->mnt->device : proc->mnt->mountpoint,
 
1321
                                   pid, status);
 
1322
                }
 
1323
 
 
1324
                exit_code = nih_max (exit_code, EXIT_ERROR);
 
1325
                if (NIH_LIST_EMPTY (procs))
 
1326
                        exit (exit_code);
 
1327
 
 
1328
                nih_free (proc);
 
1329
                return;
 
1330
        } else if (status) {
 
1331
                nih_warn ("%s %s [%d] terminated with status %d", proc->args[0],
 
1332
                          is_swap (proc->mnt) ? proc->mnt->device : proc->mnt->mountpoint,
 
1333
                          pid, status);
 
1334
        } else {
 
1335
                nih_info ("%s %s [%d] exited normally", proc->args[0],
 
1336
                          is_swap (proc->mnt) ? proc->mnt->device : proc->mnt->mountpoint,
 
1337
                          pid);
 
1338
        }
 
1339
 
 
1340
        if (proc->handler)
 
1341
                proc->handler (proc->mnt, pid, status);
 
1342
 
 
1343
        nih_free (proc);
 
1344
 
 
1345
        if (exit_code && NIH_LIST_EMPTY (procs))
 
1346
                exit (exit_code);
 
1347
}
 
1348
 
 
1349
 
 
1350
void
 
1351
run_mount (Mount *mnt,
 
1352
           int    fake)
 
1353
{
 
1354
        nih_local char **args = NULL;
 
1355
        size_t           args_len = 0;
 
1356
 
 
1357
        nih_assert (mnt != NULL);
 
1358
 
 
1359
        if (fake) {
 
1360
                nih_debug ("mtab %s", mnt->mountpoint);
 
1361
        } else if (mnt->mounted) {
 
1362
                if (needs_remount (mnt)) {
 
1363
                        nih_info ("remounting %s", mnt->mountpoint);
 
1364
                } else {
 
1365
                        nih_debug ("%s: already mounted", mnt->mountpoint);
 
1366
                        mounted (mnt);
 
1367
                        return;
 
1368
                }
 
1369
        } else if (mnt->mount_pid > 0) {
 
1370
                nih_debug ("%s: already mounting", mnt->mountpoint);
 
1371
                return;
 
1372
        } else if (! mnt->type) {
 
1373
                nih_debug ("%s: hook", mnt->mountpoint);
 
1374
                mounted (mnt);
 
1375
                return;
 
1376
        } else {
 
1377
                nih_info ("mounting %s", mnt->mountpoint);
 
1378
        }
 
1379
 
 
1380
        args = NIH_MUST (nih_str_array_new (NULL));
 
1381
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "mount"));
 
1382
        if (fake) {
 
1383
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-f"));
 
1384
        } else if (! written_mtab) {
 
1385
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-n"));
 
1386
        }
 
1387
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-a"));
 
1388
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-t"));
 
1389
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, mnt->type));
 
1390
        if (mnt->opts) {
 
1391
                nih_local char *opts = NULL;
 
1392
 
 
1393
                opts = cut_options (NULL, mnt, "showthrough", NULL);
 
1394
                if (mnt->mounted && (! fake)) {
 
1395
                        char *tmp;
 
1396
 
 
1397
                        tmp = NIH_MUST (nih_strdup (NULL, "remount,"));
 
1398
                        NIH_MUST (nih_strcat (&tmp, NULL, opts));
 
1399
 
 
1400
                        nih_discard (opts);
 
1401
                        opts = tmp;
 
1402
                }
 
1403
 
 
1404
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-o"));
 
1405
                NIH_MUST (nih_str_array_addp (&args, NULL, &args_len, opts));
 
1406
        } else if (mnt->mounted && (! fake)) {
 
1407
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-o"));
 
1408
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "remount"));
 
1409
        }
 
1410
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, mnt->device ?: "none"));
 
1411
        if (has_showthrough (mnt) && (! fake)) {
 
1412
                nih_local char *mountpoint = NULL;
 
1413
 
 
1414
                mountpoint = NIH_MUST (nih_sprintf (NULL, "/dev/%s",
 
1415
                                                    mnt->mountpoint));
 
1416
 
 
1417
                for (size_t i = 5; i < strlen (mountpoint); i++)
 
1418
                        if (mountpoint[i] == '/')
 
1419
                                mountpoint[i] = '.';
 
1420
 
 
1421
                nih_debug ("diverting mount to %s", mountpoint);
 
1422
                if ((mkdir (mountpoint, 0755) < 0)
 
1423
                    && (errno != EEXIST)) {
 
1424
                        nih_fatal ("mkdir %s: %s", mountpoint, strerror (errno));
 
1425
 
 
1426
                        exit_code = nih_max (exit_code, EXIT_ERROR);
 
1427
                        if (NIH_LIST_EMPTY (procs))
 
1428
                                exit (exit_code);
 
1429
                        return;
 
1430
                } else
 
1431
                        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, mountpoint));
 
1432
        } else {
 
1433
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, mnt->mountpoint));
 
1434
        }
 
1435
 
 
1436
        if (fake) {
 
1437
                spawn (mnt, args, TRUE, NULL);
 
1438
        } else if (has_option (mnt, "showthrough", FALSE)) {
 
1439
                spawn (mnt, args, TRUE, run_mount_finished);
 
1440
        } else {
 
1441
                mnt->mount_pid = spawn (mnt, args, FALSE, run_mount_finished);
 
1442
        }
 
1443
}
 
1444
 
 
1445
void
 
1446
run_mount_finished (Mount *mnt,
 
1447
                    pid_t  pid,
 
1448
                    int    status)
 
1449
{
 
1450
        nih_assert (mnt != NULL);
 
1451
        nih_assert ((mnt->mount_pid == pid)
 
1452
                    || (mnt->mount_pid == -1));
 
1453
 
 
1454
        mnt->mount_pid = -1;
 
1455
 
 
1456
        if (status & ~(16 | 64)) {
 
1457
                nih_error ("Filesystem could not be mounted: %s",
 
1458
                           mnt->mountpoint);
 
1459
 
 
1460
                exit_code = nih_max (exit_code, EXIT_MOUNT);
 
1461
                return;
 
1462
        }
 
1463
 
 
1464
        mount_showthrough (mnt);
 
1465
        mounted (mnt);
 
1466
}
 
1467
 
 
1468
 
 
1469
void
 
1470
run_swapon (Mount *mnt)
 
1471
{
 
1472
        nih_local char **args = NULL;
 
1473
        size_t           args_len = 0;
 
1474
        nih_local char * pri = NULL;
 
1475
 
 
1476
        nih_assert (mnt != NULL);
 
1477
        nih_assert (mnt->device != NULL);
 
1478
 
 
1479
        if (mnt->mounted) {
 
1480
                nih_debug ("%s: already activated", mnt->device);
 
1481
                mounted (mnt);
 
1482
                return;
 
1483
        } else if (mnt->mount_pid > 0) {
 
1484
                nih_debug ("%s: already activating", mnt->device);
 
1485
                return;
 
1486
        }
 
1487
 
 
1488
        nih_info ("activating %s", mnt->device);
 
1489
 
 
1490
        args = NIH_MUST (nih_str_array_new (NULL));
 
1491
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "swapon"));
 
1492
 
 
1493
        if (((pri = get_option (NULL, mnt, "pri", FALSE)) != NULL)
 
1494
            && *pri) {
 
1495
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-p"));
 
1496
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, pri));
 
1497
        }
 
1498
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, mnt->device));
 
1499
 
 
1500
        mnt->mount_pid = spawn (mnt, args, FALSE, run_swapon_finished);
 
1501
}
 
1502
 
 
1503
void
 
1504
run_swapon_finished (Mount *mnt,
 
1505
                     pid_t  pid,
 
1506
                     int    status)
 
1507
{
 
1508
        nih_assert (mnt != NULL);
 
1509
        nih_assert (mnt->mount_pid == pid);
 
1510
 
 
1511
        mnt->mount_pid = -1;
 
1512
 
 
1513
        /* Swapon doesn't return any useful status codes, so we just
 
1514
         * carry on regardless if it failed.
 
1515
         */
 
1516
        if (status)
 
1517
                nih_warn ("Problem activating swap: %s", mnt->device);
 
1518
 
 
1519
        mounted (mnt);
 
1520
}
 
1521
 
 
1522
 
 
1523
void
 
1524
run_fsck (Mount *mnt)
 
1525
{
 
1526
        nih_local char **args = NULL;
 
1527
        size_t           args_len = 0;
 
1528
 
 
1529
        nih_assert (mnt != NULL);
 
1530
        nih_assert (mnt->device != NULL);
 
1531
        nih_assert (mnt->type != NULL);
 
1532
 
 
1533
        if (! mnt->check) {
 
1534
                nih_debug ("%s: no check required", mnt->mountpoint);
 
1535
                device_ready (mnt);
 
1536
                return;
 
1537
        } else if (mnt->mounted && (! has_option (mnt, "ro", TRUE))) {
 
1538
                nih_debug ("%s: mounted filesystem", mnt->mountpoint);
 
1539
                device_ready (mnt);
 
1540
                return;
 
1541
        } else if (mnt->mount_pid > 0) {
 
1542
                nih_debug ("%s: already mounting", mnt->mountpoint);
 
1543
                return;
 
1544
        } else if (mnt->fsck_pid > 0) {
 
1545
                nih_debug ("%s: already checking", mnt->mountpoint);
 
1546
                return;
 
1547
        }
 
1548
 
 
1549
        /* FIXME look up underlying device, and add to fsck queue.
 
1550
         * only continue if was first in the queue.
 
1551
         */
 
1552
 
 
1553
        nih_info ("checking %s", mnt->mountpoint);
 
1554
 
 
1555
        args = NIH_MUST (nih_str_array_new (NULL));
 
1556
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "fsck"));
 
1557
        if (fsck_fix) {
 
1558
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-y"));
 
1559
        } else {
 
1560
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-a"));
 
1561
        }
 
1562
        if (force_fsck)
 
1563
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-f"));
 
1564
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-t"));
 
1565
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, mnt->type));
 
1566
        NIH_MUST (nih_str_array_add (&args, NULL, &args_len, mnt->device));
 
1567
 
 
1568
        mnt->fsck_pid = spawn (mnt, args, FALSE, run_fsck_finished);
 
1569
}
 
1570
 
 
1571
void
 
1572
run_fsck_finished (Mount *mnt,
 
1573
                   pid_t  pid,
 
1574
                   int    status)
 
1575
{
 
1576
        nih_assert (mnt != NULL);
 
1577
        nih_assert (mnt->fsck_pid == pid);
 
1578
 
 
1579
        mnt->fsck_pid = -1;
 
1580
 
 
1581
        /* FIXME remove from fsck queue for underlying device,
 
1582
         * queue next in that list if there is one.
 
1583
         */
 
1584
 
 
1585
        if (status & 2) {
 
1586
                nih_error ("System must be rebooted: %s",
 
1587
                           mnt->mountpoint);
 
1588
                exit_code = nih_max (exit_code, EXIT_REBOOT);
 
1589
        } else if (status & 4) {
 
1590
                nih_error ("Filesystem has errors: %s",
 
1591
                           mnt->mountpoint);
 
1592
                exit_code = nih_max (exit_code, EXIT_FSCK);
 
1593
        } else if (status & (8 | 16 | 128)) {
 
1594
                nih_fatal ("General fsck error");
 
1595
                exit_code = nih_max (exit_code, EXIT_ERROR);
 
1596
        } else if (status & 1) {
 
1597
                nih_info ("Filesystem errors corrected: %s",
 
1598
                          mnt->mountpoint);
 
1599
        } else if (status & 32) {
 
1600
                nih_info ("Filesytem check cancelled: %s",
 
1601
                          mnt->mountpoint);
 
1602
        }
 
1603
 
 
1604
        device_ready (mnt);
 
1605
}
 
1606
 
 
1607
 
 
1608
void
 
1609
write_mtab (void)
 
1610
{
 
1611
        int mtab;
 
1612
 
 
1613
        if (((mtab = open (_PATH_MOUNTED, O_CREAT | O_TRUNC | O_WRONLY, 0644)) < 0)
 
1614
            || (close (mtab) < 0))
 
1615
                return;
 
1616
 
 
1617
        for (size_t i = 0; i < num_mounts; i++) {
 
1618
                if (! mounts[i].mounted)
 
1619
                        continue;
 
1620
                if (! mounts[i].type)
 
1621
                        continue;
 
1622
                if (is_swap (&mounts[i]))
 
1623
                        continue;
 
1624
 
 
1625
                run_mount (&mounts[i], TRUE);
 
1626
        }
 
1627
 
 
1628
        written_mtab = TRUE;
 
1629
}
 
1630
 
 
1631
 
 
1632
int
 
1633
has_showthrough (Mount *root)
 
1634
{
 
1635
        size_t i = 0;
 
1636
 
 
1637
        nih_assert (root != NULL);
 
1638
 
 
1639
        /* Root cannot have showthroughs */
 
1640
        if (! strcmp (root->mountpoint, "/"))
 
1641
                return FALSE;
 
1642
 
 
1643
        while ((i < num_paths)
 
1644
               && ((paths[i].mnt != root) || paths[i].device))
 
1645
                i++;
 
1646
        nih_assert (i++ < num_paths);
 
1647
 
 
1648
        while ((i < num_paths)
 
1649
               && (! strncmp (paths[i].path, root->mountpoint,
 
1650
                              strlen (root->mountpoint)))
 
1651
               && ((paths[i].path[strlen (root->mountpoint)] == '/')
 
1652
                   || (paths[i].path[strlen (root->mountpoint)] == '\0'))) {
 
1653
                Path *path;
 
1654
 
 
1655
                path = &paths[i++];
 
1656
                if (path->device)
 
1657
                        continue;
 
1658
                if (! has_option (path->mnt, "showthrough", FALSE))
 
1659
                        continue;
 
1660
                if (! path->mnt->mounted)
 
1661
                        continue;
 
1662
 
 
1663
                /* Mount option that should show though */
 
1664
                return TRUE;
 
1665
        }
 
1666
 
 
1667
        return FALSE;
 
1668
}
 
1669
 
 
1670
void
 
1671
mount_showthrough (Mount *root)
 
1672
{
 
1673
        nih_local char * mountpoint = NULL;
 
1674
        size_t           i = 0;
 
1675
        nih_local char **args = NULL;
 
1676
        size_t           args_len = 0;
 
1677
        int              move = FALSE;
 
1678
 
 
1679
        nih_assert (root != NULL);
 
1680
 
 
1681
        /* Root cannot have showthroughs */
 
1682
        if (! strcmp (root->mountpoint, "/"))
 
1683
                return;
 
1684
 
 
1685
        while ((i < num_paths)
 
1686
               && ((paths[i].mnt != root) || paths[i].device))
 
1687
                i++;
 
1688
        nih_assert (i++ < num_paths);
 
1689
 
 
1690
        /* This is the mountpoint we actually used */
 
1691
        mountpoint = NIH_MUST (nih_sprintf (NULL, "/dev/%s", root->mountpoint));
 
1692
        for (size_t i = 5; i < strlen (mountpoint); i++)
 
1693
                if (mountpoint[i] == '/')
 
1694
                        mountpoint[i] = '.';
 
1695
 
 
1696
        while ((i < num_paths)
 
1697
               && (! strncmp (paths[i].path, root->mountpoint,
 
1698
                              strlen (root->mountpoint)))
 
1699
               && ((paths[i].path[strlen (root->mountpoint)] == '/')
 
1700
                   || (paths[i].path[strlen (root->mountpoint)] == '/'))) {
 
1701
                Path *           path;
 
1702
                nih_local char * submount = NULL;
 
1703
                nih_local char **args = NULL;
 
1704
                size_t           args_len = 0;
 
1705
 
 
1706
                path = &paths[i++];
 
1707
                if (path->device)
 
1708
                        continue;
 
1709
                if (! has_option (path->mnt, "showthrough", FALSE))
 
1710
                        continue;
 
1711
                if (! path->mnt->mounted)
 
1712
                        continue;
 
1713
 
 
1714
                submount = NIH_MUST (nih_sprintf (NULL, "%s%s", mountpoint,
 
1715
                                                  path->mnt->mountpoint + strlen (root->mountpoint)));
 
1716
 
 
1717
                args = NIH_MUST (nih_str_array_new (NULL));
 
1718
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "mount"));
 
1719
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-n"));
 
1720
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "--bind"));
 
1721
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, path->mnt->mountpoint));
 
1722
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, submount));
 
1723
 
 
1724
                nih_debug ("binding %s to %s", path->mnt->mountpoint, submount);
 
1725
 
 
1726
 
 
1727
                if (spawn (path->mnt, args, TRUE, NULL) < 0)
 
1728
                        return;
 
1729
 
 
1730
                move = TRUE;
 
1731
        }
 
1732
 
 
1733
        if (move) {
 
1734
                args = NIH_MUST (nih_str_array_new (NULL));
 
1735
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "mount"));
 
1736
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "-n"));
 
1737
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, "--move"));
 
1738
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, mountpoint));
 
1739
                NIH_MUST (nih_str_array_add (&args, NULL, &args_len, root->mountpoint));
 
1740
 
 
1741
                nih_debug ("moving %s", root->mountpoint);
 
1742
 
 
1743
                if (spawn (root, args, TRUE, NULL) < 0)
 
1744
                        return;
 
1745
 
 
1746
                if ((rmdir (mountpoint) < 0)
 
1747
                    && (errno != EEXIST))
 
1748
                        nih_warn ("rmdir %s: %s", mountpoint, strerror (errno));
 
1749
        }
 
1750
}
 
1751
 
 
1752
 
 
1753
void
 
1754
upstart_disconnected (DBusConnection *connection)
 
1755
{
 
1756
        nih_fatal (_("Disconnected from Upstart"));
 
1757
        exit_code = nih_max (exit_code, EXIT_ERROR);
 
1758
        if (NIH_LIST_EMPTY (procs))
 
1759
                exit (exit_code);
 
1760
}
 
1761
 
 
1762
void
 
1763
emit_event (const char *name)
 
1764
{
 
1765
        DBusPendingCall *pending_call;
 
1766
        nih_local char **env = NULL;
 
1767
 
 
1768
        nih_assert (name != NULL);
 
1769
 
 
1770
        env = NIH_MUST (nih_str_array_new (NULL));
 
1771
 
 
1772
        pending_call = NIH_SHOULD (upstart_emit_event (upstart,
 
1773
                                                       name, env, FALSE,
 
1774
                                                       NULL, emit_event_error, NULL,
 
1775
                                                       NIH_DBUS_TIMEOUT_NEVER));
 
1776
        if (! pending_call) {
 
1777
                NihError *err;
 
1778
 
 
1779
                err = nih_error_get ();
 
1780
                nih_warn ("%s", err->message);
 
1781
                nih_free (err);
 
1782
 
 
1783
                return;
 
1784
        }
 
1785
 
 
1786
        dbus_pending_call_unref (pending_call);
 
1787
}
 
1788
 
 
1789
void
 
1790
emit_event_error (void *          data,
 
1791
                  NihDBusMessage *message)
 
1792
{
 
1793
        NihError *err;
 
1794
 
 
1795
        err = nih_error_get ();
 
1796
        nih_warn ("%s", err->message);
 
1797
        nih_free (err);
 
1798
}
 
1799
 
 
1800
 
 
1801
void
 
1802
udev_monitor_watcher (struct udev_monitor *udev_monitor,
 
1803
                      NihIoWatch *         watch,
 
1804
                      NihIoEvents          events)
 
1805
{
 
1806
        struct udev_device *    udev_device;
 
1807
        const char *            action;
 
1808
        const char *            subsystem;
 
1809
        const char *            devname;
 
1810
        const char *            usage;
 
1811
        const char *            type;
 
1812
        const char *            uuid;
 
1813
        const char *            label;
 
1814
        struct udev_list_entry *devlinks;
 
1815
 
 
1816
        udev_device = udev_monitor_receive_device (udev_monitor);
 
1817
        if (! udev_device)
 
1818
                return;
 
1819
 
 
1820
        action    = udev_device_get_action (udev_device);
 
1821
        subsystem = udev_device_get_subsystem (udev_device);
 
1822
        devname   = udev_device_get_devnode (udev_device);
 
1823
        devlinks  = udev_device_get_devlinks_list_entry (udev_device);
 
1824
        usage     = udev_device_get_property_value (udev_device, "ID_FS_USAGE");
 
1825
        type      = udev_device_get_property_value (udev_device, "ID_FS_TYPE");
 
1826
        uuid      = udev_device_get_property_value (udev_device, "ID_FS_UUID");
 
1827
        label     = udev_device_get_property_value (udev_device, "ID_FS_LABEL");
 
1828
 
 
1829
        if ((! subsystem)
 
1830
            || strcmp (subsystem, "block")) {
 
1831
                udev_device_unref (udev_device);
 
1832
                return;
 
1833
        }
 
1834
 
 
1835
        if ((! action)
 
1836
            || (strcmp (action, "add")
 
1837
                && strcmp (action, "change"))) {
 
1838
                udev_device_unref (udev_device);
 
1839
                return;
 
1840
        }
 
1841
 
 
1842
        if ((! usage)
 
1843
            || (strcmp (usage, "filesystem")
 
1844
                && (strcmp (usage, "other")
 
1845
                    || (! type)
 
1846
                    || strcmp (type, "swap")))) {
 
1847
                udev_device_unref (udev_device);
 
1848
                return;
 
1849
        }
 
1850
 
 
1851
        nih_debug ("%s %s %s %s", subsystem, devname, uuid, label);
 
1852
 
 
1853
        for (size_t i = 0; i < num_mounts; i++) {
 
1854
                if (! mounts[i].device)
 
1855
                        continue;
 
1856
 
 
1857
                if ((! strncmp (mounts[i].device, "UUID=", 5))
 
1858
                    && uuid
 
1859
                    && (! strcmp (mounts[i].device + 5, uuid))) {
 
1860
                        struct udev_list_entry *devlink;
 
1861
 
 
1862
                        nih_debug ("%s by uuid", mounts[i].mountpoint);
 
1863
 
 
1864
                        for (devlink = devlinks; devlink;
 
1865
                             devlink = udev_list_entry_get_next (devlink)) {
 
1866
                                const char *name = udev_list_entry_get_name (devlink);
 
1867
 
 
1868
                                if (! strncmp (name, "/dev/disk/by-uuid/", 18)) {
 
1869
                                        update_mount (&mounts[i], name, -1, NULL, NULL, NULL);
 
1870
                                        break;
 
1871
                                }
 
1872
                        }
 
1873
 
 
1874
                        if (! devlink)
 
1875
                                update_mount (&mounts[i], devname, -1, NULL, NULL, NULL);
 
1876
                } else if ((! strncmp (mounts[i].device, "LABEL=", 6))
 
1877
                           && label
 
1878
                           && (! strcmp (mounts[i].device + 6, label))) {
 
1879
                        struct udev_list_entry *devlink;
 
1880
 
 
1881
                        nih_debug ("%s by label", mounts[i].mountpoint);
 
1882
 
 
1883
                        for (devlink = devlinks; devlink;
 
1884
                             devlink = udev_list_entry_get_next (devlink)) {
 
1885
                                const char *name = udev_list_entry_get_name (devlink);
 
1886
 
 
1887
                                if (! strncmp (name, "/dev/disk/by-label/", 18)) {
 
1888
                                        update_mount (&mounts[i], name, -1, NULL, NULL, NULL);
 
1889
                                        break;
 
1890
                                }
 
1891
                        }
 
1892
 
 
1893
                        if (! devlink)
 
1894
                                update_mount (&mounts[i], devname, -1, NULL, NULL, NULL);
 
1895
                } else if (! strcmp (mounts[i].device, devname)) {
 
1896
                        nih_debug ("%s by name", mounts[i].mountpoint);
 
1897
                } else {
 
1898
                        struct udev_list_entry *devlink;
 
1899
 
 
1900
                        for (devlink = devlinks; devlink;
 
1901
                             devlink = udev_list_entry_get_next (devlink)) {
 
1902
                                const char *name = udev_list_entry_get_name (devlink);
 
1903
 
 
1904
                                if (! strcmp (mounts[i].device, name)) {
 
1905
                                        nih_debug ("%s by link %s", mounts[i].mountpoint,
 
1906
                                                   name);
 
1907
                                        break;
 
1908
                                }
 
1909
                        }
 
1910
 
 
1911
                        if (! devlink)
 
1912
                                continue;
 
1913
                }
 
1914
 
 
1915
                if (mounts[i].udev_device)
 
1916
                        udev_device_unref (mounts[i].udev_device);
 
1917
 
 
1918
                mounts[i].udev_device = udev_device;
 
1919
                udev_device_ref (mounts[i].udev_device);
 
1920
 
 
1921
                run_fsck (&mounts[i]);
 
1922
        }
 
1923
 
 
1924
        udev_device_unref (udev_device);
 
1925
}
 
1926
 
 
1927
void
 
1928
usr1_handler (void *     data,
 
1929
              NihSignal *signal)
 
1930
{
 
1931
        if (no_remote)
 
1932
                return;
 
1933
 
 
1934
        nih_debug ("Received SIGUSR1 (network device up)");
 
1935
 
 
1936
        for (size_t i = 0; i < num_mounts; i++)
 
1937
                if (is_remote (&mounts[i])
 
1938
                    && (! mounts[i].mounted))
 
1939
                        device_ready (&mounts[i]);
 
1940
}
 
1941
 
 
1942
 
 
1943
int
 
1944
dev_hook (Mount *mnt)
 
1945
{
 
1946
        nih_assert (mnt != NULL);
 
1947
 
 
1948
        nih_debug ("populating %s", mnt->mountpoint);
 
1949
 
 
1950
        int dev_hook_walk (const char *       fpath,
 
1951
                           const struct stat *sb,
 
1952
                           int                typeflag,
 
1953
                           struct FTW *       ftwbuf)
 
1954
        {
 
1955
                char dest[PATH_MAX];
 
1956
 
 
1957
                strcpy (dest, mnt->mountpoint);
 
1958
                strcat (dest, fpath + 17);
 
1959
 
 
1960
                if (S_ISDIR (sb->st_mode)) {
 
1961
                        if ((mkdir (dest, sb->st_mode & ~S_IFMT) < 0)
 
1962
                            && (errno != EEXIST))
 
1963
                                nih_warn ("%s: %s", dest, strerror (errno));
 
1964
                } else if (S_ISLNK (sb->st_mode)) {
 
1965
                        char target[PATH_MAX];
 
1966
                        ssize_t len;
 
1967
 
 
1968
                        len = readlink (fpath, target, sizeof target);
 
1969
                        target[len] = '\0';
 
1970
 
 
1971
                        if ((symlink (target, dest) < 0)
 
1972
                            && (errno != EEXIST))
 
1973
                                nih_warn ("%s: %s", dest, strerror (errno));
 
1974
                } else {
 
1975
                        if ((mknod (dest, sb->st_mode, sb->st_rdev) < 0)
 
1976
                            && (errno != EEXIST))
 
1977
                                nih_warn ("%s: %s", dest, strerror (errno));
 
1978
                }
 
1979
 
 
1980
                return FTW_CONTINUE;
 
1981
        }
 
1982
 
 
1983
 
 
1984
        return nftw ("/lib/udev/devices", dev_hook_walk, 1024,
 
1985
                     FTW_ACTIONRETVAL | FTW_PHYS | FTW_MOUNT);
 
1986
}
 
1987
 
 
1988
int
 
1989
tmp_hook (Mount *mnt)
 
1990
{
 
1991
        struct stat statbuf;
 
1992
        int         purge = FALSE;
 
1993
        time_t      barrier;
 
1994
 
 
1995
        nih_assert (mnt != NULL);
 
1996
 
 
1997
        if ((lstat (mnt->mountpoint, &statbuf) < 0)
 
1998
            || (! (statbuf.st_mode & S_IWOTH))) {
 
1999
                nih_debug ("cowardly not cleaning up %s", mnt->mountpoint);
 
2000
                return 0;
 
2001
        } else if (tmptime < 0) {
 
2002
                nih_debug ("not cleaning up %s", mnt->mountpoint);
 
2003
                return 0;
 
2004
        }
 
2005
 
 
2006
        nih_debug ("cleaning up %s", mnt->mountpoint);
 
2007
 
 
2008
        if (tmptime > 0) {
 
2009
                barrier = time (NULL) - (tmptime * 3600);
 
2010
        } else {
 
2011
                purge = TRUE;
 
2012
                barrier = 0;
 
2013
        }
 
2014
 
 
2015
        int tmp_hook_walk (const char *       fpath,
 
2016
                           const struct stat *sb,
 
2017
                           int                typeflag,
 
2018
                           struct FTW *       ftwbuf)
 
2019
        {
 
2020
                const char *name = fpath + ftwbuf->base;
 
2021
 
 
2022
                if (! ftwbuf->level)
 
2023
                        return FTW_CONTINUE;
 
2024
 
 
2025
                if (S_ISDIR (sb->st_mode)) {
 
2026
                        if (strcmp (name, "lost+found")
 
2027
                            && (purge
 
2028
                                || ((sb->st_mtime < barrier
 
2029
                                     && (sb->st_ctime < barrier)))))
 
2030
                        {
 
2031
                                if (rmdir (fpath) < 0)
 
2032
                                        nih_warn ("%s: %s", fpath, strerror (errno));
 
2033
                        }
 
2034
 
 
2035
                } else {
 
2036
                        if (strcmp (name, "quota.user")
 
2037
                            && strcmp (name, "aquota.user")
 
2038
                            && strcmp (name, "quote.group")
 
2039
                            && strcmp (name, "aquota.group")
 
2040
                            && strcmp (name, ".journal")
 
2041
                            && fnmatch ("...security*", name, FNM_PATHNAME)
 
2042
                            && (purge
 
2043
                                || ((sb->st_mtime < barrier)
 
2044
                                    && (sb->st_ctime < barrier)
 
2045
                                    && (sb->st_atime < barrier))
 
2046
                                || ((ftwbuf->level == 1)
 
2047
                                    && (! fnmatch (".X*-lock", fpath + ftwbuf->base, FNM_PATHNAME)))))
 
2048
                        {
 
2049
                                if (unlink (fpath) < 0)
 
2050
                                        nih_warn ("%s: %s", fpath, strerror (errno));
 
2051
                        }
 
2052
 
 
2053
                }
 
2054
 
 
2055
                return FTW_CONTINUE;
 
2056
        }
 
2057
 
 
2058
        return nftw (mnt->mountpoint, tmp_hook_walk, 1024,
 
2059
                     FTW_ACTIONRETVAL | FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
 
2060
}
 
2061
 
 
2062
int
 
2063
var_run_hook (Mount *mnt)
 
2064
{
 
2065
        char  path[PATH_MAX];
 
2066
        int   fd;
 
2067
        FILE *fp;
 
2068
 
 
2069
        nih_assert (mnt != NULL);
 
2070
 
 
2071
        if (nih_main_write_pidfile (getpid ()) < 0) {
 
2072
                NihError *err;
 
2073
 
 
2074
                err = nih_error_get ();
 
2075
                nih_warn ("%s: %s", nih_main_get_pidfile (), err->message);
 
2076
                nih_free (err);
 
2077
        }
 
2078
 
 
2079
        nih_debug ("creating %s/utmp", mnt->mountpoint);
 
2080
 
 
2081
        strcpy (path, mnt->mountpoint);
 
2082
        strcat (path, "/utmp");
 
2083
 
 
2084
        if (((fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0664)) < 0)
 
2085
            || (close (fd) < 0)) {
 
2086
                nih_warn ("%s: %s", path, strerror (errno));
 
2087
        } else {
 
2088
                struct group *grp;
 
2089
 
 
2090
                grp = getgrnam ("utmp");
 
2091
                if (grp)
 
2092
                        chown (path, 0, grp->gr_gid);
 
2093
        }
 
2094
 
 
2095
 
 
2096
        nih_debug ("creating %s/motd", mnt->mountpoint);
 
2097
 
 
2098
        strcpy (path, mnt->mountpoint);
 
2099
        strcat (path, "/motd");
 
2100
 
 
2101
        fp = fopen (path, "w");
 
2102
        if (! fp) {
 
2103
                nih_warn ("%s: %s", path, strerror (errno));
 
2104
        } else {
 
2105
                struct utsname uts;
 
2106
 
 
2107
                uname (&uts);
 
2108
                fprintf (fp, "%s %s %s %s %s\n",
 
2109
                         uts.sysname, uts.nodename, uts.release, uts.version,
 
2110
                         uts.machine);
 
2111
                fflush (fp);
 
2112
 
 
2113
                if ((fd = open ("/etc/motd.tail", O_RDONLY)) >= 0) {
 
2114
                        char    buf[4096];
 
2115
                        ssize_t len;
 
2116
 
 
2117
                        while ((len = read (fd, buf, sizeof buf)) > 0)
 
2118
                                write (fileno (fp), buf, len);
 
2119
 
 
2120
                        close (fd);
 
2121
                }
 
2122
 
 
2123
                if (fclose (fp))
 
2124
                        nih_warn ("%s: %s", path, strerror (errno));
 
2125
        }
 
2126
 
 
2127
 
 
2128
        nih_debug ("populating %s from initramfs", mnt->mountpoint);
 
2129
 
 
2130
        int var_run_hook_walk (const char *       fpath,
 
2131
                               const struct stat *sb,
 
2132
                               int                typeflag,
 
2133
                               struct FTW *       ftwbuf)
 
2134
        {
 
2135
                char dest[PATH_MAX];
 
2136
 
 
2137
                strcpy (dest, mnt->mountpoint);
 
2138
                strcat (dest, fpath + 22);
 
2139
 
 
2140
                if (S_ISDIR (sb->st_mode)) {
 
2141
                        if ((mkdir (dest, sb->st_mode & ~S_IFMT) < 0)
 
2142
                            && (errno != EEXIST))
 
2143
                                nih_warn ("%s: %s", dest, strerror (errno));
 
2144
                } else if (S_ISLNK (sb->st_mode)) {
 
2145
                        char target[PATH_MAX];
 
2146
                        ssize_t len;
 
2147
 
 
2148
                        len = readlink (fpath, target, sizeof target);
 
2149
                        target[len] = '\0';
 
2150
 
 
2151
                        if ((symlink (target, dest) < 0)
 
2152
                            && (errno != EEXIST))
 
2153
                                nih_warn ("%s: %s", dest, strerror (errno));
 
2154
                } else {
 
2155
                        int     in_fd;
 
2156
                        int     out_fd;
 
2157
                        char    buf[4096];
 
2158
                        ssize_t len;
 
2159
 
 
2160
                        in_fd = open (fpath, O_RDONLY);
 
2161
                        if (in_fd < 0) {
 
2162
                                nih_warn ("%s: %s", fpath, strerror (errno));
 
2163
                                return FTW_CONTINUE;
 
2164
                        }
 
2165
 
 
2166
                        out_fd = open (dest, O_WRONLY | O_CREAT | O_TRUNC,
 
2167
                                       0644);
 
2168
                        if (out_fd < 0) {
 
2169
                                nih_warn ("%s: %s", dest, strerror (errno));
 
2170
                                close (in_fd);
 
2171
                                return FTW_CONTINUE;
 
2172
                        }
 
2173
 
 
2174
                        while ((len = read (in_fd, buf, sizeof buf)) > 0)
 
2175
                                write (out_fd, buf, len);
 
2176
 
 
2177
                        close (in_fd);
 
2178
                        if (close (out_fd) < 0)
 
2179
                                nih_warn ("%s: %s", dest, strerror (errno));
 
2180
                }
 
2181
 
 
2182
                return FTW_CONTINUE;
 
2183
        }
 
2184
 
 
2185
 
 
2186
        return nftw ("/dev/.initramfs/varrun", var_run_hook_walk, 1024,
 
2187
                     FTW_ACTIONRETVAL | FTW_PHYS | FTW_MOUNT);
 
2188
}
 
2189
 
 
2190
 
 
2191
int
 
2192
tmptime_option (NihOption * option,
 
2193
                const char *arg)
 
2194
{
 
2195
        int *value;
 
2196
 
 
2197
        nih_assert (option != NULL);
 
2198
        nih_assert (option->value != NULL);
 
2199
        nih_assert (arg != NULL);
 
2200
 
 
2201
        value = (int *)option->value;
 
2202
 
 
2203
        if (strcmp (arg, "infinite")
 
2204
            && strcmp (arg, "infinity")) {
 
2205
                *value = atoi (arg);
 
2206
        } else {
 
2207
                *value = -1;
 
2208
        }
 
2209
 
 
2210
        return 0;
 
2211
}
 
2212
 
 
2213
/**
 
2214
 * options:
 
2215
 *
 
2216
 * Command-line options accepted by this program.
 
2217
 **/
 
2218
static NihOption options[] = {
 
2219
        { 0, "daemon", N_("Detach and run in the background"),
 
2220
          NULL, NULL, &daemonise, NULL },
 
2221
        { 0, "force-fsck", N_("Force check of all filesystems"),
 
2222
          NULL, NULL, &force_fsck, NULL },
 
2223
        { 0, "fsck-fix", N_("Attempt to fix all fsck errors"),
 
2224
          NULL, NULL, &fsck_fix, NULL },
 
2225
        { 0, "no-remote", N_("Ignore remote filesystems"),
 
2226
          NULL, NULL, &no_remote, NULL },
 
2227
        { 0, "tmptime", N_("Grace to give files in /tmp"),
 
2228
          NULL, "HOURS", &tmptime, tmptime_option },
 
2229
 
 
2230
        NIH_OPTION_LAST
 
2231
};
 
2232
 
 
2233
 
 
2234
int
 
2235
main (int   argc,
 
2236
      char *argv[])
 
2237
{
 
2238
        char **              args;
 
2239
        DBusConnection *     connection;
 
2240
        struct udev *        udev;
 
2241
        struct udev_monitor *udev_monitor;
 
2242
        Mount *              root;
 
2243
        int                  ret;
 
2244
 
 
2245
        nih_main_init (argv[0]);
 
2246
 
 
2247
        nih_option_set_synopsis (_("Mount filesystems on boot"));
 
2248
        nih_option_set_help (
 
2249
                _("By default, mountall does not detach from the "
 
2250
                  "console and remains in the foreground.  Use the --daemon "
 
2251
                  "option to have it detach."));
 
2252
 
 
2253
        args = nih_option_parser (NULL, argc, argv, options, FALSE);
 
2254
        if (! args)
 
2255
                exit (EXIT_ERROR);
 
2256
 
 
2257
        nih_signal_reset ();
 
2258
 
 
2259
        /* Initialise the connection to Upstart */
 
2260
        connection = NIH_SHOULD (nih_dbus_connect (DBUS_ADDRESS_UPSTART, upstart_disconnected));
 
2261
        if (! connection) {
 
2262
                NihError *err;
 
2263
 
 
2264
                err = nih_error_get ();
 
2265
                nih_fatal ("%s: %s", _("Could not connect to Upstart"),
 
2266
                           err->message);
 
2267
                nih_free (err);
 
2268
 
 
2269
                exit (1);
 
2270
        }
 
2271
 
 
2272
        upstart = NIH_SHOULD (nih_dbus_proxy_new (NULL, connection,
 
2273
                                                  NULL, DBUS_PATH_UPSTART,
 
2274
                                                  NULL, NULL));
 
2275
        if (! upstart) {
 
2276
                NihError *err;
 
2277
 
 
2278
                err = nih_error_get ();
 
2279
                nih_fatal ("%s: %s", _("Could not create Upstart proxy"),
 
2280
                           err->message);
 
2281
                nih_free (err);
 
2282
 
 
2283
                exit (1);
 
2284
        }
 
2285
 
 
2286
        /* Initialise the connection to udev */
 
2287
        nih_assert (udev = udev_new ());
 
2288
        nih_assert (udev_monitor = udev_monitor_new_from_netlink (udev, "udev"));
 
2289
        nih_assert (udev_monitor_filter_add_match_subsystem_devtype (udev_monitor, "block", NULL) == 0);
 
2290
        nih_assert (udev_monitor_enable_receiving (udev_monitor) == 0);
 
2291
 
 
2292
        NIH_MUST (nih_io_add_watch (NULL, udev_monitor_get_fd (udev_monitor),
 
2293
                                    NIH_IO_READ,
 
2294
                                    (NihIoWatcher)udev_monitor_watcher,
 
2295
                                    udev_monitor));
 
2296
 
 
2297
        /* Initialse mount table with built-in filesystems, then parse
 
2298
         * from /etc/fstab and /proc/self/mountinfo to find out what else
 
2299
         * we need to do.
 
2300
         */
 
2301
        for (builtin = builtins; builtin->mountpoint; builtin++)
 
2302
                new_mount (builtin->mountpoint, builtin->device, builtin->check,
 
2303
                           builtin->type, builtin->opts, builtin->hook);
 
2304
 
 
2305
        parse_fstab ();
 
2306
        parse_mountinfo ();
 
2307
 
 
2308
        /* Parse /proc/filesystems and eliminate anything we don't know
 
2309
         * about, or anything that's got "noauto" in its options, etc.
 
2310
         */
 
2311
        parse_filesystems ();
 
2312
        cleanup ();
 
2313
 
 
2314
        /* Build a path table so we can lookup both children mountpoints
 
2315
         * and devices.  Initialise a process list.
 
2316
         */
 
2317
        build_paths ();
 
2318
        procs = NIH_MUST (nih_list_new (NULL));
 
2319
 
 
2320
        /* Sanity check, the root filesystem should be already mounted */
 
2321
        root = find_mount ("/");
 
2322
        if (! root->mounted) {
 
2323
                nih_fatal ("%s", _("root filesystem isn't mounted"));
 
2324
                exit (EXIT_ERROR);
 
2325
        }
 
2326
 
 
2327
        /* Become daemon */
 
2328
        if (daemonise) {
 
2329
                pid_t pid;
 
2330
 
 
2331
                /* Fork once because Upstart makes us a session leader,
 
2332
                 * or we may be a session leader of an existing process
 
2333
                 * group.
 
2334
                 */
 
2335
                pid = fork ();
 
2336
                if (pid < 0) {
 
2337
                        nih_fatal ("%s: %s", _("Unable to become daemon"),
 
2338
                                   strerror (errno));
 
2339
 
 
2340
                        exit (EXIT_ERROR);
 
2341
                } else if (pid > 0) {
 
2342
                        exit (0);
 
2343
                }
 
2344
 
 
2345
                /* Create a new session */
 
2346
                setsid ();
 
2347
 
 
2348
                /* Fork again so that we're not the leader of that session */
 
2349
                pid = fork ();
 
2350
                if (pid < 0) {
 
2351
                        nih_fatal ("%s: %s", _("Unable to become daemon"),
 
2352
                                   strerror (errno));
 
2353
 
 
2354
                        exit (EXIT_ERROR);
 
2355
                } else if (pid > 0) {
 
2356
                        exit (0);
 
2357
                }
 
2358
 
 
2359
                /* Usual daemon cleanups */
 
2360
                if (chdir ("/"))
 
2361
                        ;
 
2362
                umask (0);
 
2363
 
 
2364
                /* Send all logging output to syslog */
 
2365
                openlog (program_name, LOG_PID, LOG_DAEMON);
 
2366
                nih_log_set_logger (nih_logger_syslog);
 
2367
 
 
2368
                nih_signal_set_ignore (SIGHUP);
 
2369
 
 
2370
                nih_signal_set_handler (SIGINT, nih_signal_handler);
 
2371
                NIH_MUST (nih_signal_add_handler (NULL, SIGINT, nih_main_term_signal, NULL));
 
2372
        }
 
2373
 
 
2374
        nih_signal_set_handler (SIGCHLD, nih_signal_handler);
 
2375
 
 
2376
        /* Handle TERM and INT signals gracefully */
 
2377
        nih_signal_set_handler (SIGTERM, nih_signal_handler);
 
2378
        NIH_MUST (nih_signal_add_handler (NULL, SIGTERM, nih_main_term_signal, NULL));
 
2379
 
 
2380
        /* SIGUSR1 tells us that a network device came up */
 
2381
        nih_signal_set_handler (SIGUSR1, nih_signal_handler);
 
2382
        NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL));
 
2383
 
 
2384
 
 
2385
        /* All of the mountpoints under the root filesystem are now ready
 
2386
         * for mounting, even though it's still read-only.  All of the
 
2387
         * devices will be ready when it's mounted for writing.  This means
 
2388
         * we'll mount all of the virtual filesystems.
 
2389
         *
 
2390
         * Once that's done, mark the root mountpoint itself ready so we
 
2391
         * can remount the root filesystem when the device itself is ready.
 
2392
         */
 
2393
        children_ready (root, 1);
 
2394
        mountpoint_ready (root);
 
2395
 
 
2396
        ret = nih_main_loop ();
 
2397
 
 
2398
        nih_main_unlink_pidfile ();
 
2399
 
 
2400
        return ret;
 
2401
}
 
2402