/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to dracut-module/password-agent.c

  • Committer: Teddy Hogeborn
  • Date: 2019-08-03 11:52:41 UTC
  • Revision ID: teddy@recompile.se-20190803115241-oe9726lcojsaij33
dracut-module/password-agent.c: Require agent directory

Require the --agent-directory (by default "/run/systemd/ask-password")
to be an actual directory; fail otherwise.

* dracut-module/password-agent.c (main): Make ENOTDIR from
  add_inotify_dir_watch() result in EX_OSFILE.
  (add_inotify_dir_watch): Add "IN_ONLYDIR" flag to
                           inotify_add_watch().
  (test_add_inotify_dir_watch_nondir): New test.
  (run_tests): Add new test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
431
431
    case EACCES:
432
432
    case ENAMETOOLONG:
433
433
    case ENOENT:
 
434
    case ENOTDIR:
434
435
      return EX_OSFILE;
435
436
    default:
436
437
      return EX_OSERR;
1017
1018
    return false;
1018
1019
  }
1019
1020
 
1020
 
  if(inotify_add_watch(fd, dir, IN_CLOSE_WRITE
1021
 
                       | IN_MOVED_TO | IN_DELETE)
 
1021
  if(inotify_add_watch(fd, dir, IN_CLOSE_WRITE | IN_MOVED_TO
 
1022
                       | IN_MOVED_FROM| IN_DELETE | IN_EXCL_UNLINK
 
1023
                       | IN_ONLYDIR)
1022
1024
     == -1){
1023
1025
    error(0, errno, "Failed to create inotify watch on %s", dir);
1024
1026
    return false;
1071
1073
  /* "sufficient to read at least one event." - inotify(7) */
1072
1074
  const size_t ievent_size = (sizeof(struct inotify_event)
1073
1075
                              + NAME_MAX + 1);
1074
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
1075
 
  struct inotify_event *ievent = ((struct inotify_event *)
1076
 
                                  ievent_buffer);
 
1076
  struct {
 
1077
    struct inotify_event event;
 
1078
    char name_buffer[NAME_MAX + 1];
 
1079
  } ievent_buffer;
 
1080
  struct inotify_event *const ievent = &ievent_buffer.event;
1077
1081
 
1078
1082
  const ssize_t read_length = read(fd, ievent, ievent_size);
1079
1083
  if(read_length == 0){ /* EOF */
1117
1121
             immediately */
1118
1122
          queue->next_run = 1;
1119
1123
        }
1120
 
      } else if(ievent->mask & IN_DELETE){
 
1124
      } else if(ievent->mask & (IN_MOVED_FROM | IN_DELETE)){
1121
1125
        if(not string_set_add(cancelled_filenames,
1122
1126
                              question_filename)){
1123
1127
          error(0, errno, "Could not add question %s to"
3450
3454
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3451
3455
}
3452
3456
 
 
3457
static void test_add_inotify_dir_watch_nondir(__attribute__((unused))
 
3458
                                              test_fixture *fixture,
 
3459
                                            __attribute__((unused))
 
3460
                                              gconstpointer
 
3461
                                              user_data){
 
3462
  __attribute__((cleanup(cleanup_close)))
 
3463
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
3464
  g_assert_cmpint(epoll_fd, >=, 0);
 
3465
  __attribute__((cleanup(cleanup_queue)))
 
3466
    task_queue *queue = create_queue();
 
3467
  g_assert_nonnull(queue);
 
3468
  __attribute__((cleanup(string_set_clear)))
 
3469
    string_set cancelled_filenames = {};
 
3470
  const mono_microsecs current_time = 0;
 
3471
 
 
3472
  bool quit_now = false;
 
3473
  buffer password = {};
 
3474
  bool mandos_client_exited = false;
 
3475
  bool password_is_read = false;
 
3476
 
 
3477
  const char not_a_directory[] = "/dev/tty";
 
3478
 
 
3479
  FILE *real_stderr = stderr;
 
3480
  FILE *devnull = fopen("/dev/null", "we");
 
3481
  g_assert_nonnull(devnull);
 
3482
  stderr = devnull;
 
3483
  g_assert_false(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
 
3484
                                       &password, not_a_directory,
 
3485
                                       &cancelled_filenames,
 
3486
                                       &current_time,
 
3487
                                       &mandos_client_exited,
 
3488
                                       &password_is_read));
 
3489
  stderr = real_stderr;
 
3490
  g_assert_cmpint(fclose(devnull), ==, 0);
 
3491
 
 
3492
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
 
3493
}
 
3494
 
3453
3495
static void test_add_inotify_dir_watch_EAGAIN(__attribute__((unused))
3454
3496
                                              test_fixture *fixture,
3455
3497
                                              __attribute__((unused))
3670
3712
}
3671
3713
 
3672
3714
static
 
3715
void test_add_inotify_dir_watch_IN_MOVED_FROM(__attribute__((unused))
 
3716
                                              test_fixture *fixture,
 
3717
                                              __attribute__((unused))
 
3718
                                              gconstpointer
 
3719
                                              user_data){
 
3720
  __attribute__((cleanup(cleanup_close)))
 
3721
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
3722
  g_assert_cmpint(epoll_fd, >=, 0);
 
3723
  __attribute__((cleanup(cleanup_queue)))
 
3724
    task_queue *queue = create_queue();
 
3725
  g_assert_nonnull(queue);
 
3726
  __attribute__((cleanup(string_set_clear)))
 
3727
    string_set cancelled_filenames = {};
 
3728
  const mono_microsecs current_time = 0;
 
3729
 
 
3730
  bool quit_now = false;
 
3731
  buffer password = {};
 
3732
  bool mandos_client_exited = false;
 
3733
  bool password_is_read = false;
 
3734
 
 
3735
  __attribute__((cleanup(cleanup_string)))
 
3736
    char *tempdir = make_temporary_directory();
 
3737
  g_assert_nonnull(tempdir);
 
3738
 
 
3739
  __attribute__((cleanup(cleanup_string)))
 
3740
    char *tempfilename = make_temporary_file_in_directory(tempdir);
 
3741
  g_assert_nonnull(tempfilename);
 
3742
 
 
3743
  __attribute__((cleanup(cleanup_string)))
 
3744
    char *targetdir = make_temporary_directory();
 
3745
  g_assert_nonnull(targetdir);
 
3746
 
 
3747
  __attribute__((cleanup(cleanup_string)))
 
3748
    char *targetfilename = NULL;
 
3749
  g_assert_cmpint(asprintf(&targetfilename, "%s/%s", targetdir,
 
3750
                           basename(tempfilename)), >, 0);
 
3751
  g_assert_nonnull(targetfilename);
 
3752
 
 
3753
  g_assert_true(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
 
3754
                                      &password, tempdir,
 
3755
                                      &cancelled_filenames,
 
3756
                                      &current_time,
 
3757
                                      &mandos_client_exited,
 
3758
                                      &password_is_read));
 
3759
 
 
3760
  g_assert_cmpint(rename(tempfilename, targetfilename), ==, 0);
 
3761
 
 
3762
  const task_context *const added_read_task
 
3763
    = find_matching_task(queue,
 
3764
                         (task_context){ .func=read_inotify_event });
 
3765
  g_assert_nonnull(added_read_task);
 
3766
 
 
3767
  /* "sufficient to read at least one event." - inotify(7) */
 
3768
  const size_t ievent_size = (sizeof(struct inotify_event)
 
3769
                              + NAME_MAX + 1);
 
3770
  struct inotify_event *ievent = malloc(ievent_size);
 
3771
  g_assert_nonnull(ievent);
 
3772
 
 
3773
  ssize_t read_size = read(added_read_task->fd, ievent, ievent_size);
 
3774
 
 
3775
  g_assert_cmpint((int)read_size, >, 0);
 
3776
  g_assert_true(ievent->mask & IN_MOVED_FROM);
 
3777
  g_assert_cmpstr(ievent->name, ==, basename(tempfilename));
 
3778
 
 
3779
  free(ievent);
 
3780
 
 
3781
  g_assert_cmpint(unlink(targetfilename), ==, 0);
 
3782
  g_assert_cmpint(rmdir(targetdir), ==, 0);
 
3783
  g_assert_cmpint(rmdir(tempdir), ==, 0);
 
3784
}
 
3785
 
 
3786
static
3673
3787
void test_add_inotify_dir_watch_IN_DELETE(__attribute__((unused))
3674
3788
                                          test_fixture *fixture,
3675
3789
                                          __attribute__((unused))
3733
3847
  g_assert_cmpint(rmdir(tempdir), ==, 0);
3734
3848
}
3735
3849
 
 
3850
static
 
3851
void test_add_inotify_dir_watch_IN_EXCL_UNLINK(__attribute__((unused))
 
3852
                                               test_fixture *fixture,
 
3853
                                               __attribute__((unused))
 
3854
                                               gconstpointer
 
3855
                                               user_data){
 
3856
  __attribute__((cleanup(cleanup_close)))
 
3857
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
3858
  g_assert_cmpint(epoll_fd, >=, 0);
 
3859
  __attribute__((cleanup(cleanup_queue)))
 
3860
    task_queue *queue = create_queue();
 
3861
  g_assert_nonnull(queue);
 
3862
  __attribute__((cleanup(string_set_clear)))
 
3863
    string_set cancelled_filenames = {};
 
3864
  const mono_microsecs current_time = 0;
 
3865
 
 
3866
  bool quit_now = false;
 
3867
  buffer password = {};
 
3868
  bool mandos_client_exited = false;
 
3869
  bool password_is_read = false;
 
3870
 
 
3871
  __attribute__((cleanup(cleanup_string)))
 
3872
    char *tempdir = make_temporary_directory();
 
3873
  g_assert_nonnull(tempdir);
 
3874
 
 
3875
  __attribute__((cleanup(cleanup_string)))
 
3876
    char *tempfile = make_temporary_file_in_directory(tempdir);
 
3877
  g_assert_nonnull(tempfile);
 
3878
  int tempfile_fd = open(tempfile, O_WRONLY | O_CLOEXEC | O_NOCTTY
 
3879
                         | O_NOFOLLOW);
 
3880
  g_assert_cmpint(tempfile_fd, >, 2);
 
3881
 
 
3882
  g_assert_true(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
 
3883
                                      &password, tempdir,
 
3884
                                      &cancelled_filenames,
 
3885
                                      &current_time,
 
3886
                                      &mandos_client_exited,
 
3887
                                      &password_is_read));
 
3888
  g_assert_cmpint(unlink(tempfile), ==, 0);
 
3889
 
 
3890
  g_assert_cmpuint((unsigned int)queue->length, >, 0);
 
3891
 
 
3892
  const task_context *const added_read_task
 
3893
    = find_matching_task(queue,
 
3894
                         (task_context){ .func=read_inotify_event });
 
3895
  g_assert_nonnull(added_read_task);
 
3896
 
 
3897
  g_assert_cmpint(added_read_task->fd, >, 2);
 
3898
  g_assert_true(fd_has_cloexec_and_nonblock(added_read_task->fd));
 
3899
 
 
3900
  /* "sufficient to read at least one event." - inotify(7) */
 
3901
  const size_t ievent_size = (sizeof(struct inotify_event)
 
3902
                              + NAME_MAX + 1);
 
3903
  struct inotify_event *ievent = malloc(ievent_size);
 
3904
  g_assert_nonnull(ievent);
 
3905
 
 
3906
  ssize_t read_size = 0;
 
3907
  read_size = read(added_read_task->fd, ievent, ievent_size);
 
3908
 
 
3909
  g_assert_cmpint((int)read_size, >, 0);
 
3910
  g_assert_true(ievent->mask & IN_DELETE);
 
3911
  g_assert_cmpstr(ievent->name, ==, basename(tempfile));
 
3912
 
 
3913
  g_assert_cmpint(close(tempfile_fd), ==, 0);
 
3914
 
 
3915
  /* IN_EXCL_UNLINK should make the closing of the previously unlinked
 
3916
     file not appear as an ievent, so we should not see it now. */
 
3917
  read_size = read(added_read_task->fd, ievent, ievent_size);
 
3918
  g_assert_cmpint((int)read_size, ==, -1);
 
3919
  g_assert_true(errno == EAGAIN);
 
3920
 
 
3921
  free(ievent);
 
3922
 
 
3923
  g_assert_cmpint(rmdir(tempdir), ==, 0);
 
3924
}
 
