~ubuntu-branches/ubuntu/raring/autofs5/raring

« back to all changes in this revision

Viewing changes to daemon/automount.c

  • Committer: Bazaar Package Importer
  • Author(s): Jan Christoph Nordholz
  • Date: 2009-03-09 01:16:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090309011648-gjynlid883f0s2c4
Tags: 5.0.4-1
* New upstream version (5.0.4 plus patchset as of 2009/03/09).
  * Closes: #518728.
  * Remove dpatch 14, applied upstream.
* New dpatch 14 to avoid using the relatively young SOCK_CLOEXEC
  feature.
* Only invoke 'make clean' on clean target so ./configure isn't
  purged.
* Fix a typo in the postinst regarding the ucf conffile handling.
* Add 'set -e' to package maintenance scripts.
* Drop unnecessary /var/run/autofs from package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
 
82
82
#define MAX_OPEN_FILES          10240
83
83
 
 
84
int aquire_flag_file(void);
 
85
void release_flag_file(void);
84
86
static int umount_all(struct autofs_point *ap, int force);
85
87
 
86
 
extern pthread_mutex_t master_mutex;
87
88
extern struct master *master_list;
88
89
 
89
90
static int do_mkdir(const char *parent, const char *path, mode_t mode)
245
246
                                                  int, void *), int incl, unsigned logopt, void *arg)