3925
 
3736
3926
static void test_read_inotify_event_readerror(__attribute__((unused))
3737
3927
                                              test_fixture *fixture,
3738
3928
                                              __attribute__((unused))
3929
4119
  const size_t ievent_max_size = (sizeof(struct inotify_event)
3930
4120
                                  + NAME_MAX + 1);
3931
4121
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
3932
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
3933
 
  struct inotify_event *ievent = ((struct inotify_event *)
3934
 
                                  ievent_buffer);
 
4122
  struct {
 
4123
    struct inotify_event event;
 
4124
    char name_buffer[NAME_MAX + 1];
 
4125
  } ievent_buffer;
 
4126
  struct inotify_event *const ievent = &ievent_buffer.event;
3935
4127
 
3936
4128
  const char dummy_file_name[] = "ask.dummy_file_name";
3937
4129
  ievent->mask = IN_CLOSE_WRITE;
3939
4131
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
3940
4132
  const size_t ievent_size = (sizeof(struct inotify_event)
3941
4133
                              + sizeof(dummy_file_name));
3942
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4134
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
3943
4135
                  ==, ievent_size);
3944
4136
  g_assert_cmpint(close(pipefds[1]), ==, 0);
3945
4137
 
4022
4214
  const size_t ievent_max_size = (sizeof(struct inotify_event)
4023
4215
                                  + NAME_MAX + 1);
4024
4216
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4025
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4026
 
  struct inotify_event *ievent = ((struct inotify_event *)
4027
 
                                  ievent_buffer);
 
4217
  struct {
 
4218
    struct inotify_event event;
 
4219
    char name_buffer[NAME_MAX + 1];
 
4220
  } ievent_buffer;
 
4221
  struct inotify_event *const ievent = &ievent_buffer.event;
4028
4222
 
4029
4223
  const char dummy_file_name[] = "ask.dummy_file_name";
4030
4224
  ievent->mask = IN_MOVED_TO;
4032
4226
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4033
4227
  const size_t ievent_size = (sizeof(struct inotify_event)
4034
4228
                              + sizeof(dummy_file_name));
4035
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4229
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4036
4230
                  ==, ievent_size);
4037
4231
  g_assert_cmpint(close(pipefds[1]), ==, 0);
4038
4232
 
4098
4292
      }));
4099
4293
}
4100
4294
 
 
4295
static
 
4296
void test_read_inotify_event_IN_MOVED_FROM(__attribute__((unused))
 
4297
                                           test_fixture *fixture,
 
4298
                                           __attribute__((unused))
 
4299
                                           gconstpointer user_data){
 
4300
  __attribute__((cleanup(cleanup_close)))
 
4301
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
4302
  g_assert_cmpint(epoll_fd, >=, 0);
 
4303
  __attribute__((cleanup(string_set_clear)))
 
4304
    string_set cancelled_filenames = {};
 
4305
  const mono_microsecs current_time = 0;
 
4306
 
 
4307
  int pipefds[2];
 
4308
  g_assert_cmpint(pipe2(pipefds, O_CLOEXEC | O_NONBLOCK), ==, 0);
 
4309
 
 
4310
  /* "sufficient to read at least one event." - inotify(7) */
 
4311
  const size_t ievent_max_size = (sizeof(struct inotify_event)
 
4312
                                  + NAME_MAX + 1);
 
4313
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
 
4314
  struct {
 
4315
    struct inotify_event event;
 
4316
    char name_buffer[NAME_MAX + 1];
 
4317
  } ievent_buffer;
 
4318
  struct inotify_event *const ievent = &ievent_buffer.event;
 
4319
 
 
4320
  const char dummy_file_name[] = "ask.dummy_file_name";
 
4321
  ievent->mask = IN_MOVED_FROM;
 
4322
  ievent->len = sizeof(dummy_file_name);
 
4323
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
 
4324
  const size_t ievent_size = (sizeof(struct inotify_event)
 
4325
                              + sizeof(dummy_file_name));
 
4326
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
 
4327
                  ==, ievent_size);
 