246
247
{
247
248
        char buf[PATH_MAX + 1];
248
 
        struct stat st;
249
 
 
250
 
        if (lstat(base, &st) != -1 && (fn) (logopt, base, &st, 0, arg)) {
 
249
        struct stat st, *pst = &st;
 
250
        int ret;
 
251
 
 
252
        if (!is_mounted(_PATH_MOUNTED, base, MNTS_REAL))
 
253
                ret = lstat(base, pst);
 
254
        else {
 
255
                pst = NULL;
 
256
                ret = 0;
 
257
        }
 
258
 
 
259
        if (ret != -1 && (fn) (logopt, base, pst, 0, arg)) {
251
260
                if (S_ISDIR(st.st_mode)) {
252
261
                        struct dirent **de;
253
262
                        int n;
281
290
                        free(de);
282
291
                }
283
292
                if (incl)
284
 
                        (fn) (logopt, base, &st, 1, arg);
 
293
                        (fn) (logopt, base, pst, 1, arg);
285
294
        }
286
295
        return 0;
287
296
}
292
301
        char buf[MAX_ERR_BUF];
293
302
        struct stat newst;
294
303
 
 
304
        if (!st)
 
305
                return 0;
 
306
 
295
307
        if (when == 0) {
296
308
                if (st->st_dev != dev)
297
309
                        return 0;
342
354
{
343
355
        struct counter_args *counter = (struct counter_args *) arg;
344
356
 
345
 
        if (S_ISLNK(st->st_mode) || (S_ISDIR(st->st_mode) 
346
 
                && st->st_dev != counter->dev)) {
 
357
        if (!st || (S_ISLNK(st->st_mode) || (S_ISDIR(st->st_mode)
 
358
                && st->st_dev != counter->dev))) {
347
359
                counter->count++;
348
360
                return 0;
349
361
        }
367
379
 
368
380
static void check_rm_dirs(struct autofs_point *ap, const char *path, int incl)
369
381
{
370
 
        if ((!ap->ghost) ||
 
382
        /*
 
383
         * If we're a submount the kernel can't know we're trying to
 
384
         * shutdown and so cannot block processes walking into the
 
385
         * mount point directory. If this is the call to umount_multi()
 
386
         * made during shutdown (incl == 0) we have to leave any mount
 
387
         * point directories in place so we can recover if needed. The
 
388
         * umount itself will clean these directories up for us
 
389
         * automagically.
 
390
         */
 
391
        if (!incl && ap->submount)
 
392
                return;
 
393
 
 
394
        if ((!(ap->flags & MOUNT_FLAG_GHOST)) ||
371
395
            (ap->state == ST_SHUTDOWN_PENDING ||
372
396
             ap->state == ST_SHUTDOWN_FORCE ||
373
397
             ap->state == ST_SHUTDOWN))
374
398
                rm_unwanted(ap->logopt, path, incl, ap->dev);
375
 
        else if (ap->ghost && (ap->type == LKP_INDIRECT))
 
399
        else if ((ap->flags & MOUNT_FLAG_GHOST) && (ap->type == LKP_INDIRECT))
376
400
                rm_unwanted(ap->logopt, path, 0, ap->dev);
377
401
}
378
402
 
388
412
        else
389
413
                key = path;
390
414
 
391
 
        pthread_cleanup_push(master_source_lock_cleanup, ap->entry);
392
 
        master_source_readlock(ap->entry);
393
415
        map = ap->entry->maps;
394
416
        while (map) {
395
417
                struct mapent *me = NULL;
411
433
 
412
434
                map = map->next;
413
435
        }
414
 
        pthread_cleanup_pop(1);
415
436
 
416
437
        return;
417
438
}
466
487
 
467
488
                pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
468
489
                /* Lock the closest parent nesting point for umount */
469
 
                cache_multi_lock(me->parent);
470
 
                if (umount_multi_triggers(ap, root, me, base)) {
 
490
                cache_multi_writelock(me->parent);
 
491
                if (umount_multi_triggers(ap, me, root, base)) {
471
492
                        warn(ap->logopt,
472
493
                             "some offset mounts still present under %s", path);
473
494
                        left++;
501
522
int umount_multi(struct autofs_point *ap, const char *path, int incl)
502
523
{
503
524
        struct mapent_cache *nc;
504
 
        struct statfs fs;
505
525
        int is_autofs_fs;
506
 
        int ret, left;
 
526
        int left;
507
527
 
508
528
        debug(ap->logopt, "path %s incl %d", path, incl);
509
529
 
515
535
        }
516
536
        cache_unlock(nc);
517
537
 
518
 
        ret = statfs(path, &fs);
519
 
        if (ret == -1) {
520
 
                error(ap->logopt, "could not stat fs of %s", path);
521
 
                return 1;
522
 
        }
523
 
 
524
 
        is_autofs_fs = fs.f_type == (__SWORD_TYPE) AUTOFS_SUPER_MAGIC ? 1 : 0;
 
538
        is_autofs_fs = 0;
 
539
        if (master_find_submount(ap, path))
 
540
                is_autofs_fs = 1;
525
541
 
526
542
        left = 0;
527
543
 
555
571
        return left;
556
572
}
557
573
 
558
 
int umount_autofs(struct autofs_point *ap, int force)
 
574
int umount_autofs(struct autofs_point *ap, const char *root, int force)
559
575
{
560
576
        int ret = 0;
561
577
 
572
588
        if (ap->type == LKP_INDIRECT) {
573
589
                if (umount_all(ap, force) && !force)
574
590
                        return -1;
575
 
                ret = umount_autofs_indirect(ap);
 
591
                ret = umount_autofs_indirect(ap, root);
576
592
        } else
577
593
                ret = umount_autofs_direct(ap);
578
594
 
579
595
        return ret;
580
596
}
581
597
 
582
 
int send_ready(unsigned logopt, int ioctlfd, unsigned int wait_queue_token)
583
 
{
584
 
        char buf[MAX_ERR_BUF];
585
 
 
586
 
        if (wait_queue_token == 0)
587
 
                return 0;
588
 
 
589
 
        debug(logopt, "token = %d", wait_queue_token);
590
 
 
591
 
        if (ioctl(ioctlfd, AUTOFS_IOC_READY, wait_queue_token) < 0) {
592
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
593
 
                logerr("AUTOFS_IOC_READY: error %s", estr);
594
 
                return 1;
595
 
        }
596
 
        return 0;
597
 
}
598
 
 
599
 
int send_fail(unsigned logopt, int ioctlfd, unsigned int wait_queue_token)
600
 
{
601
 
        char buf[MAX_ERR_BUF];
602
 
 
603
 
        if (wait_queue_token == 0)
604
 
                return 0;
605
 
 
606
 
        debug(logopt, "token = %d", wait_queue_token);
607
 
 
608
 
        if (ioctl(ioctlfd, AUTOFS_IOC_FAIL, wait_queue_token) < 0) {
609
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
610
 
                logerr("AUTOFS_IOC_FAIL: error %s", estr);
611
 
                return 1;
612
 
        }
613
 
        return 0;
614
 
}
615
 
 
616
598
static size_t get_kpkt_len(void)
617
599
{
618
600
        size_t pkt_len = sizeof(struct autofs_v5_packet);
737
719
        return ret;
738
720
}
739
721
 
740
 
static int destroy_logpri_fifo(struct autofs_point *ap)
 
722
int destroy_logpri_fifo(struct autofs_point *ap)
741
723
{
742
724
        int ret = -1;
743
725
        int fd = ap->logpri_fifo;
890
872
        return 0;
891
873
}
892
874
 
893
 
static int get_pkt(struct autofs_point *ap, union autofs_packet_union *pkt)
 
875
static int get_pkt(struct autofs_point *ap, union autofs_v5_packet_union *pkt)
894
876
{
895
877
        struct pollfd fds[3];
896
878
        int pollfds = 3;
916
898
                }
917
899
 
918
900
                if (fds[1].revents & POLLIN) {
919
 
                        enum states next_state, post_state;
 
901
                        enum states next_state;
920
902
                        size_t read_size = sizeof(next_state);
921
903
                        int state_pipe;
922
904
 
923
 
                        next_state = post_state = ST_INVAL;
 
905
                        next_state = ST_INVAL;
924
906
 
925
 
                        state_mutex_lock(ap);
 
907
                        st_mutex_lock();
926
908
 
927
909
                        state_pipe = ap->state_pipe[0];
928
910
 
929
911
                        if (fullread(state_pipe, &next_state, read_size)) {
930
 
                                state_mutex_unlock(ap);
 
912
                                st_mutex_unlock();
931
913
                                continue;
932
914
                        }
933
915
 
934
 
                        if (next_state != ST_INVAL && next_state != ap->state) {
935
 
                                if (next_state != ST_SHUTDOWN)
936
 
                                        post_state = next_state;
937
 
                                else
938
 
                                        ap->state = ST_SHUTDOWN;
939
 
                        }
940
 
 
941
 
                        state_mutex_unlock(ap);
942
 
 
943
 
                        if (post_state != ST_INVAL) {
944
 
                                if (post_state == ST_SHUTDOWN_PENDING ||
945
 
                                    post_state == ST_SHUTDOWN_FORCE) {
946
 
                                        alarm_delete(ap);
947
 
                                        st_remove_tasks(ap);
948
 
                                }
949
 
                                st_add_task(ap, post_state);
950
 
                        }
 
916
                        st_mutex_unlock();
951
917
 
952
918
                        if (next_state == ST_SHUTDOWN)
953
919
                                return -1;
983
949
 
984
950
        info(ap->logopt, "expiring path %s", buf);
985
951
 
 
952
        pthread_cleanup_push(master_source_lock_cleanup, ap->entry);
 
953
        master_source_readlock(ap->entry);
986
954
        ret = umount_multi(ap, buf, 1);
987
955
        if (ret == 0)
988
956
                info(ap->logopt, "expired %s", buf);
989
957
        else
990
958
                warn(ap->logopt, "couldn't complete expire of %s", buf);
 
959
        pthread_cleanup_pop(1);
991
960
 
992
961
        return ret;
993
962
}
1052
1021
        return 0;
1053
1022
}
1054
1023
 
1055
 
static int mount_autofs(struct autofs_point *ap)
 
1024
static int mount_autofs(struct autofs_point *ap, const char *root)
1056
1025
{
1057
1026
        int status = 0;
1058
1027
 
1062
1031
        if (ap->type == LKP_DIRECT)
1063
1032
                status = mount_autofs_direct(ap);
1064
1033
        else
1065
 
                status = mount_autofs_indirect(ap);
 
1034
                status = mount_autofs_indirect(ap, root);
1066
1035
 
1067
1036
        if (status < 0)
1068
1037
                return -1;
1069
1038
 
1070
 
        ap->state = ST_READY;
 
1039
        st_add_task(ap, ST_READY);
1071
1040
 
1072
1041
        return 0;
1073
1042
}
1074
1043
 
1075
1044
static int handle_packet(struct autofs_point *ap)
1076
1045
{
1077
 
        union autofs_packet_union pkt;
 
1046
        union autofs_v5_packet_union pkt;
1078
1047
 
1079
1048
        if (get_pkt(ap, &pkt))
1080
1049
                return -1;
1098
1067
        return -1;
1099
1068
}
1100
1069
 
1101
 
static void become_daemon(unsigned foreground)
 
1070
static void become_daemon(unsigned foreground, unsigned daemon_check)
1102
1071
{
1103
1072
        FILE *pidfp;
1104
1073
        char buf[MAX_ERR_BUF];
1118
1087
        }
1119
1088
 
1120
1089
        /* Detach from foreground process */
1121
 
        if (foreground)
 
1090
        if (foreground) {
 
1091
                if (daemon_check && !aquire_flag_file()) {
 
1092
                        fprintf(stderr, "%s: program is already running.\n",
 
1093
                                program);
 
1094
                        exit(1);
 
1095
                }
1122
1096
                log_to_stderr();
1123
 
        else {
 
1097
        } else {
1124
1098
                pid = fork();
1125
1099
                if (pid > 0) {
1126
1100
                        int r;
1136
1110
                }
1137
1111
                close(start_pipefd[0]);
1138
1112
 
 
1113
                if (daemon_check && !aquire_flag_file()) {
 
1114
                        fprintf(stderr, "%s: program is already running.\n",
 
1115
                                program);
 
1116
                        close(start_pipefd[1]);
 
1117
                        exit(1);
 
1118
                }
 
1119
 
1139
1120
                /*
1140
1121
                 * Make our own process group for "magic" reason: processes that share
1141
1122
                 * our pgrp see the raw filesystem behind the magic.
1143
1124
                if (setsid() == -1) {
1144
1125
                        char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1145
1126
                        fprintf(stderr, "setsid: %s", estr);
 
1127
                        close(start_pipefd[1]);
1146
1128
                        exit(1);
1147
1129
                }
1148
1130
                log_to_syslog();
1408
1390
                fatal(status);
1409
1391
}
1410
1392
 
1411
 
static void mutex_operation_wait(pthread_mutex_t *mutex)
1412
 
{
1413
 
        int status;
1414
 
 
1415
 
        /*
1416
 
         * Unlock a mutex, but wait for a pending operation
1417
 
         * if one is in progress
1418
 
         */
1419
 
        status = pthread_mutex_trylock(mutex);
1420
 
        if (status) {
1421
 
                if (status == EBUSY) {
1422
 
                        /* Mutex locked - do we own it */
1423
 
                        status = pthread_mutex_unlock(mutex);
1424
 
                        if (status) {
1425
 
                                if (status != EPERM)
1426
 
                                        fatal(status);
1427
 
                        } else
1428
 
                                return;
1429
 
 
1430
 
                        status = pthread_mutex_lock(mutex);
1431
 
                        if (status)
1432
 
                                fatal(status);
1433
 
                } else
1434
 
                        fatal(status);
1435
 
 
1436
 
                /* Operation complete, release it */
1437
 
                status = pthread_mutex_unlock(mutex);
1438
 
                if (status)
1439
 
                        fatal(status);
1440
 
        } else {
1441
 
                status = pthread_mutex_unlock(mutex);
1442
 
                if (status)
1443
 
                        fatal(status);
1444
 
        }
 
1393
int handle_mounts_startup_cond_init(struct startup_cond *suc)
 
1394
{
 
1395
        int status;
 
1396
 
 
1397
        status = pthread_mutex_init(&suc->mutex, NULL);
 
1398
        if (status)
 
1399
                return status;
 
1400
 
 
1401
        status = pthread_cond_init(&suc->cond, NULL);
 
1402
        if (status) {
 
1403
                status = pthread_mutex_destroy(&suc->mutex);
 
1404
                if (status)
 
1405
                        fatal(status);
 
1406
                return status;
 
1407
        }
 
1408
 
 
1409
        status = pthread_mutex_lock(&suc->mutex);
 
1410
        if (status) {
 
1411
                status = pthread_mutex_destroy(&suc->mutex);
 
1412
                if (status)
 
1413
                        fatal(status);
 
1414
                status = pthread_cond_destroy(&suc->cond);
 
1415
                if (status)
 
1416
                        fatal(status);
 
1417
        }
 
1418
 
 
1419
        return 0;
 
1420
}
 
1421
 
 
1422
void handle_mounts_startup_cond_destroy(void *arg)
 
1423
{
 
1424
        struct startup_cond *suc = (struct startup_cond *) arg;
 
1425
        int status;
 
1426
 
 
1427
        status = pthread_mutex_unlock(&suc->mutex);
 
1428
        if (status)
 
1429
                fatal(status);
 
1430
 
 
1431
        status = pthread_mutex_destroy(&suc->mutex);
 
1432
        if (status)
 
1433
                fatal(status);
 
1434
 
 
1435
        status = pthread_cond_destroy(&suc->cond);
 
1436
        if (status)
 
1437
                fatal(status);
1445
1438
 
1446
1439
        return;
1447
1440
}
1459
1452
        submount = ap->submount;
1460
1453
 
1461
1454
        strcpy(path, ap->path);
1462
 
        if (!submount && strcmp(ap->path, "/-") && ap->dir_created)
 
1455
        if (!submount && strcmp(ap->path, "/-") &&
 
1456
            ap->flags & MOUNT_FLAG_DIR_CREATED)
1463
1457
                clean = 1;
1464
1458
 
1465
 
        /* If we have been canceled then we may hold the state mutex. */
1466
 
        mutex_operation_wait(&ap->state_mutex);
1467
 
 
1468
 
        alarm_delete(ap);
1469
 
        st_remove_tasks(ap);
1470
 
 
1471
 
        umount_autofs(ap, 1);
 
1459
        if (submount) {
 
1460
                /* We are finishing up */
 
1461
                ap->parent->submnt_count--;
 
1462
                list_del_init(&ap->mounts);
 
1463
        }
 
1464
 
 
1465
        master_remove_mapent(ap->entry);
 
1466
        master_source_unlock(ap->entry);
 
1467
 
 
1468
        if (submount) {
 
1469
                mounts_mutex_unlock(ap->parent);
 
1470
                master_source_unlock(ap->parent->entry);
 
1471
        }
 
1472
        master_mutex_unlock();
1472
1473
 
1473
1474
        destroy_logpri_fifo(ap);
1474
 
        master_signal_submount(ap, MASTER_SUBMNT_JOIN);
1475
 
        master_remove_mapent(ap->entry);
1476
1475
        master_free_mapent_sources(ap->entry, 1);
1477
1476
        master_free_mapent(ap->entry);
1478
1477
 
1479
 
        sched_yield();
1480
 
 
1481
1478
        if (clean) {
1482
1479
                if (rmdir(path) == -1) {
1483
1480
                        char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1497
1494
 
1498
1495
void *handle_mounts(void *arg)
1499
1496
{
 
1497
        struct startup_cond *suc;
1500
1498
        struct autofs_point *ap;
1501
1499
        int cancel_state, status = 0;
1502
 
 
1503
 
        ap = (struct autofs_point *) arg;
1504
 
 
1505
 
        pthread_cleanup_push(return_start_status, &suc);
 
1500
        char *root;
 
1501
 
 
1502
        suc = (struct startup_cond *) arg;
 
1503
 
 
1504
        ap = suc->ap;
 
1505
        root = strdup(suc->root);
 
1506
 
 
1507
        pthread_cleanup_push(return_start_status, suc);
1506
1508
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
1507
1509
 
1508
 
        state_mutex_lock(ap);
1509
 
 
1510
 
        status = pthread_mutex_lock(&suc.mutex);
 
1510
        status = pthread_mutex_lock(&suc->mutex);
1511
1511
        if (status) {
1512
1512
                logerr("failed to lock startup condition mutex!");
1513
1513
                fatal(status);
1514
1514
        }
1515
1515
 
1516
 
        if (mount_autofs(ap) < 0) {
 
1516
        if (!root) {
 
1517
                crit(ap->logopt, "failed to alloc string root");
 
1518
                suc->status = 1;
 
1519
                pthread_setcancelstate(cancel_state, NULL);
 
1520
                pthread_exit(NULL);
 
1521
        }
 
1522
 
 
1523
        if (mount_autofs(ap, root) < 0) {
1517
1524
                crit(ap->logopt, "mount of %s failed!", ap->path);
1518
 
                suc.status = 1;
1519
 
                state_mutex_unlock(ap);
1520
 
                umount_autofs(ap, 1);
 
1525
                suc->status = 1;
 
1526
                umount_autofs(ap, root, 1);
 
1527
                free(root);
1521
1528
                pthread_setcancelstate(cancel_state, NULL);
1522
1529
                pthread_exit(NULL);
1523
1530
        }
1524
1531
 
1525
 
        if (ap->ghost && ap->type != LKP_DIRECT)
 
1532
        free(root);
 
1533
 
 
1534
        if (ap->flags & MOUNT_FLAG_GHOST && ap->type != LKP_DIRECT)
1526
1535
                info(ap->logopt, "ghosting enabled");
1527
1536
 
1528
 
        suc.status = 0;
 
1537
        suc->status = 0;
1529
1538
        pthread_cleanup_pop(1);
1530
1539
 
1531
1540
        /* We often start several automounters at the same time.  Add some
1533
1542
        if (!ap->submount && ap->exp_timeout)
1534
1543
                alarm_add(ap, ap->exp_runfreq + rand() % ap->exp_runfreq);
1535
1544
 
1536
 
        pthread_cleanup_push(handle_mounts_cleanup, ap);
1537
1545
        pthread_setcancelstate(cancel_state, NULL);
1538
1546
 
1539
 
        state_mutex_unlock(ap);
1540
 
 
1541
1547
        while (ap->state != ST_SHUTDOWN) {
1542
1548
                if (handle_packet(ap)) {
1543
 
                        int ret, result;
1544
 
 
1545
 
                        state_mutex_lock(ap);
 
1549
                        int ret, cur_state;
 
1550
 
 
1551
                        /*
 
1552
                         * If we're a submount we need to ensure our parent
 
1553
                         * doesn't try to mount us again until our shutdown
 
1554
                         * is complete and that any outstanding mounts are
 
1555
                         * completed before we try to shutdown.
 
1556
                         */
 
1557
                        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
 
1558
 
 
1559
                        master_mutex_lock();
 
1560
 
 
1561
                        if (ap->submount) {
 
1562
                                master_source_writelock(ap->parent->entry);
 
1563
                                mounts_mutex_lock(ap->parent);
 
1564
                        }
 
1565
 
 
1566
                        master_source_writelock(ap->entry);
 
1567
 
 
1568
                        if (ap->state != ST_SHUTDOWN) {
 
1569
                                if (!ap->submount)
 
1570
                                        alarm_add(ap, ap->exp_runfreq);
 
1571
                                /* Return to ST_READY is done immediately */
 
1572
                                st_add_task(ap, ST_READY);
 
1573
                                master_source_unlock(ap->entry);
 
1574
                                if (ap->submount) {
 
1575
                                        mounts_mutex_unlock(ap->parent);
 
1576
                                        master_source_unlock(ap->parent->entry);
 
1577
                                }
 
1578
 
 
1579
                                master_mutex_unlock();
 
1580
 
 
1581
                                pthread_setcancelstate(cur_state, NULL);
 
1582
                                continue;
 
1583
                        }
 
1584
 
 
1585
                        alarm_delete(ap);
 
1586
                        st_remove_tasks(ap);
 
1587
                        st_wait_task(ap, ST_ANY, 0);
 
1588
 
1546
1589
                        /*
1547
1590
                         * For a direct mount map all mounts have already gone
1548
 
                         * by the time we get here.
 
1591
                         * by the time we get here and since we only ever
 
1592
                         * umount direct mounts at shutdown there is no need
 
1593
                         * to check for possible recovery.
1549
1594
                         */
1550
1595
                        if (ap->type == LKP_DIRECT) {
1551
 
                                status = 1;
1552
 
                                state_mutex_unlock(ap);
 
1596
                                umount_autofs(ap, NULL, 1);
1553
1597
                                break;
1554
1598
                        }
1555
1599
 
1556
1600
                        /*
1557
 
                         * If the ioctl fails assume the kernel doesn't have
1558
 
                         * AUTOFS_IOC_ASKUMOUNT and just continue.
 
1601
                         * If umount_autofs returns non-zero it wasn't able
 
1602
                         * to complete the umount and has left the mount intact
 
1603
                         * so we can continue. This can happen if a lookup
 
1604
                         * occurs while we're trying to umount.
1559
1605
                         */
1560
 
                        ret = ioctl(ap->ioctlfd, AUTOFS_IOC_ASKUMOUNT, &result);
1561
 
                        if (ret == -1) {
1562
 
                                state_mutex_unlock(ap);
 
1606
                        ret = umount_autofs(ap, NULL, 1);
 
1607
                        if (!ret)
1563
1608
                                break;
1564
 
                        }
1565
 
 
1566
 
                        /* OK to exit */
1567
 
                        if (ap->state == ST_SHUTDOWN) {
1568
 
                                if (result) {
1569
 
                                        state_mutex_unlock(ap);
1570
 
                                        break;
1571
 
                                }
1572
 
#ifdef ENABLE_IGNORE_BUSY_MOUNTS
1573
 
                                /*
1574
 
                                 * There weren't any active mounts but if the
1575
 
                                 * filesystem is busy there may be a mount
1576
 
                                 * request in progress so return to the ready
1577
 
                                 * state unless a shutdown has been explicitly
1578
 
                                 * requested.
1579
 
                                 */
1580
 
                                if (ap->shutdown) {
1581
 
                                        state_mutex_unlock(ap);
1582
 
                                        break;
1583
 
                                }
1584
 
#endif
1585
 
                        }
1586
1609
 
1587
1610
                        /* Failed shutdown returns to ready */
1588
1611
                        warn(ap->logopt,
1590
1613
                             ap->path);
1591
1614
                        if (!ap->submount)
1592
1615
                                alarm_add(ap, ap->exp_runfreq);
1593
 
                        nextstate(ap->state_pipe[1], ST_READY);
1594
 
 
1595
 
                        state_mutex_unlock(ap);
 
1616
                        /* Return to ST_READY is done immediately */
 
1617
                        st_add_task(ap, ST_READY);
 
1618
                        master_source_unlock(ap->entry);
 
1619
                        if (ap->submount) {
 
1620
                                mounts_mutex_unlock(ap->parent);
 
1621
                                master_source_unlock(ap->parent->entry);
 
1622
                        }
 
1623
 
 
1624
                        master_mutex_unlock();
 
1625
 
 
1626
                        pthread_setcancelstate(cur_state, NULL);
 
1627
 
1596
1628
                }
1597
1629
        }
1598
1630
 
1599
 
        pthread_cleanup_pop(1);
1600
 
        sched_yield();
 
1631
        handle_mounts_cleanup(ap);
1601
1632
 
1602
1633
        return NULL;
1603
1634
}
1617
1648
        return;
1618
1649
}
1619
1650
 
1620
 
static int is_automount_running(void)
1621
 
{
1622
 
        FILE *fp;
1623
 
        DIR *dir;
1624
 
        struct dirent entry;
1625
 
        struct dirent *result;
1626
 
        char path[PATH_MAX + 1], buf[PATH_MAX];
1627
 
 
1628
 
        if ((dir = opendir("/proc")) == NULL) {
1629
 
                fprintf(stderr, "cannot opendir(/proc)\n");
1630
 
                exit(1);
1631
 
        }
1632
 
 
1633
 
        while (readdir_r(dir, &entry, &result) == 0) {
1634
 
                int path_len, pid = 0;
1635
 
 
1636
 
                if (!result)
1637
 
                        break;
1638
 
 
1639
 
                if (*entry.d_name == '.')
1640
 
                        continue;
1641
 
 
1642
 
                if (!strcmp(entry.d_name, "self"))
1643
 
                        continue;
1644
 
 
1645
 
                if (!isdigit(*entry.d_name))
1646
 
                        continue;
1647
 
 
1648
 
                pid = atoi(entry.d_name);
1649
 
                if (pid == getpid())
1650
 
                        continue;
1651
 
 
1652
 
                path_len = sprintf(path, "/proc/%s/cmdline", entry.d_name);
1653
 
                if (path_len >= PATH_MAX) {
1654
 
                        fprintf(stderr,
1655
 
                                "buffer to small for /proc path\n");
1656
 
                        return -1;
1657
 
                }
1658
 
                path[path_len] = '\0';
1659
 
 
1660
 
                fp = fopen(path, "r");
1661
 
                if (fp) {
1662
 
                        int c, len = 0;
1663
 
 
1664
 
                        while (len < 127 && (c = fgetc(fp)) != EOF && c)
1665
 
                                buf[len++] = c;
1666
 
                        buf[len] = '\0';
1667
 
 
1668
 
                        if (strstr(buf, "automount"))
1669
 
                                return pid;
1670
 
                        fclose(fp);
1671
 
                }
1672
 
        }
1673
 
        closedir(dir);
1674
 
 
1675
 
        return 0;
1676
 
}
1677
 
 
1678
1651
static void usage(void)
1679
1652
{
1680
1653
        fprintf(stderr,
1694
1667
                "                       specify global mount options\n"
1695
1668
                "       -l --set-log-priority priority path [path,...]\n"
1696
1669
                "                       set daemon log verbosity\n"
 
1670
                "       -C --dont-check-daemon\n"
 
1671
                "                       don't check if daemon is already running\n"
1697
1672
                "       -V --version    print version, build config and exit\n"
1698
1673
                , program);
1699
1674
}
1814
1789
{
1815
1790
        int res, opt, status;
1816
1791
        int logpri = -1;
1817
 
        unsigned ghost, logging;
 
1792
        unsigned ghost, logging, daemon_check;
1818
1793
        unsigned foreground, have_global_options;
1819
1794
        time_t timeout;
1820
1795
        time_t age = time(NULL);
1833
1808
                {"global-options", 1, 0, 'O'},
1834
1809
                {"version", 0, 0, 'V'},
1835
1810
                {"set-log-priority", 1, 0, 'l'},
 
1811
                {"dont-check-daemon", 0, 0, 'C'},
1836
1812
                {0, 0, 0, 0}
1837
1813
        };
1838
1814
 
1851
1827
        global_options = NULL;
1852
1828
        have_global_options = 0;
1853
1829
        foreground = 0;
 
1830
        daemon_check = 1;
1854
1831
 
1855
1832
        opterr = 0;
1856
 
        while ((opt = getopt_long(argc, argv, "+hp:t:vdD:fVrO:l:n:", long_options, NULL)) != EOF) {
 
1833
        while ((opt = getopt_long(argc, argv, "+hp:t:vdD:fVrO:l:n:C", long_options, NULL)) != EOF) {
1857
1834
                switch (opt) {
1858
1835
                case 'h':
1859
1836
                        usage();
1922
1899
                        }
1923
1900
                        break;
1924
1901
 
 
1902
                case 'C':
 
1903
                        daemon_check = 0;
 
1904
                        break;
 
1905
 
1925
1906
                case '?':
1926
1907
                case ':':
1927
1908
                        printf("%s: Ambiguous or unknown options\n", program);
1965
1946
                exit(exit_code);
1966
1947
        }
1967
1948
 
1968
 
        if (is_automount_running() > 0) {
1969
 
                fprintf(stderr, "%s: program is already running.\n",
1970
 
                        program);
1971
 
                exit(1);
1972
 
        }
1973
1949
#if 0
1974
1950
        if (!load_autofs4_module()) {
1975
1951
                fprintf(stderr, "%s: can't load %s filesystem module.\n",
1980
1956
 
1981
1957
        if (!query_kproto_ver() || get_kver_major() < 5) {
1982
1958
                fprintf(stderr,
1983
 
                        "%s: kernel protocol version 5.00 or above required.\n",
 
1959
                        "%s: test mount forbidden or "
 
1960
                        "incorrect kernel protocol version, "
 
1961
                        "kernel protocol version 5.00 or above required.\n",
1984
1962
                        program);
1985
1963
                exit(1);
1986
1964
        }
2001
1979
                     "can't increase core file limit - continuing");
2002
1980
#endif
2003
1981
 
2004
 
        become_daemon(foreground);
 
1982
        become_daemon(foreground, daemon_check);
2005
1983
 
2006
1984
        if (argc == 0)
2007
1985
                master_list = master_new(NULL, timeout, ghost);
2012
1990
                logerr("%s: can't create master map %s",
2013
1991
                        program, argv[0]);
2014
1992
                close(start_pipefd[1]);
 
1993
                release_flag_file();
2015
1994
                exit(1);
2016
1995
        }
2017
1996
 
2019
1998
                logerr("%s: failed to init thread attribute struct!",
2020
1999
                     program);
2021
2000
                close(start_pipefd[1]);
 
2001
                release_flag_file();
2022
2002
                exit(1);
2023
2003
        }
2024
2004
 
2027
2007
                logerr("%s: failed to set detached thread attribute!",
2028
2008
                     program);
2029
2009
                close(start_pipefd[1]);
 
2010
                release_flag_file();
2030
2011
                exit(1);
2031
2012
        }
2032
2013
 
2036
2017
                logerr("%s: failed to set stack size thread attribute!",
2037
2018
                       program);
2038
2019
                close(start_pipefd[1]);
 
2020
                release_flag_file();
2039
2021
                exit(1);
2040
2022
        }
2041
2023
#endif
2052
2034
                       program);
2053
2035
                master_kill(master_list);
2054
2036
                close(start_pipefd[1]);
 
2037
                release_flag_file();
2055
2038
                exit(1);
2056
2039
        }
2057
2040
 
 
2041
        init_ioctl_ctl();
 
2042
 
2058
2043
        if (!alarm_start_handler()) {
2059
2044
                logerr("%s: failed to create alarm handler thread!", program);
2060
2045
                master_kill(master_list);
2061
2046
                close(start_pipefd[1]);
 
2047
                release_flag_file();
2062
2048
                exit(1);
2063
2049
        }
2064
2050
 
2066
2052
                logerr("%s: failed to create FSM handler thread!", program);
2067
2053
                master_kill(master_list);
2068
2054
                close(start_pipefd[1]);
 
2055
                release_flag_file();
2069
2056
                exit(1);
2070
2057
        }
2071
2058
 
2078
2065
                *pst_stat = 3;
2079
2066
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2080
2067
                close(start_pipefd[1]);
 
2068
                release_flag_file();
2081
2069
                exit(3);
2082
2070
        }
2083
2071
 
2094
2082
                pid_file = NULL;
2095
2083
        }
2096
2084
        closelog();
 
2085
        release_flag_file();
2097
2086
 
2098
2087
#ifdef LIBXML2_WORKAROUND
2099
2088
        if (dh)
2100
2089
                dlclose(dh);
2101
2090
#endif
 
2091
        close_ioctl_ctl();
 
2092
 
2102
2093
        info(logging, "autofs stopped");
2103
2094
 
2104
2095
        exit(0);