4328
  g_assert_cmpint(close(pipefds[1]), ==, 0);
 
4329
 
 
4330
  bool quit_now = false;
 
4331
  buffer password = {};
 
4332
  bool mandos_client_exited = false;
 
4333
  bool password_is_read = false;
 
4334
  __attribute__((cleanup(cleanup_queue)))
 
4335
    task_queue *queue = create_queue();
 
4336
  g_assert_nonnull(queue);
 
4337
 
 
4338
  task_context task = {
 
4339
    .func=read_inotify_event,
 
4340
    .epoll_fd=epoll_fd,
 
4341
    .fd=pipefds[0],
 
4342
    .quit_now=&quit_now,
 
4343
    .password=&password,
 
4344
    .filename=strdup("/nonexistent"),
 
4345
    .cancelled_filenames=&cancelled_filenames,
 
4346
    .current_time=&current_time,
 
4347
    .mandos_client_exited=&mandos_client_exited,
 
4348
    .password_is_read=&password_is_read,
 
4349
  };
 
4350
  task.func(task, queue);
 
4351
  g_assert_false(quit_now);
 
4352
  g_assert_true(queue->next_run == 0);
 
4353
  g_assert_cmpuint((unsigned int)queue->length, ==, 1);
 
4354
 
 
4355
  g_assert_nonnull(find_matching_task(queue, (task_context){
 
4356
        .func=read_inotify_event,
 
4357
        .epoll_fd=epoll_fd,
 
4358
        .fd=pipefds[0],
 
4359
        .quit_now=&quit_now,
 
4360
        .password=&password,
 
4361
        .filename=task.filename,
 
4362
        .cancelled_filenames=&cancelled_filenames,
 
4363
        .current_time=&current_time,
 
4364
        .mandos_client_exited=&mandos_client_exited,
 
4365
        .password_is_read=&password_is_read,
 
4366
      }));
 
4367
 
 
4368
  g_assert_true(epoll_set_contains(epoll_fd, pipefds[0],
 
4369
                                   EPOLLIN | EPOLLRDHUP));
 
4370
 
 
4371
  __attribute__((cleanup(cleanup_string)))
 
4372
    char *filename = NULL;
 
4373
  g_assert_cmpint(asprintf(&filename, "%s/%s", task.filename,
 
4374
                           dummy_file_name), >, 0);
 
4375
  g_assert_nonnull(filename);
 
4376
  g_assert_true(string_set_contains(*task.cancelled_filenames,
 
4377
                                    filename));
 
4378
}
 
4379
 
4101
4380
static void test_read_inotify_event_IN_DELETE(__attribute__((unused))
4102
4381
                                              test_fixture *fixture,
4103
4382
                                              __attribute__((unused))
4117
4396
  const size_t ievent_max_size = (sizeof(struct inotify_event)
4118
4397
                                  + NAME_MAX + 1);
4119
4398
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4120
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4121
 
  struct inotify_event *ievent = ((struct inotify_event *)
4122
 
                                  ievent_buffer);
 
4399
  struct {
 
4400
    struct inotify_event event;
 
4401
    char name_buffer[NAME_MAX + 1];
 
4402
  } ievent_buffer;
 
4403
  struct inotify_event *const ievent = &ievent_buffer.event;
4123
4404
 
4124
4405
  const char dummy_file_name[] = "ask.dummy_file_name";
4125
4406
  ievent->mask = IN_DELETE;
4127
4408
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4128
4409
  const size_t ievent_size = (sizeof(struct inotify_event)
4129
4410
                              + sizeof(dummy_file_name));
4130
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4411
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4131
4412
                  ==, ievent_size);
4132
4413
  g_assert_cmpint(close(pipefds[1]), ==, 0);
4133
4414
 
4199
4480
  const size_t ievent_max_size = (sizeof(struct inotify_event)
4200
4481
                                  + NAME_MAX + 1);
4201
4482
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4202
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4203
 
  struct inotify_event *ievent = ((struct inotify_event *)
4204
 
                                  ievent_buffer);
 
4483
  struct {
 
4484
    struct inotify_event event;
 
4485
    char name_buffer[NAME_MAX + 1];
 
4486
  } ievent_buffer;
 
4487
  struct inotify_event *const ievent = &ievent_buffer.event;
4205
4488
 
4206
4489
  const char dummy_file_name[] = "ignored.dummy_file_name";
4207
4490
  ievent->mask = IN_CLOSE_WRITE;
4209
4492
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4210
4493
  const size_t ievent_size = (sizeof(struct inotify_event)
4211
4494
                              + sizeof(dummy_file_name));
4212
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4495
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4213
4496
                  ==, ievent_size);
4214
4497
  g_assert_cmpint(close(pipefds[1]), ==, 0);
4215
4498
 
4273
4556
  const size_t ievent_max_size = (sizeof(struct inotify_event)
4274
4557
                                  + NAME_MAX + 1);
4275
4558
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4276
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4277
 
  struct inotify_event *ievent = ((struct inotify_event *)
4278
 
                                  ievent_buffer);
 
4559
  struct {
 
4560
    struct inotify_event event;
 
4561
    char name_buffer[NAME_MAX + 1];
 
4562
  } ievent_buffer;
 
4563
  struct inotify_event *const ievent = &ievent_buffer.event;
4279
4564
 
4280
4565
  const char dummy_file_name[] = "ignored.dummy_file_name";
4281
4566
  ievent->mask = IN_MOVED_TO;
4283
4568
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4284
4569
  const size_t ievent_size = (sizeof(struct inotify_event)
4285
4570
                              + sizeof(dummy_file_name));
4286
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4571
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4287
4572
                  ==, ievent_size);
4288
4573
  g_assert_cmpint(close(pipefds[1]), ==, 0);
4289
4574
 
4330
4615
                                   EPOLLIN | EPOLLRDHUP));
4331
4616
}
4332
4617
 
 
4618
static void
 
4619
test_read_inotify_event_IN_MOVED_FROM_badname(__attribute__((unused))
 
4620
                                              test_fixture *fixture,
 
4621
                                              __attribute__((unused))
 
4622
                                              gconstpointer
 
4623
                                              user_data){
 
4624
  __attribute__((cleanup(cleanup_close)))
 
4625
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
4626
  g_assert_cmpint(epoll_fd, >=, 0);
 
4627
  __attribute__((cleanup(string_set_clear)))
 
4628
    string_set cancelled_filenames = {};
 
4629
  const mono_microsecs current_time = 0;
 
4630
 
 
4631
  int pipefds[2];
 
4632
  g_assert_cmpint(pipe2(pipefds, O_CLOEXEC | O_NONBLOCK), ==, 0);
 
4633
 
 
4634
  /* "sufficient to read at least one event." - inotify(7) */
 
4635
  const size_t ievent_max_size = (sizeof(struct inotify_event)
 
4636
                                  + NAME_MAX + 1);
 
4637
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
 
4638
  struct {
 
4639
    struct inotify_event event;
 
4640
    char name_buffer[NAME_MAX + 1];
 
4641
  } ievent_buffer;
 
4642
  struct inotify_event *const ievent = &ievent_buffer.event;
 
4643
 
 
4644
  const char dummy_file_name[] = "ignored.dummy_file_name";
 
4645
  ievent->mask = IN_MOVED_FROM;
 
4646
  ievent->len = sizeof(dummy_file_name);
 
4647
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
 
4648
  const size_t ievent_size = (sizeof(struct inotify_event)
 
4649
                              + sizeof(dummy_file_name));
 
4650
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
 
4651
                  ==, ievent_size);
 
4652
  g_assert_cmpint(close(pipefds[1]), ==, 0);
 
4653
 
 
4654
  bool quit_now = false;
 
4655
  buffer password = {};
 
4656
  bool mandos_client_exited = false;
 
4657
  bool password_is_read = false;
 
4658
  __attribute__((cleanup(cleanup_queue)))
 
4659
    task_queue *queue = create_queue();
 
4660
  g_assert_nonnull(queue);
 
4661
 
 
4662
  task_context task = {
 
4663
    .func=read_inotify_event,
 
4664
    .epoll_fd=epoll_fd,
 
4665
    .fd=pipefds[0],
 
4666
    .quit_now=&quit_now,
 
4667
    .password=&password,
 
4668
    .filename=strdup("/nonexistent"),
 
4669
    .cancelled_filenames=&cancelled_filenames,
 
4670
    .current_time=&current_time,
 
4671
    .mandos_client_exited=&mandos_client_exited,
 
4672
    .password_is_read=&password_is_read,
 
4673
  };
 
4674
  task.func(task, queue);
 
4675
  g_assert_false(quit_now);
 
4676
  g_assert_true(queue->next_run == 0);
 
4677
  g_assert_cmpuint((unsigned int)queue->length, ==, 1);
 
4678
 
 
4679
  g_assert_nonnull(find_matching_task(queue, (task_context){
 
4680
        .func=read_inotify_event,
 
4681
        .epoll_fd=epoll_fd,
 
4682
        .fd=pipefds[0],
 
4683
        .quit_now=&quit_now,
 
4684
        .password=&password,
 
4685
        .filename=task.filename,
 
4686
        .cancelled_filenames=&cancelled_filenames,
 
4687
        .current_time=&current_time,
 
4688
        .mandos_client_exited=&mandos_client_exited,
 
4689
        .password_is_read=&password_is_read,
 
4690
      }));
 
4691
 
 
4692
  g_assert_true(epoll_set_contains(epoll_fd, pipefds[0],
 
4693
                                   EPOLLIN | EPOLLRDHUP));
 
4694
 
 
4695
  __attribute__((cleanup(cleanup_string)))
 
4696
    char *filename = NULL;
 
4697
  g_assert_cmpint(asprintf(&filename, "%s/%s", task.filename,
 
4698
                           dummy_file_name), >, 0);
 
4699
  g_assert_nonnull(filename);
 
4700
  g_assert_false(string_set_contains(cancelled_filenames, filename));
 
4701
}
 
4702
 
4333
4703
static
4334
4704
void test_read_inotify_event_IN_DELETE_badname(__attribute__((unused))
4335
4705
                                               test_fixture *fixture,
4350
4720
  const size_t ievent_max_size = (sizeof(struct inotify_event)
4351
4721
                                  + NAME_MAX + 1);
4352
4722
  g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4353
 
  char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4354
 
  struct inotify_event *ievent = ((struct inotify_event *)
4355
 
                                  ievent_buffer);
 
4723
  struct {
 
4724
    struct inotify_event event;
 
4725
    char name_buffer[NAME_MAX + 1];
 
4726
  } ievent_buffer;
 
4727
  struct inotify_event *const ievent = &ievent_buffer.event;
4356
4728
 
4357
4729
  const char dummy_file_name[] = "ignored.dummy_file_name";
4358
4730
  ievent->mask = IN_DELETE;
4360
4732
  memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4361
4733
  const size_t ievent_size = (sizeof(struct inotify_event)
4362
4734
                              + sizeof(dummy_file_name));
4363
 
  g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
 
4735
  g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4364
4736
                  ==, ievent_size);
4365
4737
  g_assert_cmpint(close(pipefds[1]), ==, 0);
4366
4738
 
7573
7945
              test_add_inotify_dir_watch);
7574
7946
  test_add_st("/task-creators/add_inotify_dir_watch/fail",
7575
7947
              test_add_inotify_dir_watch_fail);
 
7948
  test_add_st("/task-creators/add_inotify_dir_watch/not-a-directory",
 
7949
              test_add_inotify_dir_watch_nondir);
7576
7950
  test_add_st("/task-creators/add_inotify_dir_watch/EAGAIN",
7577
7951
              test_add_inotify_dir_watch_EAGAIN);
7578
7952
  test_add_st("/task-creators/add_inotify_dir_watch/IN_CLOSE_WRITE",
7579
7953
              test_add_inotify_dir_watch_IN_CLOSE_WRITE);
7580
7954
  test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_TO",
7581
7955
              test_add_inotify_dir_watch_IN_MOVED_TO);
 
7956
  test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_FROM",
 
7957
              test_add_inotify_dir_watch_IN_MOVED_FROM);
 
7958
  test_add_st("/task-creators/add_inotify_dir_watch/IN_EXCL_UNLINK",
 
7959
              test_add_inotify_dir_watch_IN_EXCL_UNLINK);
7582
7960
  test_add_st("/task-creators/add_inotify_dir_watch/IN_DELETE",
7583
7961
              test_add_inotify_dir_watch_IN_DELETE);
7584
7962
  test_add_st("/task/read_inotify_event/readerror",
7593
7971
              test_read_inotify_event_IN_CLOSE_WRITE);
7594
7972
  test_add_st("/task/read_inotify_event/IN_MOVED_TO",
7595
7973
              test_read_inotify_event_IN_MOVED_TO);
 
7974
  test_add_st("/task/read_inotify_event/IN_MOVED_FROM",
 
7975
              test_read_inotify_event_IN_MOVED_FROM);
7596
7976
  test_add_st("/task/read_inotify_event/IN_DELETE",
7597
7977
              test_read_inotify_event_IN_DELETE);
7598
7978
  test_add_st("/task/read_inotify_event/IN_CLOSE_WRITE/badname",
7599
7979
              test_read_inotify_event_IN_CLOSE_WRITE_badname);
7600
7980
  test_add_st("/task/read_inotify_event/IN_MOVED_TO/badname",
7601
7981
              test_read_inotify_event_IN_MOVED_TO_badname);
 
7982
  test_add_st("/task/read_inotify_event/IN_MOVED_FROM/badname",
 
7983
              test_read_inotify_event_IN_MOVED_FROM_badname);
7602
7984
  test_add_st("/task/read_inotify_event/IN_DELETE/badname",
7603
7985
              test_read_inotify_event_IN_DELETE_badname);
7604
7986
  test_add_st("/task/open_and_parse_question/ENOENT",