Coverage Report

Created: 2025-11-07 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lvm2/libdm/libdm-common.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3
 * Copyright (C) 2004-2025 Red Hat, Inc. All rights reserved.
4
 *
5
 * This file is part of the device-mapper userspace tools.
6
 *
7
 * This copyrighted material is made available to anyone wishing to use,
8
 * modify, copy, or redistribute it subject to the terms and conditions
9
 * of the GNU Lesser General Public License v.2.1.
10
 *
11
 * You should have received a copy of the GNU Lesser General Public License
12
 * along with this program; if not, write to the Free Software Foundation,
13
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14
 */
15
16
#include "libdm/misc/dmlib.h"
17
#include "libdm/ioctl/libdm-targets.h"
18
#include "libdm-common.h"
19
#include "libdm/misc/kdev_t.h"
20
#include "libdm/misc/dm-ioctl.h"
21
22
#include <stdarg.h>
23
#include <sys/param.h>
24
#include <sys/ioctl.h>
25
#include <fcntl.h>
26
#include <dirent.h>
27
28
#ifdef UDEV_SYNC_SUPPORT
29
#  include <sys/types.h>
30
#  include <sys/ipc.h>
31
#  include <sys/sem.h>
32
#  include <libudev.h>
33
#endif
34
35
#ifdef __linux__
36
#  include <linux/fs.h>
37
#endif
38
39
#ifdef HAVE_SELINUX
40
#  include <selinux/selinux.h>
41
#endif
42
#ifdef HAVE_SELINUX_LABEL_H
43
#  include <selinux/label.h>
44
#endif
45
46
2
#define DM_DEFAULT_NAME_MANGLING_MODE_ENV_VAR_NAME "DM_DEFAULT_NAME_MANGLING_MODE"
47
48
0
#define DEV_DIR "/dev/"
49
50
#ifdef UDEV_SYNC_SUPPORT
51
#ifdef _SEM_SEMUN_UNDEFINED
52
union semun
53
{
54
  int val;      /* value for SETVAL */
55
  struct semid_ds *buf;   /* buffer for IPC_STAT & IPC_SET */
56
  unsigned short int *array;  /* array for GETALL & SETALL */
57
  struct seminfo *__buf;    /* buffer for IPC_INFO */
58
};
59
#endif
60
#endif
61
62
static char _dm_dir[PATH_MAX] = DEV_DIR DM_DIR;
63
static char _sysfs_dir[PATH_MAX] = "/sys/";
64
static char _path0[PATH_MAX];           /* path buffer, safe 4kB on stack */
65
static const char _mountinfo[] = "/proc/self/mountinfo";
66
67
0
#define DM_MAX_UUID_PREFIX_LEN  15
68
static char _default_uuid_prefix[DM_MAX_UUID_PREFIX_LEN + 1] = "LVM-";
69
70
static int _verbose = 0;
71
static int _suspended_dev_counter = 0;
72
static dm_string_mangling_t _name_mangling_mode = DEFAULT_DM_NAME_MANGLING;
73
74
#ifdef HAVE_SELINUX_LABEL_H
75
static struct selabel_handle *_selabel_handle = NULL;
76
#endif
77
78
static int _udev_disabled = 0;
79
80
#ifdef UDEV_SYNC_SUPPORT
81
static int _semaphore_supported = -1;
82
static int _udev_running = -1;
83
static int _sync_with_udev = 1;
84
static int _udev_checking = 1;
85
#endif
86
87
void dm_lib_init(void)
88
2
{
89
2
  const char *env;
90
91
2
  if (getenv("DM_DISABLE_UDEV"))
92
0
    _udev_disabled = 1;
93
94
2
  _name_mangling_mode = DEFAULT_DM_NAME_MANGLING;
95
2
  if ((env = getenv(DM_DEFAULT_NAME_MANGLING_MODE_ENV_VAR_NAME))) {
96
0
    if (!strcasecmp(env, "none"))
97
0
      _name_mangling_mode = DM_STRING_MANGLING_NONE;
98
0
    else if (!strcasecmp(env, "auto"))
99
0
      _name_mangling_mode = DM_STRING_MANGLING_AUTO;
100
0
    else if (!strcasecmp(env, "hex"))
101
0
      _name_mangling_mode = DM_STRING_MANGLING_HEX;
102
0
  }
103
2
}
104
105
/*
106
 * Library users can provide their own logging
107
 * function.
108
 */
109
110
__attribute__((format(printf, 5, 0)))
111
static void _default_log_line(int level, const char *file,
112
            int line, int dm_errno_or_class,
113
            const char *f, va_list ap)
114
0
{
115
0
  static int _abort_on_internal_errors = -1;
116
0
  static int _debug_with_line_numbers = -1;
117
0
  FILE *out = log_stderr(level) ? stderr : stdout;
118
119
0
  level = log_level(level);
120
121
0
  if (level <= _LOG_WARN || _verbose) {
122
0
    if (level < _LOG_WARN)
123
0
      out = stderr;
124
125
0
    if (_debug_with_line_numbers < 0)
126
      /* Set when env DM_DEBUG_WITH_LINE_NUMBERS is not "0" */
127
0
      _debug_with_line_numbers =
128
0
        strcmp(getenv("DM_DEBUG_WITH_LINE_NUMBERS") ? : "0", "0");
129
130
0
    if (_debug_with_line_numbers)
131
0
      fprintf(out, "%s:%d     ", file, line);
132
133
0
    vfprintf(out, f, ap);
134
0
    fputc('\n', out);
135
0
  }
136
137
0
  if (_abort_on_internal_errors < 0)
138
    /* Set when env DM_ABORT_ON_INTERNAL_ERRORS is not "0" */
139
0
    _abort_on_internal_errors =
140
0
      strcmp(getenv("DM_ABORT_ON_INTERNAL_ERRORS") ? : "0", "0");
141
142
0
  if (_abort_on_internal_errors &&
143
0
      !strncmp(f, INTERNAL_ERROR, sizeof(INTERNAL_ERROR) - 1))
144
0
    abort();
145
0
}
146
147
__attribute__((format(printf, 5, 6)))
148
static void _default_log_with_errno(int level,
149
      const char *file, int line, int dm_errno_or_class,
150
      const char *f, ...)
151
0
{
152
0
  va_list ap;
153
154
0
  va_start(ap, f);
155
0
  _default_log_line(level, file, line, dm_errno_or_class, f, ap);
156
0
  va_end(ap);
157
0
}
158
159
__attribute__((format(printf, 4, 5)))
160
static void _default_log(int level, const char *file,
161
       int line, const char *f, ...)
162
0
{
163
0
  va_list ap;
164
165
0
  va_start(ap, f);
166
0
  _default_log_line(level, file, line, 0, f, ap);
167
0
  va_end(ap);
168
0
}
169
170
dm_log_fn dm_log = _default_log;
171
dm_log_with_errno_fn dm_log_with_errno = _default_log_with_errno;
172
173
/*
174
 * Wrapper function to reformat new messages to and
175
 * old style logging which had not used errno parameter
176
 *
177
 * As we cannot simply pass '...' to old function we
178
 * need to process arg list locally and just pass '%s' + buffer
179
 */
180
__attribute__((format(printf, 5, 6)))
181
static void _log_to_default_log(int level,
182
      const char *file, int line, int dm_errno_or_class,
183
      const char *f, ...)
184
0
{
185
0
  int n;
186
0
  va_list ap;
187
0
  char buf[2 * PATH_MAX + 256]; /* big enough for most messages */
188
189
0
  va_start(ap, f);
190
0
  n = vsnprintf(buf, sizeof(buf), f, ap);
191
0
  va_end(ap);
192
193
0
  if (n > 0) /* Could be truncated */
194
0
    dm_log(level, file, line, "%s", buf);
195
0
}
196
197
/*
198
 * Wrapper function take 'old' style message without errno
199
 * and log it via new logging function with errno arg
200
 *
201
 * This minor case may happen if new libdm is used with old
202
 * recompiled tool that would decided to use new logging,
203
 * but still would like to use old binary plugins.
204
 */
205
__attribute__((format(printf, 4, 5)))
206
static void _log_to_default_log_with_errno(int level,
207
      const char *file, int line, const char *f, ...)
208
0
{
209
0
  int n;
210
0
  va_list ap;
211
0
  char buf[2 * PATH_MAX + 256]; /* big enough for most messages */
212
213
0
  va_start(ap, f);
214
0
  n = vsnprintf(buf, sizeof(buf), f, ap);
215
0
  va_end(ap);
216
217
0
  if (n > 0) /* Could be truncated */
218
0
    dm_log_with_errno(level, file, line, 0, "%s", buf);
219
0
}
220
221
void dm_log_init(dm_log_fn fn)
222
2.92k
{
223
2.92k
  if (fn)  {
224
1.46k
    dm_log = fn;
225
1.46k
    dm_log_with_errno = _log_to_default_log;
226
1.46k
  } else {
227
1.46k
    dm_log = _default_log;
228
1.46k
    dm_log_with_errno = _default_log_with_errno;
229
1.46k
  }
230
2.92k
}
231
232
int dm_log_is_non_default(void)
233
0
{
234
0
  return (dm_log == _default_log && dm_log_with_errno == _default_log_with_errno) ? 0 : 1;
235
0
}
236
237
void dm_log_with_errno_init(dm_log_with_errno_fn fn)
238
0
{
239
0
  if (fn) {
240
0
    dm_log = _log_to_default_log_with_errno;
241
0
    dm_log_with_errno = fn;
242
0
  } else {
243
0
    dm_log = _default_log;
244
0
    dm_log_with_errno = _default_log_with_errno;
245
0
  }
246
0
}
247
248
void dm_log_init_verbose(int level)
249
2.92k
{
250
2.92k
  _verbose = level;
251
2.92k
}
252
253
static int _build_dev_path(char *buffer, size_t len, const char *dev_name)
254
0
{
255
0
  int r;
256
257
  /* If there's a /, assume caller knows what they're doing */
258
0
  if (strchr(dev_name, '/'))
259
0
    r = dm_strncpy(buffer, dev_name, len);
260
0
  else
261
0
    r = (dm_snprintf(buffer, len, "%s/%s",
262
0
         _dm_dir, dev_name) < 0) ? 0 : 1;
263
0
  if (!r)
264
0
    log_error("Failed to build dev path for \"%s\".", dev_name);
265
266
0
  return r;
267
0
}
268
269
int dm_get_library_version(char *version, size_t size)
270
0
{
271
0
  return dm_strncpy(version, DM_LIB_VERSION, size);
272
0
}
273
274
void inc_suspended(void)
275
0
{
276
0
  _suspended_dev_counter++;
277
0
  log_debug_activation("Suspended device counter increased to %d", _suspended_dev_counter);
278
0
}
279
280
void dec_suspended(void)
281
0
{
282
0
  if (!_suspended_dev_counter) {
283
0
    log_error("Attempted to decrement suspended device counter below zero.");
284
0
    return;
285
0
  }
286
287
0
  _suspended_dev_counter--;
288
0
  log_debug_activation("Suspended device counter reduced to %d", _suspended_dev_counter);
289
0
}
290
291
int dm_get_suspended_counter(void)
292
0
{
293
0
  return _suspended_dev_counter;
294
0
}
295
296
int dm_set_name_mangling_mode(dm_string_mangling_t name_mangling_mode)
297
0
{
298
0
  _name_mangling_mode = name_mangling_mode;
299
300
0
  return 1;
301
0
}
302
303
dm_string_mangling_t dm_get_name_mangling_mode(void)
304
0
{
305
0
  return _name_mangling_mode;
306
0
}
307
308
struct dm_task *dm_task_create(int type)
309
0
{
310
0
  struct dm_task *dmt = dm_zalloc(sizeof(*dmt));
311
312
0
  if (!dmt) {
313
0
    log_error("dm_task_create: malloc(%" PRIsize_t ") failed",
314
0
        sizeof(*dmt));
315
0
    return NULL;
316
0
  }
317
318
0
  if (!dm_check_version()) {
319
0
    dm_free(dmt);
320
0
    return_NULL;
321
0
  }
322
323
0
  dmt->type = type;
324
0
  dmt->minor = -1;
325
0
  dmt->major = -1;
326
0
  dmt->allow_default_major_fallback = 1;
327
0
  dmt->uid = DM_DEVICE_UID;
328
0
  dmt->gid = DM_DEVICE_GID;
329
0
  dmt->mode = DM_DEVICE_MODE;
330
0
  dmt->no_open_count = 0;
331
0
  dmt->read_ahead = DM_READ_AHEAD_AUTO;
332
0
  dmt->read_ahead_flags = 0;
333
0
  dmt->event_nr = 0;
334
0
  dmt->cookie_set = 0;
335
0
  dmt->query_inactive_table = 0;
336
0
  dmt->new_uuid = 0;
337
0
  dmt->secure_data = 0;
338
0
  dmt->record_timestamp = 0;
339
0
  dmt->ima_measurement = 0;
340
341
0
  return dmt;
342
0
}
343
344
/*
345
 * Find the name associated with a given device number by scanning _dm_dir.
346
 */
347
static int _find_dm_name_of_device(dev_t st_rdev, char *buf, size_t buf_len)
348
0
{
349
0
  const char *name;
350
0
  char path[PATH_MAX];
351
0
  struct dirent *dirent;
352
0
  DIR *d;
353
0
  struct stat st;
354
0
  int r = 0;
355
356
0
  if (!(d = opendir(_dm_dir))) {
357
0
    log_sys_error("opendir", _dm_dir);
358
0
    return 0;
359
0
  }
360
361
0
  while ((dirent = readdir(d))) {
362
0
    name = dirent->d_name;
363
364
0
    if (!strcmp(name, ".") || !strcmp(name, ".."))
365
0
      continue;
366
367
0
    if (dm_snprintf(path, sizeof(path), "%s/%s", _dm_dir,
368
0
        name) == -1) {
369
0
      log_error("Couldn't create path for %s", name);
370
0
      continue;
371
0
    }
372
373
0
    if (stat(path, &st))
374
0
      continue;
375
376
0
    if (st.st_rdev == st_rdev) {
377
0
      strncpy(buf, name, buf_len);
378
0
      r = 1;
379
0
      break;
380
0
    }
381
0
  }
382
383
0
  if (closedir(d))
384
0
    log_sys_debug("closedir", _dm_dir);
385
386
0
  return r;
387
0
}
388
389
static int _is_whitelisted_char(char c)
390
0
{
391
  /*
392
   * Actually, DM supports any character in a device name.
393
   * This whitelist is just for proper integration with udev.
394
   */
395
0
        if ((c >= '0' && c <= '9') ||
396
0
            (c >= 'A' && c <= 'Z') ||
397
0
            (c >= 'a' && c <= 'z') ||
398
0
            strchr("#+-.:=@_", c) != NULL)
399
0
                return 1;
400
401
0
        return 0;
402
0
}
403
404
int check_multiple_mangled_string_allowed(const char *str, const char *str_name,
405
           dm_string_mangling_t mode)
406
0
{
407
0
  if (mode == DM_STRING_MANGLING_AUTO && strstr(str, "\\x5cx")) {
408
0
    log_error("The %s \"%s\" seems to be mangled more than once. "
409
0
        "This is not allowed in auto mode.", str_name, str);
410
0
    return 0;
411
0
  }
412
413
0
  return 1;
414
0
}
415
416
/*
417
 * Mangle all characters in the input string which are not on a whitelist
418
 * with '\xNN' format where NN is the hex value of the character.
419
 */
420
int mangle_string(const char *str, const char *str_name, size_t len,
421
      char *buf, size_t buf_len, dm_string_mangling_t mode)
422
0
{
423
0
  int need_mangling = -1; /* -1 don't know yet, 0 no, 1 yes */
424
0
  size_t i, j;
425
426
0
  if (!str || !buf)
427
0
    return -1;
428
429
  /* Is there anything to do at all? */
430
0
  if (!*str || !len)
431
0
    return 0;
432
433
0
  if (buf_len < DM_NAME_LEN) {
434
0
    log_error(INTERNAL_ERROR "mangle_string: supplied buffer too small");
435
0
    return -1;
436
0
  }
437
438
0
  if (mode == DM_STRING_MANGLING_NONE)
439
0
    mode = DM_STRING_MANGLING_AUTO;
440
441
0
  for (i = 0, j = 0; str[i]; i++) {
442
0
    if (mode == DM_STRING_MANGLING_AUTO) {
443
      /*
444
       * Detect already mangled part of the string and keep it.
445
       * Return error on mixture of mangled/not mangled!
446
       */
447
0
      if (str[i] == '\\' && str[i+1] == 'x') {
448
0
        if ((len - i < 4) || (need_mangling == 1))
449
0
          goto bad1;
450
0
        if (buf_len - j < 4)
451
0
          goto bad2;
452
453
0
        memcpy(&buf[j], &str[i], 4);
454
0
        i+=3; j+=4;
455
456
0
        need_mangling = 0;
457
0
        continue;
458
0
      }
459
0
    }
460
461
0
    if (_is_whitelisted_char(str[i])) {
462
      /* whitelisted, keep it. */
463
0
      if (buf_len - j < 1)
464
0
        goto bad2;
465
0
      buf[j] = str[i];
466
0
      j++;
467
0
    } else {
468
      /*
469
       * Not on a whitelist, mangle it.
470
       * Return error on mixture of mangled/not mangled
471
       * unless a DM_STRING_MANGLING_HEX is used!.
472
       */
473
0
      if ((mode != DM_STRING_MANGLING_HEX) && (need_mangling == 0))
474
0
        goto bad1;
475
0
      if (buf_len - j < 4)
476
0
        goto bad2;
477
478
0
      sprintf(&buf[j], "\\x%02x", (unsigned char) str[i]);
479
0
      j+=4;
480
481
0
      need_mangling = 1;
482
0
    }
483
0
  }
484
485
0
  if (buf_len - j < 1)
486
0
    goto bad2;
487
0
  buf[j] = '\0';
488
489
  /* All chars in the string whitelisted? */
490
0
  if (need_mangling == -1)
491
0
    need_mangling = 0;
492
493
0
  return need_mangling;
494
495
0
bad1:
496
0
  log_error("The %s \"%s\" contains mixed mangled and unmangled "
497
0
      "characters or it's already mangled improperly.", str_name, str);
498
0
  return -1;
499
0
bad2:
500
0
  log_error("Mangled form of the %s too long for \"%s\".", str_name, str);
501
0
  return -1;
502
0
}
503
504
/*
505
 * Try to unmangle supplied string.
506
 * Return value: -1 on error, 0 when no unmangling needed, 1 when unmangling applied
507
 */
508
int unmangle_string(const char *str, const char *str_name, size_t len,
509
        char *buf, size_t buf_len, dm_string_mangling_t mode)
510
0
{
511
0
  int strict = mode != DM_STRING_MANGLING_NONE;
512
0
  char str_rest[DM_NAME_LEN + 1];
513
0
  size_t i, j;
514
0
  unsigned int code;
515
0
  int r = 0;
516
517
0
  if (!str || !buf)
518
0
    return -1;
519
520
  /* Is there anything to do at all? */
521
0
  if (!*str || !len)
522
0
    return 0;
523
524
0
  if (buf_len < DM_NAME_LEN) {
525
0
    log_error(INTERNAL_ERROR "unmangle_string: supplied buffer too small");
526
0
    return -1;
527
0
  }
528
529
0
  for (i = 0, j = 0; str[i]; i++, j++) {
530
0
    if (strict && !(_is_whitelisted_char(str[i]) || str[i]=='\\')) {
531
0
      log_error("The %s \"%s\" should be mangled but "
532
0
          "it contains blacklisted characters.", str_name, str);
533
0
      j=0; r=-1;
534
0
      goto out;
535
0
    }
536
537
0
    if (str[i] == '\\' && str[i+1] == 'x') {
538
0
      if (!sscanf(&str[i+2], "%2x%" DM_TO_STRING(DM_NAME_LEN) "s",
539
0
            &code, str_rest)) {
540
0
        log_debug_activation("Hex encoding mismatch detected in %s \"%s\" "
541
0
                 "while trying to unmangle it.", str_name, str);
542
0
        goto out;
543
0
      }
544
0
      buf[j] = (unsigned char) code;
545
546
      /* skip the encoded part we've just decoded! */
547
0
      i+= 3;
548
549
      /* unmangling applied */
550
0
      r = 1;
551
0
    } else
552
0
      buf[j] = str[i];
553
0
  }
554
555
0
out:
556
0
  buf[j] = '\0';
557
0
  return r;
558
0
}
559
560
static int _dm_task_set_name(struct dm_task *dmt, const char *name,
561
           dm_string_mangling_t mangling_mode)
562
0
{
563
0
  char mangled_name[DM_NAME_LEN];
564
0
  int r = 0;
565
566
0
  dm_free(dmt->dev_name);
567
0
  dmt->dev_name = NULL;
568
0
  dm_free(dmt->mangled_dev_name);
569
0
  dmt->mangled_dev_name = NULL;
570
571
0
  if (strlen(name) >= DM_NAME_LEN) {
572
0
    log_error("Name \"%s\" too long.", name);
573
0
    return 0;
574
0
  }
575
576
0
  if (!check_multiple_mangled_string_allowed(name, "name", mangling_mode))
577
0
    return_0;
578
579
0
  if (mangling_mode != DM_STRING_MANGLING_NONE &&
580
0
      (r = mangle_string(name, "name", strlen(name), mangled_name,
581
0
             sizeof(mangled_name), mangling_mode)) < 0) {
582
0
    log_error("Failed to mangle device name \"%s\".", name);
583
0
    return 0;
584
0
  }
585
586
  /* Store mangled_dev_name only if it differs from dev_name! */
587
0
  if (r) {
588
0
    log_debug_activation("Device name mangled [%s]: %s --> %s",
589
0
             mangling_mode == DM_STRING_MANGLING_AUTO ? "auto" : "hex",
590
0
             name, mangled_name);
591
0
    if (!(dmt->mangled_dev_name = dm_strdup(mangled_name))) {
592
0
      log_error("_dm_task_set_name: dm_strdup(%s) failed", mangled_name);
593
0
      return 0;
594
0
    }
595
0
  }
596
597
0
  if (!(dmt->dev_name = dm_strdup(name))) {
598
0
    log_error("_dm_task_set_name: strdup(%s) failed", name);
599
0
    return 0;
600
0
  }
601
602
0
  return 1;
603
0
}
604
605
static int _dm_task_set_name_from_path(struct dm_task *dmt, const char *path,
606
               const char *name)
607
0
{
608
0
  char buf[PATH_MAX];
609
0
  struct stat st1, st2;
610
0
  const char *final_name = NULL;
611
0
  size_t len;
612
613
0
  if (dmt->type == DM_DEVICE_CREATE) {
614
0
    log_error("Name \"%s\" invalid. It contains \"/\".", path);
615
0
    return 0;
616
0
  }
617
618
0
  if (!stat(path, &st1)) {
619
    /*
620
     * Found directly.
621
     * If supplied path points to same device as last component
622
     * under /dev/mapper, use that name directly.  
623
     */
624
0
    if (dm_snprintf(buf, sizeof(buf), "%s/%s", _dm_dir, name) == -1) {
625
0
      log_error("Couldn't create path for %s", name);
626
0
      return 0;
627
0
    }
628
629
0
    if (!stat(buf, &st2) && (st1.st_rdev == st2.st_rdev))
630
0
      final_name = name;
631
0
  } else {
632
    /* Not found. */
633
    /* If there is exactly one '/' try a prefix of /dev */
634
0
    if ((len = strlen(path)) < 3 || path[0] == '/' ||
635
0
        dm_count_chars(path, len, '/') != 1) {
636
0
      log_error("Device %s not found", path);
637
0
      return 0;
638
0
    }
639
0
    if (dm_snprintf(buf, sizeof(buf), "%s/../%s", _dm_dir, path) == -1) {
640
0
      log_error("Couldn't create /dev path for %s", path);
641
0
      return 0;
642
0
    }
643
0
    if (stat(buf, &st1)) {
644
0
      log_error("Device %s not found", path);
645
0
      return 0;
646
0
    }
647
    /* Found */
648
0
  }
649
650
  /*
651
   * If we don't have the dm name yet, Call _find_dm_name_of_device() to
652
   * scan _dm_dir for a match.
653
   */
654
0
  if (!final_name) {
655
0
    if (_find_dm_name_of_device(st1.st_rdev, buf, sizeof(buf)))
656
0
      final_name = buf;
657
0
    else {
658
0
      log_error("Device %s not found", name);
659
0
      return 0;
660
0
    }
661
0
  }
662
663
  /* This is an already existing path - do not mangle! */
664
0
  return _dm_task_set_name(dmt, final_name, DM_STRING_MANGLING_NONE);
665
0
}
666
667
int dm_task_set_name(struct dm_task *dmt, const char *name)
668
0
{
669
0
  char *pos;
670
671
  /* Path supplied for existing device? */
672
0
  if ((pos = strrchr(name, '/')))
673
0
    return _dm_task_set_name_from_path(dmt, name, pos + 1);
674
675
0
  return _dm_task_set_name(dmt, name, dm_get_name_mangling_mode());
676
0
}
677
678
const char *dm_task_get_name(const struct dm_task *dmt)
679
0
{
680
0
  return (dmt->dmi.v4->name);
681
0
}
682
683
static char *_task_get_string_mangled(const char *str, const char *str_name,
684
              char *buf, size_t buf_size,
685
              dm_string_mangling_t mode)
686
0
{
687
0
  char *rs;
688
0
  int r;
689
690
0
  if ((r = mangle_string(str, str_name, strlen(str), buf, buf_size, mode)) < 0)
691
0
    return NULL;
692
693
0
  if (!(rs = r ? dm_strdup(buf) : dm_strdup(str)))
694
0
    log_error("_task_get_string_mangled: dm_strdup failed");
695
696
0
  return rs;
697
0
}
698
699
static char *_task_get_string_unmangled(const char *str, const char *str_name,
700
          char *buf, size_t buf_size,
701
          dm_string_mangling_t mode)
702
0
{
703
0
  char *rs;
704
0
  int r = 0;
705
706
  /*
707
   * Unless the mode used is 'none', the string
708
   * is *already* unmangled on ioctl return!
709
   */
710
0
  if (mode == DM_STRING_MANGLING_NONE &&
711
0
      (r = unmangle_string(str, str_name, strlen(str), buf, buf_size, mode)) < 0)
712
0
    return NULL;
713
714
0
  if (!(rs = r ? dm_strdup(buf) : dm_strdup(str)))
715
0
    log_error("_task_get_string_unmangled: dm_strdup failed");
716
717
0
  return rs;
718
0
}
719
720
char *dm_task_get_name_mangled(const struct dm_task *dmt)
721
0
{
722
0
  const char *s = dm_task_get_name(dmt);
723
0
  char buf[DM_NAME_LEN];
724
0
  char *rs;
725
726
0
  if (!(rs = _task_get_string_mangled(s, "name", buf, sizeof(buf), dm_get_name_mangling_mode())))
727
0
    log_error("Failed to mangle device name \"%s\".", s);
728
729
0
  return rs;
730
0
}
731
732
char *dm_task_get_name_unmangled(const struct dm_task *dmt)
733
0
{
734
0
  const char *s = dm_task_get_name(dmt);
735
0
  char buf[DM_NAME_LEN];
736
0
  char *rs;
737
738
0
  if (!(rs = _task_get_string_unmangled(s, "name", buf, sizeof(buf), dm_get_name_mangling_mode())))
739
0
    log_error("Failed to unmangle device name \"%s\".", s);
740
741
0
  return rs;
742
0
}
743
744
const char *dm_task_get_uuid(const struct dm_task *dmt)
745
0
{
746
0
  return (dmt->dmi.v4->uuid);
747
0
}
748
749
char *dm_task_get_uuid_mangled(const struct dm_task *dmt)
750
0
{
751
0
  const char *s = dm_task_get_uuid(dmt);
752
0
  char buf[DM_UUID_LEN];
753
0
  char *rs;
754
755
0
  if (!(rs = _task_get_string_mangled(s, "UUID", buf, sizeof(buf), dm_get_name_mangling_mode())))
756
0
    log_error("Failed to mangle device uuid \"%s\".", s);
757
758
0
  return rs;
759
0
}
760
761
char *dm_task_get_uuid_unmangled(const struct dm_task *dmt)
762
0
{
763
0
  const char *s = dm_task_get_uuid(dmt);
764
0
  char buf[DM_UUID_LEN];
765
0
  char *rs;
766
767
0
  if (!(rs = _task_get_string_unmangled(s, "UUID", buf, sizeof(buf), dm_get_name_mangling_mode())))
768
0
    log_error("Failed to unmangle device uuid \"%s\".", s);
769
770
0
  return rs;
771
0
}
772
773
int dm_task_set_newname(struct dm_task *dmt, const char *newname)
774
0
{
775
0
  dm_string_mangling_t mangling_mode = dm_get_name_mangling_mode();
776
0
  char mangled_name[DM_NAME_LEN];
777
0
  int r = 0;
778
779
0
  if (strchr(newname, '/')) {
780
0
    log_error("Name \"%s\" invalid. It contains \"/\".", newname);
781
0
    return 0;
782
0
  }
783
784
0
  if (strlen(newname) >= DM_NAME_LEN) {
785
0
    log_error("Name \"%s\" too long", newname);
786
0
    return 0;
787
0
  }
788
789
0
  if (!*newname) {
790
0
    log_error("Non empty new name is required.");
791
0
    return 0;
792
0
  }
793
794
0
  if (!check_multiple_mangled_string_allowed(newname, "new name", mangling_mode))
795
0
    return_0;
796
797
0
  if (mangling_mode != DM_STRING_MANGLING_NONE &&
798
0
      (r = mangle_string(newname, "new name", strlen(newname), mangled_name,
799
0
             sizeof(mangled_name), mangling_mode)) < 0) {
800
0
    log_error("Failed to mangle new device name \"%s\"", newname);
801
0
    return 0;
802
0
  }
803
804
0
  if (r) {
805
0
    log_debug_activation("New device name mangled [%s]: %s --> %s",
806
0
             mangling_mode == DM_STRING_MANGLING_AUTO ? "auto" : "hex",
807
0
             newname, mangled_name);
808
0
    newname = mangled_name;
809
0
  }
810
811
0
  dm_free(dmt->newname);
812
0
  if (!(dmt->newname = dm_strdup(newname))) {
813
0
    log_error("dm_task_set_newname: strdup(%s) failed", newname);
814
0
    return 0;
815
0
  }
816
817
0
  dmt->new_uuid = 0;
818
819
0
  return 1;
820
0
}
821
822
int dm_task_set_uuid(struct dm_task *dmt, const char *uuid)
823
0
{
824
0
  char mangled_uuid[DM_UUID_LEN];
825
0
  dm_string_mangling_t mangling_mode = dm_get_name_mangling_mode();
826
0
  int r = 0;
827
828
0
  dm_free(dmt->uuid);
829
0
  dmt->uuid = NULL;
830
0
  dm_free(dmt->mangled_uuid);
831
0
  dmt->mangled_uuid = NULL;
832
833
0
  if (!check_multiple_mangled_string_allowed(uuid, "UUID", mangling_mode))
834
0
    return_0;
835
836
0
  if (mangling_mode != DM_STRING_MANGLING_NONE &&
837
0
      (r = mangle_string(uuid, "UUID", strlen(uuid), mangled_uuid,
838
0
             sizeof(mangled_uuid), mangling_mode)) < 0) {
839
0
    log_error("Failed to mangle device uuid \"%s\".", uuid);
840
0
    return 0;
841
0
  }
842
843
0
  if (r) {
844
0
    log_debug_activation("Device uuid mangled [%s]: %s --> %s",
845
0
             mangling_mode == DM_STRING_MANGLING_AUTO ? "auto" : "hex",
846
0
             uuid, mangled_uuid);
847
848
0
    if (!(dmt->mangled_uuid = dm_strdup(mangled_uuid))) {
849
0
      log_error("dm_task_set_uuid: dm_strdup(%s) failed", mangled_uuid);
850
0
      return 0;
851
0
    }
852
0
  }
853
854
0
  if (!(dmt->uuid = dm_strdup(uuid))) {
855
0
    log_error("dm_task_set_uuid: strdup(%s) failed", uuid);
856
0
    return 0;
857
0
  }
858
859
0
  return 1;
860
0
}
861
862
int dm_task_set_major(struct dm_task *dmt, int major)
863
0
{
864
0
  dmt->major = major;
865
0
  dmt->allow_default_major_fallback = 0;
866
867
0
  return 1;
868
0
}
869
870
int dm_task_set_minor(struct dm_task *dmt, int minor)
871
0
{
872
0
  dmt->minor = minor;
873
874
0
  return 1;
875
0
}
876
877
int dm_task_set_major_minor(struct dm_task *dmt, int major, int minor,
878
          int allow_default_major_fallback)
879
0
{
880
0
  dmt->major = major;
881
0
  dmt->minor = minor;
882
0
  dmt->allow_default_major_fallback = allow_default_major_fallback;
883
884
0
  return 1;
885
0
}
886
887
int dm_task_set_uid(struct dm_task *dmt, uid_t uid)
888
0
{
889
0
  dmt->uid = uid;
890
891
0
  return 1;
892
0
}
893
894
int dm_task_set_gid(struct dm_task *dmt, gid_t gid)
895
0
{
896
0
  dmt->gid = gid;
897
898
0
  return 1;
899
0
}
900
901
int dm_task_set_mode(struct dm_task *dmt, mode_t mode)
902
0
{
903
0
  dmt->mode = mode;
904
905
0
  return 1;
906
0
}
907
908
int dm_task_enable_checks(struct dm_task *dmt)
909
0
{
910
0
  dmt->enable_checks = 1;
911
912
0
  return 1;
913
0
}
914
915
int dm_task_add_target(struct dm_task *dmt, uint64_t start, uint64_t size,
916
           const char *ttype, const char *params)
917
0
{
918
0
  struct target *t = create_target(start, size, ttype, params);
919
0
  if (!t)
920
0
    return_0;
921
922
0
  if (!dmt->head)
923
0
    dmt->head = dmt->tail = t;
924
0
  else {
925
0
    dmt->tail->next = t;
926
0
    dmt->tail = t;
927
0
  }
928
929
0
  return 1;
930
0
}
931
932
#ifdef HAVE_SELINUX
933
static int _selabel_lookup(const char *path, mode_t mode,
934
         char **scontext)
935
{
936
#ifdef HAVE_SELINUX_LABEL_H
937
  if (!_selabel_handle &&
938
      !(_selabel_handle = selabel_open(SELABEL_CTX_FILE, NULL, 0))) {
939
    log_error("selabel_open failed: %s", strerror(errno));
940
    return 0;
941
  }
942
943
  if (selabel_lookup(_selabel_handle, scontext, path, mode)) {
944
    log_debug_activation("selabel_lookup failed for %s: %s",
945
             path, strerror(errno));
946
    return 0;
947
  }
948
#else
949
  if (matchpathcon(path, mode, scontext)) {
950
    log_debug_activation("matchpathcon failed for %s: %s",
951
             path, strerror(errno));
952
    return 0;
953
  }
954
#endif
955
  return 1;
956
}
957
#endif
958
959
#ifdef HAVE_SELINUX
960
static int _is_selinux_enabled(void)
961
{
962
  static int _tested = 0;
963
  static int _enabled;
964
965
  if (!_tested) {
966
    _tested = 1;
967
    _enabled = is_selinux_enabled();
968
  }
969
970
  return _enabled;
971
}
972
#endif
973
974
int dm_prepare_selinux_context(const char *path, mode_t mode)
975
0
{
976
#ifdef HAVE_SELINUX
977
  char *scontext = NULL;
978
979
  if (_is_selinux_enabled() <= 0)
980
    return 1;
981
982
  if (path) {
983
    if (!_selabel_lookup(path, mode, &scontext))
984
      return_0;
985
986
    log_debug_activation("Preparing SELinux context for %s to %s.", path, scontext);
987
  }
988
  else
989
    log_debug_activation("Resetting SELinux context to default value.");
990
991
  if (setfscreatecon(scontext) < 0) {
992
    log_sys_error("setfscreatecon", (path ? : "SELinux context reset"));
993
    freecon(scontext);
994
    return 0;
995
  }
996
997
  freecon(scontext);
998
#endif
999
0
  return 1;
1000
0
}
1001
1002
int dm_set_selinux_context(const char *path, mode_t mode)
1003
0
{
1004
#ifdef HAVE_SELINUX
1005
  char *scontext = NULL;
1006
1007
  if (_is_selinux_enabled() <= 0)
1008
    return 1;
1009
1010
  if (!_selabel_lookup(path, mode, &scontext))
1011
    return_0;
1012
1013
  log_debug_activation("Setting SELinux context for %s to %s.", path, scontext);
1014
1015
  if ((lsetfilecon(path, scontext) < 0) && (errno != ENOTSUP)) {
1016
    log_sys_error("lsetfilecon", path);
1017
    freecon(scontext);
1018
    return 0;
1019
  }
1020
1021
  freecon(scontext);
1022
#endif
1023
0
  return 1;
1024
0
}
1025
1026
void selinux_release(void)
1027
0
{
1028
#ifdef HAVE_SELINUX_LABEL_H
1029
  if (_selabel_handle)
1030
    selabel_close(_selabel_handle);
1031
  _selabel_handle = NULL;
1032
#endif
1033
0
}
1034
1035
static int _warn_if_op_needed(int warn_if_udev_failed)
1036
0
{
1037
0
    return warn_if_udev_failed && dm_udev_get_sync_support() && dm_udev_get_checking();
1038
0
}
1039
1040
static int _add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
1041
       uid_t uid, gid_t gid, mode_t mode, int warn_if_udev_failed)
1042
0
{
1043
0
  char path[PATH_MAX];
1044
0
  struct stat info;
1045
0
  struct stat linfo;
1046
0
  dev_t dev = MKDEV(major, minor);
1047
0
  mode_t old_mask;
1048
1049
0
  if (!_build_dev_path(path, sizeof(path), dev_name))
1050
0
    return_0;
1051
1052
  /*
1053
   * Check if the device node already exists.
1054
   * Note: stat() follows symlinks, so this checks the target device,
1055
   * not the symlink itself. This works correctly for both real nodes
1056
   * and symlinks pointing to the right device.
1057
   */
1058
0
  if (stat(path, &info) >= 0) {
1059
0
    if (!S_ISBLK(info.st_mode)) {
1060
0
      log_error("A non-block device file at '%s' "
1061
0
          "is already present", path);
1062
0
      return 0;
1063
0
    }
1064
1065
    /* If right inode already exists we don't touch uid etc. */
1066
0
    if (info.st_rdev == dev) {
1067
      /*
1068
       * Correct device exists (either as real node or symlink).
1069
       * Use lstat() to distinguish between them for logging.
1070
       */
1071
0
      if (lstat(path, &linfo) >= 0 && S_ISLNK(linfo.st_mode))
1072
0
        log_debug_activation("Symlink %s to correct device already exists", path);
1073
0
      return 1;
1074
0
    }
1075
1076
0
    if (unlink(path) && (errno != ENOENT)) {
1077
0
      log_sys_error("unlink", path);
1078
0
      return 0;
1079
0
    }
1080
0
  } else {
1081
    /*
1082
     * stat() failed. Check for dangling symlinks (where lstat succeeds
1083
     * but stat fails with ENOENT because the symlink target doesn't exist).
1084
     * Remove the dangling symlink before attempting to create a new node.
1085
     */
1086
0
    if (errno == ENOENT && lstat(path, &linfo) >= 0 && S_ISLNK(linfo.st_mode)) {
1087
0
      log_debug_activation("Removing dangling symlink %s", path);
1088
0
      if (unlink(path) && (errno != ENOENT)) {
1089
0
        log_sys_error("unlink", path);
1090
0
        return 0;
1091
0
      }
1092
0
    }
1093
0
    if (_warn_if_op_needed(warn_if_udev_failed))
1094
0
      log_warn("%s not set up by udev: Falling back to direct "
1095
0
         "node creation.", path);
1096
0
  }
1097
1098
  /*
1099
   * Test environment optimization: If using alternative dev dir (e.g., /tmp/LVMTEST/dev)
1100
   * and the real /dev node already exists, create a symlink instead of a duplicate node.
1101
   * This ensures operations trigger udev events which only monitors /dev.
1102
   */
1103
0
  if (strcmp(_dm_dir, DEV_DIR) != 0) {
1104
0
    char real_path[PATH_MAX];
1105
0
    struct stat real_stat;
1106
1107
    /* Build path to real /dev node (kernel always creates /dev/dm-N) */
1108
0
    if (dm_snprintf(real_path, sizeof(real_path), DEV_DIR "dm-%u", minor) >= 0) {
1109
      /* Check if real node exists with matching dev */
1110
0
      if (stat(real_path, &real_stat) >= 0 &&
1111
0
          S_ISBLK(real_stat.st_mode) &&
1112
0
          real_stat.st_rdev == dev) {
1113
        /*
1114
         * Real /dev/dm-N exists. Create symlink from alternative location.
1115
         * This allows operations to work through the symlink and trigger
1116
         * udev events on the real device.
1117
         */
1118
0
        log_debug_activation("Creating symlink %s -> %s", path, real_path);
1119
0
        (void) dm_prepare_selinux_context(path, S_IFLNK);
1120
0
        if (symlink(real_path, path) < 0) {
1121
0
          log_sys_error("symlink", path);
1122
0
          (void) dm_prepare_selinux_context(NULL, 0);
1123
0
          return 0;
1124
0
        }
1125
0
        (void) dm_prepare_selinux_context(NULL, 0);
1126
0
        log_debug_activation("Created symlink %s -> %s", path, real_path);
1127
0
        return 1;
1128
0
      }
1129
0
    }
1130
0
  }
1131
1132
0
  (void) dm_prepare_selinux_context(path, S_IFBLK);
1133
0
  old_mask = umask(0);
1134
1135
  /* The node may already have been created by udev. So ignore EEXIST. */
1136
0
  if (mknod(path, S_IFBLK | mode, dev) < 0 && errno != EEXIST) {
1137
0
    log_error("%s: mknod for %s failed: %s", path, dev_name, strerror(errno));
1138
0
    umask(old_mask);
1139
0
    (void) dm_prepare_selinux_context(NULL, 0);
1140
0
    return 0;
1141
0
  }
1142
0
  umask(old_mask);
1143
0
  (void) dm_prepare_selinux_context(NULL, 0);
1144
1145
0
  if (chown(path, uid, gid) < 0) {
1146
0
    log_sys_error("chown", path);
1147
0
    return 0;
1148
0
  }
1149
1150
0
  log_debug_activation("Created %s", path);
1151
1152
0
  return 1;
1153
0
}
1154
1155
static int _rm_dev_node(const char *dev_name, int warn_if_udev_failed)
1156
0
{
1157
0
  char path[PATH_MAX];
1158
0
  struct stat info;
1159
1160
0
  if (!_build_dev_path(path, sizeof(path), dev_name))
1161
0
    return_0;
1162
0
  if (lstat(path, &info) < 0)
1163
0
    return 1;
1164
0
  else if (_warn_if_op_needed(warn_if_udev_failed))
1165
0
    log_warn("Node %s was not removed by udev. "
1166
0
       "Falling back to direct node removal.", path);
1167
1168
  /* udev may already have deleted the node. Ignore ENOENT. */
1169
0
  if (unlink(path) && (errno != ENOENT)) {
1170
0
    log_sys_error("unlink", path);
1171
0
    return 0;
1172
0
  }
1173
1174
0
  log_debug_activation("Removed %s", path);
1175
1176
0
  return 1;
1177
0
}
1178
1179
static int _rename_dev_node(const char *old_name, const char *new_name,
1180
          int warn_if_udev_failed)
1181
0
{
1182
0
  char oldpath[PATH_MAX];
1183
0
  char newpath[PATH_MAX];
1184
0
  struct stat info, info2;
1185
0
  struct stat *info_block_dev;
1186
1187
0
  if (!_build_dev_path(oldpath, sizeof(oldpath), old_name) ||
1188
0
      !_build_dev_path(newpath, sizeof(newpath), new_name))
1189
0
    return_0;
1190
1191
0
  if (lstat(newpath, &info) == 0) {
1192
0
    if (S_ISLNK(info.st_mode)) {
1193
0
      if (stat(newpath, &info2) == 0)
1194
0
        info_block_dev = &info2;
1195
0
      else {
1196
0
        log_sys_error("stat", newpath);
1197
0
        return 0;
1198
0
      }
1199
0
    } else
1200
0
      info_block_dev = &info;
1201
1202
0
    if (!S_ISBLK(info_block_dev->st_mode)) {
1203
0
      log_error("A non-block device file at '%s' "
1204
0
          "is already present", newpath);
1205
0
      return 0;
1206
0
    }
1207
0
    else if (_warn_if_op_needed(warn_if_udev_failed)) {
1208
0
      if (lstat(oldpath, &info) < 0 &&
1209
0
         errno == ENOENT)
1210
        /* assume udev already deleted this */
1211
0
        return 1;
1212
1213
0
      log_warn("The node %s should have been renamed to %s "
1214
0
         "by udev but old node is still present. "
1215
0
         "Falling back to direct old node removal.",
1216
0
         oldpath, newpath);
1217
0
      return _rm_dev_node(old_name, 0);
1218
0
    }
1219
1220
0
    if (unlink(newpath) < 0) {
1221
0
      if (errno == EPERM) {
1222
        /* devfs, entry has already been renamed */
1223
0
        return 1;
1224
0
      }
1225
0
      log_error("Unable to unlink device node for '%s'",
1226
0
          new_name);
1227
0
      return 0;
1228
0
    }
1229
0
  }
1230
0
  else if (_warn_if_op_needed(warn_if_udev_failed))
1231
0
    log_warn("The node %s should have been renamed to %s "
1232
0
       "by udev but new node is not present. "
1233
0
       "Falling back to direct node rename.",
1234
0
       oldpath, newpath);
1235
1236
  /* udev may already have renamed the node. Ignore ENOENT. */
1237
  /* FIXME: when renaming to target mangling mode "none" with udev
1238
   * while there are some blacklisted characters in the node name,
1239
   * udev will remove the old_node, but fails to properly rename
1240
   * to new_node. The libdevmapper code tries to call
1241
   * rename(old_node,new_node), but that won't do anything
1242
   * since the old node is already removed by udev.
1243
   * For example renaming 'a\x20b' to 'a b':
1244
   *   - udev removes 'a\x20b'
1245
   *   - udev creates 'a' and 'b' (since it considers the ' ' as a delimiter
1246
   *   - libdevmapper checks udev has done the rename properly
1247
   *   - libdevmapper calls stat(new_node) and it does not see it
1248
   *   - libdevmapper calls rename(old_node,new_node)
1249
   *   - the rename is a NOP since the old_node does not exist anymore
1250
   *
1251
   * However, this situation is very rare - why would anyone need
1252
   * to rename to an unsupported mode??? So a fix for this would be
1253
   * just for completeness.
1254
   */
1255
0
  if (rename(oldpath, newpath) < 0 && errno != ENOENT) {
1256
0
    log_error("Unable to rename device node from '%s' to '%s'",
1257
0
        old_name, new_name);
1258
0
    return 0;
1259
0
  }
1260
1261
0
  log_debug_activation("Renamed %s to %s", oldpath, newpath);
1262
1263
0
  return 1;
1264
0
}
1265
1266
#ifdef __linux__
1267
static int _open_dev_node(const char *dev_name)
1268
0
{
1269
0
  int fd = -1;
1270
0
  char path[PATH_MAX];
1271
1272
0
  if (!_build_dev_path(path, sizeof(path), dev_name))
1273
0
    return fd;
1274
1275
0
  if ((fd = open(path, O_RDONLY, 0)) < 0)
1276
0
    log_sys_error("open", path);
1277
1278
0
  return fd;
1279
0
}
1280
1281
int get_dev_node_read_ahead(const char *dev_name, uint32_t major, uint32_t minor,
1282
          uint32_t *read_ahead)
1283
0
{
1284
0
  char buf[24];
1285
0
  int len;
1286
0
  int r = 1;
1287
0
  int fd;
1288
0
  long read_ahead_long = 0;
1289
1290
  /*
1291
   * If we know the device number, use sysfs if we can.
1292
   * Otherwise use BLKRAGET ioctl.
1293
   */
1294
0
  if (*_sysfs_dir && major != 0) {
1295
0
    if (dm_snprintf(_path0, sizeof(_path0), "%sdev/block/%" PRIu32
1296
0
        ":%" PRIu32 "/bdi/read_ahead_kb", _sysfs_dir,
1297
0
        major, minor) < 0) {
1298
0
      log_error("Failed to build sysfs_path.");
1299
0
      return 0;
1300
0
    }
1301
1302
0
    if ((fd = open(_path0, O_RDONLY, 0)) != -1) {
1303
      /* Reading from sysfs, expecting number\n */
1304
0
      if ((len = read(fd, buf, sizeof(buf) - 1)) < 1) {
1305
0
        log_sys_error("read", _path0);
1306
0
        r = 0;
1307
0
      } else {
1308
0
        buf[len] = 0; /* kill \n and ensure \0 */
1309
0
        *read_ahead = atoi(buf) * 2;
1310
0
        log_debug_activation("%s (%d:%d): read ahead is %" PRIu32,
1311
0
                 dev_name, major, minor, *read_ahead);
1312
0
      }
1313
1314
0
      if (close(fd))
1315
0
        log_sys_debug("close", _path0);
1316
1317
0
      return r;
1318
0
    }
1319
1320
0
    log_sys_debug("open", _path0);
1321
    /* Fall back to use dev_name */
1322
0
  }
1323
1324
  /*
1325
   * Open/close dev_name may block the process
1326
   * (i.e. overfilled thin pool volume)
1327
   */
1328
0
  if (!*dev_name) {
1329
0
    log_error("Empty device name passed to BLKRAGET");
1330
0
    return 0;
1331
0
  }
1332
1333
0
  if ((fd = _open_dev_node(dev_name)) < 0)
1334
0
    return_0;
1335
1336
0
  if (ioctl(fd, BLKRAGET, &read_ahead_long)) {
1337
0
    log_sys_error("BLKRAGET", dev_name);
1338
0
    *read_ahead = 0;
1339
0
    r = 0;
1340
0
  } else {
1341
0
    *read_ahead = (uint32_t) read_ahead_long;
1342
0
    log_debug_activation("%s: read ahead is %" PRIu32, dev_name, *read_ahead);
1343
0
  }
1344
1345
0
  if (close(fd))
1346
0
    log_sys_debug("close", dev_name);
1347
1348
0
  return r;
1349
0
}
1350
1351
static int _set_read_ahead(const char *dev_name, uint32_t major, uint32_t minor,
1352
         uint32_t read_ahead)
1353
0
{
1354
0
  char buf[24];
1355
0
  int len;
1356
0
  int r = 1;
1357
0
  int fd;
1358
0
  long read_ahead_long = (long) read_ahead;
1359
1360
0
  log_debug_activation("%s (%d:%d): Setting read ahead to %" PRIu32, dev_name,
1361
0
           major, minor, read_ahead);
1362
1363
  /*
1364
   * If we know the device number, use sysfs if we can.
1365
   * Otherwise use BLKRASET ioctl. RA is set after resume.
1366
   */
1367
0
  if (*_sysfs_dir && major != 0) {
1368
0
    if (dm_snprintf(_path0, sizeof(_path0), "%sdev/block/%" PRIu32
1369
0
        ":%" PRIu32 "/bdi/read_ahead_kb",
1370
0
        _sysfs_dir, major, minor) < 0) {
1371
0
      log_error("Failed to build sysfs_path.");
1372
0
      return 0;
1373
0
    }
1374
1375
    /* Sysfs is kB based, round up to kB */
1376
0
    if ((len = dm_snprintf(buf, sizeof(buf), FMTu32,
1377
0
               (read_ahead + 1) / 2)) < 0) {
1378
0
      log_error("Failed to build size in kB.");
1379
0
      return 0;
1380
0
    }
1381
1382
0
    if ((fd = open(_path0, O_WRONLY, 0)) != -1) {
1383
0
      if (write(fd, buf, len) < len) {
1384
0
        log_sys_error("write", _path0);
1385
0
        r = 0;
1386
0
      }
1387
1388
0
      if (close(fd))
1389
0
        log_sys_debug("close", _path0);
1390
1391
0
      return r;
1392
0
    }
1393
1394
0
    log_sys_debug("open", _path0);
1395
    /* Fall back to use dev_name */
1396
0
  }
1397
1398
0
  if (!*dev_name) {
1399
0
    log_error("Empty device name passed to BLKRAGET");
1400
0
    return 0;
1401
0
  }
1402
1403
0
  if ((fd = _open_dev_node(dev_name)) < 0)
1404
0
    return_0;
1405
1406
0
  if (ioctl(fd, BLKRASET, read_ahead_long)) {
1407
0
    log_sys_error("BLKRASET", dev_name);
1408
0
    r = 0;
1409
0
  }
1410
1411
0
  if (close(fd))
1412
0
    log_sys_debug("close", dev_name);
1413
1414
0
  return r;
1415
0
}
1416
1417
static int _set_dev_node_read_ahead(const char *dev_name,
1418
            uint32_t major, uint32_t minor,
1419
            uint32_t read_ahead, uint32_t read_ahead_flags)
1420
0
{
1421
0
  uint32_t current_read_ahead;
1422
1423
0
  if (read_ahead == DM_READ_AHEAD_AUTO)
1424
0
    return 1;
1425
1426
0
  if (read_ahead == DM_READ_AHEAD_NONE)
1427
0
    read_ahead = 0;
1428
1429
0
  if (read_ahead_flags & DM_READ_AHEAD_MINIMUM_FLAG) {
1430
0
    if (!get_dev_node_read_ahead(dev_name, major, minor, &current_read_ahead))
1431
0
      return_0;
1432
1433
0
    if (current_read_ahead >= read_ahead) {
1434
0
      log_debug_activation("%s: retaining kernel read ahead of %" PRIu32
1435
0
          " (requested %" PRIu32 ")",           
1436
0
          dev_name, current_read_ahead, read_ahead);
1437
0
      return 1;
1438
0
    }
1439
0
  }
1440
1441
0
  return _set_read_ahead(dev_name, major, minor, read_ahead);
1442
0
}
1443
1444
#else
1445
1446
int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
1447
{
1448
  *read_ahead = 0;
1449
1450
  return 1;
1451
}
1452
1453
static int _set_dev_node_read_ahead(const char *dev_name,
1454
            uint32_t major, uint32_t minor,
1455
            uint32_t read_ahead, uint32_t read_ahead_flags)
1456
{
1457
  return 1;
1458
}
1459
#endif
1460
1461
typedef enum {
1462
  NODE_ADD,
1463
  NODE_DEL,
1464
  NODE_RENAME,
1465
  NODE_READ_AHEAD,
1466
  NUM_NODES
1467
} node_op_t;
1468
1469
static int _do_node_op(node_op_t type, const char *dev_name, uint32_t major,
1470
           uint32_t minor, uid_t uid, gid_t gid, mode_t mode,
1471
           const char *old_name, uint32_t read_ahead,
1472
           uint32_t read_ahead_flags, int warn_if_udev_failed)
1473
0
{
1474
0
  switch (type) {
1475
0
  case NODE_ADD:
1476
0
    return _add_dev_node(dev_name, major, minor, uid, gid,
1477
0
             mode, warn_if_udev_failed);
1478
0
  case NODE_DEL:
1479
0
    return _rm_dev_node(dev_name, warn_if_udev_failed);
1480
0
  case NODE_RENAME:
1481
0
    return _rename_dev_node(old_name, dev_name, warn_if_udev_failed);
1482
0
  case NODE_READ_AHEAD:
1483
0
    return _set_dev_node_read_ahead(dev_name, major, minor,
1484
0
            read_ahead, read_ahead_flags);
1485
0
  default:
1486
0
    ; /* NOTREACHED */
1487
0
  }
1488
1489
0
  return 1;
1490
0
}
1491
1492
static DM_LIST_INIT(_node_ops);
1493
static int _count_node_ops[NUM_NODES];
1494
1495
struct node_op_parms {
1496
  struct dm_list list;
1497
  node_op_t type;
1498
  char *dev_name;
1499
  uint32_t major;
1500
  uint32_t minor;
1501
  uid_t uid;
1502
  gid_t gid;
1503
  mode_t mode;
1504
  uint32_t read_ahead;
1505
  uint32_t read_ahead_flags;
1506
  char *old_name;
1507
  int warn_if_udev_failed;
1508
  unsigned rely_on_udev;
1509
  char names[0];
1510
};
1511
1512
static void _store_str(char **pos, char **ptr, const char *str)
1513
0
{
1514
0
  size_t len = strlen(str) + 1;
1515
0
  memcpy(*pos, str, len);
1516
0
  *ptr = *pos;
1517
0
  *pos += len;
1518
0
}
1519
1520
static void _del_node_op(struct node_op_parms *nop)
1521
0
{
1522
0
  _count_node_ops[nop->type]--;
1523
0
  dm_list_del(&nop->list);
1524
0
  dm_free(nop);
1525
1526
0
}
1527
1528
/* Check if there is other the type of node operation stacked */
1529
static int _other_node_ops(node_op_t type)
1530
0
{
1531
0
  unsigned i;
1532
1533
0
  for (i = 0; i < NUM_NODES; i++)
1534
0
    if (type != i && _count_node_ops[i])
1535
0
      return 1;
1536
0
  return 0;
1537
0
}
1538
1539
static void _log_node_op(const char *action_str, struct node_op_parms *nop)
1540
0
{
1541
0
  const char *rely = nop->rely_on_udev ? " [trust_udev]" : "" ;
1542
0
  const char *verify = nop->warn_if_udev_failed ? " [verify_udev]" : "";
1543
1544
0
  switch (nop->type) {
1545
0
  case NODE_ADD:
1546
0
    log_debug_activation("%s: %s NODE_ADD (%" PRIu32 ",%" PRIu32 ") %u:%u 0%o%s%s",
1547
0
             nop->dev_name, action_str, nop->major, nop->minor, nop->uid, nop->gid, nop->mode,
1548
0
             rely, verify);
1549
0
    break;
1550
0
  case NODE_DEL:
1551
0
    log_debug_activation("%s: %s NODE_DEL%s%s", nop->dev_name, action_str, rely, verify);
1552
0
    break;
1553
0
  case NODE_RENAME:
1554
0
    log_debug_activation("%s: %s NODE_RENAME to %s%s%s", nop->old_name, action_str, nop->dev_name, rely, verify);
1555
0
    break;
1556
0
  case NODE_READ_AHEAD:
1557
0
    log_debug_activation("%s: %s NODE_READ_AHEAD %" PRIu32 " (flags=%" PRIu32 ")%s%s",
1558
0
             nop->dev_name, action_str, nop->read_ahead, nop->read_ahead_flags, rely, verify);
1559
0
    break;
1560
0
  default:
1561
0
    ; /* NOTREACHED */
1562
0
  }
1563
0
}
1564
1565
static int _stack_node_op(node_op_t type, const char *dev_name, uint32_t major,
1566
        uint32_t minor, uid_t uid, gid_t gid, mode_t mode,
1567
        const char *old_name, uint32_t read_ahead,
1568
        uint32_t read_ahead_flags, int warn_if_udev_failed,
1569
        unsigned rely_on_udev)
1570
0
{
1571
0
  struct node_op_parms *nop;
1572
0
  struct dm_list *noph, *nopht;
1573
0
  size_t len = strlen(dev_name) + strlen(old_name) + 2;
1574
0
  char *pos;
1575
1576
  /*
1577
   * Note: warn_if_udev_failed must have valid content
1578
   */
1579
0
  if ((type == NODE_DEL) && _other_node_ops(type))
1580
    /*
1581
     * Ignore any outstanding operations on the node if deleting it.
1582
     */
1583
0
    dm_list_iterate_safe(noph, nopht, &_node_ops) {
1584
0
      nop = dm_list_item(noph, struct node_op_parms);
1585
0
      if (!strcmp(dev_name, nop->dev_name)) {
1586
0
        _log_node_op("Unstacking", nop);
1587
0
        _del_node_op(nop);
1588
0
        if (!_other_node_ops(type))
1589
0
          break; /* no other non DEL ops */
1590
0
      }
1591
0
    }
1592
0
  else if ((type == NODE_ADD) && _count_node_ops[NODE_DEL])
1593
    /*
1594
     * Ignore previous DEL operation on added node.
1595
     * (No other operations for this device then DEL could be stacked here).
1596
     */
1597
0
    dm_list_iterate_safe(noph, nopht, &_node_ops) {
1598
0
      nop = dm_list_item(noph, struct node_op_parms);
1599
0
      if ((nop->type == NODE_DEL) &&
1600
0
          !strcmp(dev_name, nop->dev_name)) {
1601
0
        _log_node_op("Unstacking", nop);
1602
0
        _del_node_op(nop);
1603
0
        break; /* no other DEL ops */
1604
0
      }
1605
0
    }
1606
0
  else if (type == NODE_RENAME)
1607
    /*
1608
     * Ignore any outstanding operations if renaming it.
1609
     *
1610
     * Currently  RENAME operation happens through 'suspend -> resume'.
1611
     * On 'resume' device is added with read_ahead settings, so it is
1612
     * safe to remove any stacked ADD, RENAME, READ_AHEAD operation
1613
     * There cannot be any DEL operation on the renamed device.
1614
     */
1615
0
    dm_list_iterate_safe(noph, nopht, &_node_ops) {
1616
0
      nop = dm_list_item(noph, struct node_op_parms);
1617
0
      if (!strcmp(old_name, nop->dev_name)) {
1618
0
        _log_node_op("Unstacking", nop);
1619
0
        _del_node_op(nop);
1620
0
      }
1621
0
    }
1622
0
  else if (type == NODE_READ_AHEAD) {
1623
    /* udev doesn't process readahead */
1624
0
    rely_on_udev = 0;
1625
0
    warn_if_udev_failed = 0;
1626
0
  }
1627
1628
0
  if (!(nop = dm_malloc(sizeof(*nop) + len))) {
1629
0
    log_error("Insufficient memory to stack mknod operation");
1630
0
    return 0;
1631
0
  }
1632
1633
0
  pos = nop->names;
1634
0
  nop->type = type;
1635
0
  nop->major = major;
1636
0
  nop->minor = minor;
1637
0
  nop->uid = uid;
1638
0
  nop->gid = gid;
1639
0
  nop->mode = mode;
1640
0
  nop->read_ahead = read_ahead;
1641
0
  nop->read_ahead_flags = read_ahead_flags;
1642
0
  nop->rely_on_udev = rely_on_udev;
1643
1644
  /*
1645
   * Clear warn_if_udev_failed if rely_on_udev is set.  It doesn't get
1646
   * checked in this case - this just removes the flag from log messages.
1647
   */
1648
0
  nop->warn_if_udev_failed = rely_on_udev ? 0 : warn_if_udev_failed;
1649
1650
0
  _store_str(&pos, &nop->dev_name, dev_name);
1651
0
  _store_str(&pos, &nop->old_name, old_name);
1652
1653
0
  _count_node_ops[type]++;
1654
0
  dm_list_add(&_node_ops, &nop->list);
1655
1656
0
  _log_node_op("Stacking", nop);
1657
1658
0
  return 1;
1659
0
}
1660
1661
static void _pop_node_ops(void)
1662
1.46k
{
1663
1.46k
  struct dm_list *noph, *nopht;
1664
1.46k
  struct node_op_parms *nop;
1665
1666
1.46k
  dm_list_iterate_safe(noph, nopht, &_node_ops) {
1667
0
    nop = dm_list_item(noph, struct node_op_parms);
1668
0
    if (!nop->rely_on_udev) {
1669
0
      _log_node_op("Processing", nop);
1670
0
      _do_node_op(nop->type, nop->dev_name, nop->major, nop->minor,
1671
0
            nop->uid, nop->gid, nop->mode, nop->old_name,
1672
0
            nop->read_ahead, nop->read_ahead_flags,
1673
0
            nop->warn_if_udev_failed);
1674
0
    } else
1675
0
      _log_node_op("Skipping", nop);
1676
0
    _del_node_op(nop);
1677
0
  }
1678
1.46k
}
1679
1680
int add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
1681
     uid_t uid, gid_t gid, mode_t mode, int check_udev, unsigned rely_on_udev)
1682
0
{
1683
0
  return _stack_node_op(NODE_ADD, dev_name, major, minor, uid,
1684
0
            gid, mode, "", 0, 0, check_udev, rely_on_udev);
1685
0
}
1686
1687
int rename_dev_node(const char *old_name, const char *new_name, int check_udev, unsigned rely_on_udev)
1688
0
{
1689
0
  return _stack_node_op(NODE_RENAME, new_name, 0, 0, 0,
1690
0
            0, 0, old_name, 0, 0, check_udev, rely_on_udev);
1691
0
}
1692
1693
int rm_dev_node(const char *dev_name, int check_udev, unsigned rely_on_udev)
1694
0
{
1695
0
  return _stack_node_op(NODE_DEL, dev_name, 0, 0, 0,
1696
0
            0, 0, "", 0, 0, check_udev, rely_on_udev);
1697
0
}
1698
1699
int set_dev_node_read_ahead(const char *dev_name,
1700
                            uint32_t major, uint32_t minor,
1701
          uint32_t read_ahead, uint32_t read_ahead_flags)
1702
0
{
1703
0
  if (read_ahead == DM_READ_AHEAD_AUTO)
1704
0
    return 1;
1705
1706
0
  return _stack_node_op(NODE_READ_AHEAD, dev_name, major, minor, 0, 0,
1707
0
                              0, "", read_ahead, read_ahead_flags, 0, 0);
1708
0
}
1709
1710
void update_devs(void)
1711
1.46k
{
1712
1.46k
  _pop_node_ops();
1713
1.46k
}
1714
1715
static int _canonicalize_and_set_dir(const char *src, const char *suffix, size_t max_len, char *dir)
1716
0
{
1717
0
  size_t len;
1718
0
  const char *slash;
1719
1720
0
  if (*src != '/') {
1721
0
    log_debug_activation("Invalid directory value, %s: "
1722
0
             "not an absolute name.", src);
1723
0
    return 0;
1724
0
  }
1725
1726
0
  len = strlen(src);
1727
0
  slash = src[len-1] == '/' ? "" : "/";
1728
1729
0
  if (dm_snprintf(dir, max_len, "%s%s%s", src, slash, suffix ? suffix : "") < 0) {
1730
0
    log_debug_activation("Invalid directory value, %s: name too long.", src);
1731
0
    return 0;
1732
0
  }
1733
1734
0
  return 1;
1735
0
}
1736
1737
int dm_set_dev_dir(const char *dev_dir)
1738
0
{
1739
0
  return _canonicalize_and_set_dir(dev_dir, DM_DIR, sizeof _dm_dir, _dm_dir);
1740
0
}
1741
1742
const char *dm_dir(void)
1743
0
{
1744
0
  return _dm_dir;
1745
0
}
1746
1747
int dm_set_sysfs_dir(const char *sysfs_dir)
1748
0
{
1749
0
  if (!sysfs_dir || !*sysfs_dir) {
1750
0
    _sysfs_dir[0] = '\0';
1751
0
    return 1;
1752
0
  }
1753
1754
0
  return _canonicalize_and_set_dir(sysfs_dir, NULL, sizeof _sysfs_dir, _sysfs_dir);
1755
0
}
1756
1757
const char *dm_sysfs_dir(void)
1758
0
{
1759
0
  return _sysfs_dir;
1760
0
}
1761
1762
/*
1763
 * Replace existing uuid_prefix provided it isn't too long.
1764
 */
1765
int dm_set_uuid_prefix(const char *uuid_prefix)
1766
0
{
1767
0
  size_t len;
1768
1769
0
  if (!uuid_prefix)
1770
0
    return_0;
1771
1772
0
  if ((len = strlen(uuid_prefix)) > DM_MAX_UUID_PREFIX_LEN) {
1773
0
    log_error("New uuid prefix %s too long.", uuid_prefix);
1774
0
    return 0;
1775
0
  }
1776
1777
0
  memcpy(_default_uuid_prefix, uuid_prefix, len + 1);
1778
1779
0
  return 1;
1780
0
}
1781
1782
const char *dm_uuid_prefix(void)
1783
0
{
1784
0
  return _default_uuid_prefix;
1785
0
}
1786
1787
static int _is_octal(int a)
1788
0
{
1789
0
  return (((a) & ~7) == '0');
1790
0
}
1791
1792
/* Convert mangled mountinfo into normal ASCII string */
1793
static void _unmangle_mountinfo_string(const char *src, char *buf)
1794
0
{
1795
0
  while (*src) {
1796
0
    if ((*src == '\\') &&
1797
0
        _is_octal(src[1]) && _is_octal(src[2]) && _is_octal(src[3])) {
1798
0
      *buf++ = 64 * (src[1] & 7) + 8 * (src[2] & 7) + (src[3] & 7);
1799
0
      src += 4;
1800
0
    } else
1801
0
      *buf++ = *src++;
1802
0
  }
1803
0
  *buf = '\0';
1804
0
}
1805
1806
/* coverity[+tainted_string_sanitize_content:arg-0] */
1807
0
static int _sanitize_line(const char *line) { return 1; }
1808
1809
/* Parse one line of mountinfo and unmangled target line */
1810
static int _mountinfo_parse_line(const char *line, unsigned *maj, unsigned *min, char *buf)
1811
0
{
1812
0
  char root[PATH_MAX + 1]; /* sscanf needs extra '\0' */
1813
0
  char target[PATH_MAX + 1];
1814
0
  char *devmapper;
1815
0
  struct dm_task *dmt;
1816
0
  struct dm_info info;
1817
0
  unsigned i;
1818
1819
  /* TODO: maybe detect availability of  %ms  glib support ? */
1820
0
  if (sscanf(line, "%*u %*u %u:%u %" DM_TO_STRING(PATH_MAX)
1821
0
       "s %" DM_TO_STRING(PATH_MAX) "s",
1822
0
       maj, min, root, target) < 4) {
1823
0
    log_error("Failed to parse mountinfo line.");
1824
0
    return 0;
1825
0
  }
1826
1827
  /* btrfs fakes device numbers, but there is still /dev/mapper name
1828
   * placed in mountinfo, so try to detect proper major:minor via this */
1829
0
  if (*maj == 0 && (devmapper = strstr(line, "/dev/mapper/"))) {
1830
0
    if (!(dmt = dm_task_create(DM_DEVICE_INFO))) {
1831
0
      log_error("Mount info task creation failed.");
1832
0
      return 0;
1833
0
    }
1834
0
    devmapper += 12; /* skip fixed prefix */
1835
0
    for (i = 0; devmapper[i] && devmapper[i] != ' ' && i < sizeof(root)-1; ++i)
1836
0
      root[i] = devmapper[i];
1837
0
    root[i] = 0;
1838
0
    _unmangle_mountinfo_string(root, buf);
1839
0
    buf[DM_NAME_LEN] = 0; /* cut away */
1840
1841
0
    if (dm_task_set_name(dmt, buf) &&
1842
0
        dm_task_no_open_count(dmt) &&
1843
0
        dm_task_run(dmt) &&
1844
0
        dm_task_get_info(dmt, &info)) {
1845
0
      log_debug("Replacing mountinfo device (%u:%u) with matching DM device %s (%u:%u).",
1846
0
          *maj, *min, buf, info.major, info.minor);
1847
0
      *maj = info.major;
1848
0
      *min = info.minor;
1849
0
    }
1850
0
    dm_task_destroy(dmt);
1851
0
  }
1852
1853
0
  _unmangle_mountinfo_string(target, buf);
1854
1855
0
  return 1;
1856
0
}
1857
1858
/*
1859
 * Function to operate on individual mountinfo line,
1860
 * minor, major and mount target are parsed and unmangled
1861
 */
1862
int dm_mountinfo_read(dm_mountinfo_line_callback_fn read_fn, void *cb_data)
1863
0
{
1864
0
  FILE *minfo;
1865
0
  char buffer[2 * PATH_MAX];
1866
0
  char target[PATH_MAX];
1867
0
  unsigned maj, min;
1868
0
  int r = 1;
1869
1870
0
  if (!(minfo = fopen(_mountinfo, "r"))) {
1871
0
    if (errno != ENOENT)
1872
0
      log_sys_error("fopen", _mountinfo);
1873
0
    else
1874
0
      log_sys_debug("fopen", _mountinfo);
1875
0
    return 0;
1876
0
  }
1877
1878
0
  while (!feof(minfo) && fgets(buffer, sizeof(buffer), minfo))
1879
0
    if (!_sanitize_line(buffer) ||
1880
0
        !_mountinfo_parse_line(buffer, &maj, &min, target) ||
1881
0
        !read_fn(buffer, maj, min, target, cb_data)) {
1882
0
      stack;
1883
0
      r = 0;
1884
0
      break;
1885
0
    }
1886
1887
0
  if (fclose(minfo))
1888
0
    log_sys_error("fclose", _mountinfo);
1889
1890
0
  return r;
1891
0
}
1892
1893
static int _sysfs_get_dm_name(uint32_t major, uint32_t minor, char *buf, size_t buf_size)
1894
0
{
1895
0
  char sysfs_path[PATH_MAX], temp_buf[2 * DM_NAME_LEN];
1896
0
  FILE *fp = NULL;
1897
0
  int r = 0;
1898
0
  size_t len;
1899
1900
0
  if (dm_snprintf(sysfs_path, sizeof(sysfs_path),
1901
0
      "%sdev/block/%" PRIu32 ":%" PRIu32
1902
0
      "/dm/name", _sysfs_dir, major, minor) < 0) {
1903
0
    log_error("_sysfs_get_dm_name: dm_snprintf failed.");
1904
0
    goto bad;
1905
0
  }
1906
1907
0
  if (!(fp = fopen(sysfs_path, "r"))) {
1908
0
    if (errno == ENOENT)
1909
0
      log_sys_debug("fopen", sysfs_path);
1910
0
                else
1911
0
      log_sys_error("fopen", sysfs_path);
1912
0
    goto bad;
1913
0
  }
1914
1915
0
  if (!fgets(temp_buf, sizeof(temp_buf), fp)) {
1916
0
    log_sys_error("fgets", sysfs_path);
1917
0
    goto bad;
1918
0
  }
1919
1920
0
  len = strlen(temp_buf);
1921
1922
0
  if (len > buf_size) {
1923
0
    log_error("_sysfs_get_dm_name: supplied buffer too small.");
1924
0
    goto bad;
1925
0
  }
1926
1927
0
  if (len)
1928
0
    --len;  /* strip \n */
1929
1930
0
  memcpy(buf, temp_buf, len);
1931
0
  buf[len] = '\0';
1932
1933
0
  r = 1;
1934
0
bad:
1935
0
  if (fp && fclose(fp))
1936
0
    log_sys_error("fclose", sysfs_path);
1937
1938
0
  return r;
1939
0
}
1940
1941
static int _sysfs_get_dev_major_minor(const char *path, uint32_t major, uint32_t minor)
1942
0
{
1943
0
  FILE *fp;
1944
0
  uint32_t ma, mi;
1945
0
  int r;
1946
1947
0
  if (!(fp = fopen(path, "r")))
1948
0
    return 0;
1949
1950
0
  r = (fscanf(fp, "%" PRIu32 ":%" PRIu32 , &ma, &mi) == 2) &&
1951
0
    (ma == major) && (mi == minor);
1952
  // log_debug("Checking %s  %u:%u  -> %d", path, ma, mi, r);
1953
1954
0
  if (fclose(fp))
1955
0
    log_sys_error("fclose", path);
1956
1957
0
  return r;
1958
0
}
1959
1960
1961
static int _sysfs_find_kernel_name(uint32_t major, uint32_t minor, char *buf, size_t buf_size)
1962
0
{
1963
0
  const char *name, *name_dev;
1964
0
  char path[PATH_MAX];
1965
0
  struct dirent *dirent, *dirent_dev;
1966
0
  DIR *d, *d_dev;
1967
0
  struct stat st;
1968
0
  int r = 0, sz;
1969
1970
0
  if (!*_sysfs_dir ||
1971
0
      dm_snprintf(path, sizeof(path), "%s/block/", _sysfs_dir) < 0) {
1972
0
    log_error("Failed to build sysfs_path.");
1973
0
    return 0;
1974
0
  }
1975
1976
0
  if (!(d = opendir(path))) {
1977
0
    log_sys_error("opendir", path);
1978
0
    return 0;
1979
0
  }
1980
1981
0
  while (!r && (dirent = readdir(d))) {
1982
0
    name = dirent->d_name;
1983
1984
0
    if (!strcmp(name, ".") || !strcmp(name, ".."))
1985
0
      continue;
1986
1987
0
    if ((sz = dm_snprintf(path, sizeof(path), "%sblock/%s/dev",
1988
0
              _sysfs_dir, name)) < 5) {
1989
0
      log_warn("Couldn't create path for %s.", name);
1990
0
      continue;
1991
0
    }
1992
1993
0
    if (_sysfs_get_dev_major_minor(path, major, minor)) {
1994
0
      r = dm_strncpy(buf, name, buf_size);
1995
0
      break; /* found */
1996
0
    }
1997
1998
0
    path[sz - 4] = 0; /* strip /dev from end of path string */
1999
0
    if (stat(path, &st))
2000
0
      continue;
2001
2002
0
    if (S_ISDIR(st.st_mode)) {
2003
2004
      /* let's assume there is no tree-complex device in past systems */
2005
0
      if (!(d_dev = opendir(path))) {
2006
0
        log_sys_debug("opendir", path);
2007
0
        continue;
2008
0
      }
2009
2010
0
      while ((dirent_dev = readdir(d_dev))) {
2011
0
        name_dev = dirent_dev->d_name;
2012
2013
        /* skip known ignorable paths */
2014
0
        if (!strcmp(name_dev, ".") || !strcmp(name_dev, "..") ||
2015
0
            !strcmp(name_dev, "bdi") ||
2016
0
            !strcmp(name_dev, "dev") ||
2017
0
            !strcmp(name_dev, "device") ||
2018
0
            !strcmp(name_dev, "holders") ||
2019
0
            !strcmp(name_dev, "integrity") ||
2020
0
            !strcmp(name_dev, "loop") ||
2021
0
            !strcmp(name_dev, "queue") ||
2022
0
            !strcmp(name_dev, "md") ||
2023
0
            !strcmp(name_dev, "mq") ||
2024
0
            !strcmp(name_dev, "power") ||
2025
0
            !strcmp(name_dev, "removable") ||
2026
0
            !strcmp(name_dev, "slave") ||
2027
0
            !strcmp(name_dev, "slaves") ||
2028
0
            !strcmp(name_dev, "subsystem") ||
2029
0
            !strcmp(name_dev, "trace") ||
2030
0
            !strcmp(name_dev, "uevent"))
2031
0
          continue;
2032
2033
0
        if (dm_snprintf(path, sizeof(path), "%sblock/%s/%s/dev",
2034
0
            _sysfs_dir, name, name_dev) == -1) {
2035
0
          log_warn("Couldn't create path for %s/%s.", name, name_dev);
2036
0
          continue;
2037
0
        }
2038
2039
0
        if (_sysfs_get_dev_major_minor(path, major, minor)) {
2040
0
          r = dm_strncpy(buf, name_dev, buf_size);
2041
0
          break; /* found */
2042
0
        }
2043
0
      }
2044
2045
0
      if (closedir(d_dev))
2046
0
        log_sys_debug("closedir", name);
2047
0
    }
2048
0
  }
2049
2050
0
  if (closedir(d))
2051
0
    log_sys_debug("closedir", path);
2052
2053
0
  return r;
2054
0
}
2055
2056
static int _sysfs_get_kernel_name(uint32_t major, uint32_t minor, char *buf, size_t buf_size)
2057
0
{
2058
0
  char *name;
2059
0
  char sysfs_path[PATH_MAX], temp_buf[PATH_MAX];
2060
0
  ssize_t size;
2061
0
  int r = 0;
2062
2063
0
  if (dm_snprintf(sysfs_path, sizeof(sysfs_path),
2064
0
      "%sdev/block/%" PRIu32 ":%" PRIu32,
2065
0
      _sysfs_dir, major, minor) < 0) {
2066
0
    log_error("_sysfs_get_kernel_name: dm_snprintf failed");
2067
0
    goto bad;
2068
0
  }
2069
2070
0
  if ((size = readlink(sysfs_path, temp_buf, sizeof(temp_buf) - 1)) < 0) {
2071
0
    if (errno == ENOENT) {
2072
0
      log_sys_debug("readlink", sysfs_path);
2073
0
      r = _sysfs_find_kernel_name(major, minor, buf, buf_size);
2074
0
      goto out;
2075
0
    } else
2076
0
      log_sys_error("readlink", sysfs_path);
2077
0
    goto bad;
2078
0
  }
2079
0
  temp_buf[size] = '\0';
2080
2081
0
  if (!(name = strrchr(temp_buf, '/'))) {
2082
0
    log_error("Could not locate device kernel name in sysfs path %s", temp_buf);
2083
0
    goto bad;
2084
0
  }
2085
2086
0
  if (!_dm_strncpy(buf, name + 1, buf_size)) {
2087
0
    log_error("_sysfs_get_kernel_name: output buffer too small");
2088
0
    goto bad;
2089
0
  }
2090
2091
0
  r = 1;
2092
0
bad:
2093
0
out:
2094
2095
0
  return r;
2096
0
}
2097
2098
int dm_device_get_name(uint32_t major, uint32_t minor, int prefer_kernel_name,
2099
           char *buf, size_t buf_size)
2100
0
{
2101
0
  if (!*_sysfs_dir)
2102
0
    return 0;
2103
2104
  /*
2105
   * device-mapper devices and prefer_kernel_name = 0
2106
   * get dm name by reading /sys/dev/block/major:minor/dm/name,
2107
   * fallback to _sysfs_get_kernel_name if not successful
2108
   */
2109
0
  if (dm_is_dm_major(major) && !prefer_kernel_name) {
2110
0
    if (_sysfs_get_dm_name(major, minor, buf, buf_size))
2111
0
      return 1;
2112
0
    else
2113
0
      stack;
2114
0
  }
2115
2116
  /*
2117
   * non-device-mapper devices or prefer_kernel_name = 1
2118
   * get kernel name using readlink /sys/dev/block/major:minor -> .../dm-X
2119
   */
2120
0
  return _sysfs_get_kernel_name(major, minor, buf, buf_size);
2121
0
}
2122
2123
int dm_device_has_holders(uint32_t major, uint32_t minor)
2124
0
{
2125
0
  char sysfs_path[PATH_MAX];
2126
0
  struct stat st;
2127
2128
0
  if (!*_sysfs_dir)
2129
0
    return 0;
2130
2131
0
  if (dm_snprintf(sysfs_path, PATH_MAX, "%sdev/block/%" PRIu32
2132
0
      ":%" PRIu32 "/holders", _sysfs_dir, major, minor) < 0) {
2133
0
    log_warn("WARNING: sysfs_path dm_snprintf failed.");
2134
0
    return 0;
2135
0
  }
2136
2137
0
  if (stat(sysfs_path, &st)) {
2138
0
    if (errno != ENOENT)
2139
0
      log_sys_debug("stat", sysfs_path);
2140
0
    return 0;
2141
0
  }
2142
2143
0
  return !dm_is_empty_dir(sysfs_path);
2144
0
}
2145
2146
static int _mounted_fs_on_device(const char *kernel_dev_name)
2147
0
{
2148
0
  char sysfs_path[PATH_MAX];
2149
0
  struct dirent *dirent;
2150
0
  DIR *d;
2151
0
  struct stat st;
2152
0
  int r = 0;
2153
2154
0
  if (dm_snprintf(sysfs_path, PATH_MAX, "%sfs", _sysfs_dir) < 0) {
2155
0
    log_warn("WARNING: sysfs_path dm_snprintf failed.");
2156
0
    return 0;
2157
0
  }
2158
2159
0
  if (!(d = opendir(sysfs_path))) {
2160
0
    if (errno != ENOENT)
2161
0
      log_sys_debug("opendir", sysfs_path);
2162
0
    return 0;
2163
0
  }
2164
2165
0
  while ((dirent = readdir(d))) {
2166
0
    if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, ".."))
2167
0
      continue;
2168
2169
0
    if (dm_snprintf(sysfs_path, PATH_MAX, "%sfs/%s/%s",
2170
0
        _sysfs_dir, dirent->d_name, kernel_dev_name) < 0) {
2171
0
      log_warn("WARNING: sysfs_path dm_snprintf failed.");
2172
0
      break;
2173
0
    }
2174
2175
0
    if (!stat(sysfs_path, &st)) {
2176
      /* found! */
2177
0
      r = 1;
2178
0
      break;
2179
0
    }
2180
0
    else if (errno != ENOENT) {
2181
0
      log_sys_debug("stat", sysfs_path);
2182
0
      break;
2183
0
    }
2184
0
  }
2185
2186
0
  if (closedir(d))
2187
0
    log_sys_debug("closedir", kernel_dev_name);
2188
2189
0
  return r;
2190
0
}
2191
2192
struct mountinfo_s {
2193
  unsigned maj;
2194
  unsigned min;
2195
  int mounted;
2196
};
2197
2198
static int _device_has_mounted_fs(char *buffer, unsigned major, unsigned minor,
2199
          char *target, void *cb_data)
2200
0
{
2201
0
  struct mountinfo_s *data = cb_data;
2202
0
  char kernel_dev_name[PATH_MAX];
2203
2204
0
  if ((major == data->maj) && (minor == data->min)) {
2205
0
    if (!dm_device_get_name(major, minor, 1, kernel_dev_name,
2206
0
          sizeof(kernel_dev_name))) {
2207
0
      stack;
2208
0
      *kernel_dev_name = '\0';
2209
0
    }
2210
0
    log_verbose("Device %s (%u:%u) appears to be mounted on %s.",
2211
0
          kernel_dev_name, major, minor, target);
2212
0
    data->mounted = 1;
2213
0
  }
2214
2215
0
  return 1;
2216
0
}
2217
2218
int dm_device_has_mounted_fs(uint32_t major, uint32_t minor)
2219
0
{
2220
0
  char kernel_dev_name[PATH_MAX];
2221
0
  struct mountinfo_s data = {
2222
0
    .maj = major,
2223
0
    .min = minor,
2224
0
  };
2225
2226
0
  if (!dm_mountinfo_read(_device_has_mounted_fs, &data))
2227
0
    stack;
2228
2229
0
  if (data.mounted)
2230
0
    return 1;
2231
  /*
2232
   * TODO: Verify dm_mountinfo_read() is superset
2233
   * and remove sysfs check (namespaces)
2234
   */
2235
  /* Get kernel device name first */
2236
0
  if (!dm_device_get_name(major, minor, 1, kernel_dev_name, PATH_MAX))
2237
0
    return 0;
2238
2239
  /* Check /sys/fs/<fs_name>/<kernel_dev_name> presence */
2240
0
  return _mounted_fs_on_device(kernel_dev_name);
2241
0
}
2242
2243
int dm_mknodes(const char *name)
2244
0
{
2245
0
  struct dm_task *dmt;
2246
0
  int r = 0;
2247
2248
0
  if (!(dmt = dm_task_create(DM_DEVICE_MKNODES)))
2249
0
    return_0;
2250
2251
0
  if (name && !dm_task_set_name(dmt, name))
2252
0
    goto out;
2253
2254
0
  if (!dm_task_no_open_count(dmt))
2255
0
    goto out;
2256
2257
0
  r = dm_task_run(dmt);
2258
2259
0
out:
2260
0
  dm_task_destroy(dmt);
2261
0
  return r;
2262
0
}
2263
2264
int dm_driver_version(char *version, size_t size)
2265
0
{
2266
0
  struct dm_task *dmt;
2267
0
  int r = 0;
2268
2269
0
  if (!(dmt = dm_task_create(DM_DEVICE_VERSION)))
2270
0
    return_0;
2271
2272
0
  if (!dm_task_run(dmt))
2273
0
    log_error("Failed to get driver version");
2274
2275
0
  if (!dm_task_get_driver_version(dmt, version, size))
2276
0
    goto out;
2277
2278
0
  r = 1;
2279
2280
0
out:
2281
0
  dm_task_destroy(dmt);
2282
0
  return r;
2283
0
}
2284
2285
static void _set_cookie_flags(struct dm_task *dmt, uint16_t flags)
2286
0
{
2287
0
  if (!dm_cookie_supported())
2288
0
    return;
2289
2290
0
  if (_udev_disabled) {
2291
    /*
2292
     * If udev is disabled, hardcode this functionality:
2293
     *   - we want libdm to create the nodes
2294
     *   - we don't want the /dev/mapper and any subsystem
2295
     *     related content to be created by udev if udev
2296
     *     rules are installed
2297
     */
2298
0
    flags &= ~DM_UDEV_DISABLE_LIBRARY_FALLBACK;
2299
0
    flags |= DM_UDEV_DISABLE_DM_RULES_FLAG | DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG;
2300
0
  }
2301
2302
0
  dmt->event_nr = flags << DM_UDEV_FLAGS_SHIFT;
2303
0
}
2304
2305
#ifndef UDEV_SYNC_SUPPORT
2306
void dm_udev_set_sync_support(int sync_with_udev)
2307
0
{
2308
0
}
2309
2310
int dm_udev_get_sync_support(void)
2311
0
{
2312
0
  return 0;
2313
0
}
2314
2315
void dm_udev_set_checking(int checking)
2316
0
{
2317
0
}
2318
2319
int dm_udev_get_checking(void)
2320
0
{
2321
0
  return 0;
2322
0
}
2323
2324
int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags)
2325
0
{
2326
0
  _set_cookie_flags(dmt, flags);
2327
2328
0
  *cookie = 0;
2329
0
  dmt->cookie_set = 1;
2330
2331
0
  return 1;
2332
0
}
2333
2334
int dm_udev_complete(uint32_t cookie)
2335
0
{
2336
0
  return 1;
2337
0
}
2338
2339
int dm_udev_wait(uint32_t cookie)
2340
0
{
2341
0
  update_devs();
2342
2343
0
  return 1;
2344
0
}
2345
2346
int dm_udev_wait_immediate(uint32_t cookie, int *ready)
2347
0
{
2348
0
  update_devs();
2349
0
  *ready = 1;
2350
2351
0
  return 1;
2352
0
}
2353
2354
#else   /* UDEV_SYNC_SUPPORT */
2355
2356
static int _check_semaphore_is_supported(void)
2357
{
2358
  int maxid;
2359
  union semun arg;
2360
  struct seminfo seminfo;
2361
2362
  arg.__buf = &seminfo;
2363
  maxid = semctl(0, 0, SEM_INFO, arg);
2364
2365
  if (maxid < 0) {
2366
    log_warn("Kernel not configured for semaphores (System V IPC). "
2367
       "Not using udev synchronization code.");
2368
    return 0;
2369
  }
2370
2371
  return 1;
2372
}
2373
2374
static int _check_udev_is_running(void)
2375
{
2376
  struct udev *udev;
2377
  struct udev_queue *udev_queue;
2378
  int r;
2379
2380
  if (!(udev = udev_new()))
2381
    goto_bad;
2382
2383
  if (!(udev_queue = udev_queue_new(udev))) {
2384
    udev_unref(udev);
2385
    goto_bad;
2386
  }
2387
2388
  if (!(r = udev_queue_get_udev_is_active(udev_queue)))
2389
    log_debug_activation("Udev is not running. "
2390
             "Not using udev synchronization code.");
2391
2392
  udev_queue_unref(udev_queue);
2393
  udev_unref(udev);
2394
2395
  return r;
2396
2397
bad:
2398
  log_error("Could not get udev state. Assuming udev is not running.");
2399
  return 0;
2400
}
2401
2402
static void _check_udev_sync_requirements_once(void)
2403
{
2404
  if (_semaphore_supported < 0)
2405
    _semaphore_supported = _check_semaphore_is_supported();
2406
2407
  if (_udev_running < 0) {
2408
    _udev_running = _check_udev_is_running();
2409
    if (_udev_disabled && _udev_running)
2410
      log_warn("Udev is running and DM_DISABLE_UDEV environment variable is set. "
2411
         "Bypassing udev, device-mapper library will manage device "
2412
         "nodes in device directory.");
2413
  }
2414
}
2415
2416
void dm_udev_set_sync_support(int sync_with_udev)
2417
{
2418
  _check_udev_sync_requirements_once();
2419
  _sync_with_udev = sync_with_udev;
2420
}
2421
2422
int dm_udev_get_sync_support(void)
2423
{
2424
  _check_udev_sync_requirements_once();
2425
2426
  return !_udev_disabled && _semaphore_supported &&
2427
    dm_cookie_supported() &&_udev_running && _sync_with_udev;
2428
}
2429
2430
void dm_udev_set_checking(int checking)
2431
{
2432
  if ((_udev_checking = checking))
2433
    log_debug_activation("DM udev checking enabled");
2434
  else
2435
    log_debug_activation("DM udev checking disabled");
2436
}
2437
2438
int dm_udev_get_checking(void)
2439
{
2440
  return _udev_checking;
2441
}
2442
2443
static int _get_cookie_sem(uint32_t cookie, int *semid)
2444
{
2445
  if (cookie >> 16 != DM_COOKIE_MAGIC) {
2446
    log_error("Could not continue to access notification "
2447
        "semaphore identified by cookie value %"
2448
        PRIu32 " (0x%x). Incorrect cookie prefix.",
2449
        cookie, cookie);
2450
    return 0;
2451
  }
2452
2453
  if ((*semid = semget((key_t) cookie, 1, 0)) >= 0)
2454
    return 1;
2455
2456
  switch (errno) {
2457
    case ENOENT:
2458
      log_error("Could not find notification "
2459
          "semaphore identified by cookie "
2460
          "value %" PRIu32 " (0x%x)",
2461
          cookie, cookie);
2462
      break;
2463
    case EACCES:
2464
      log_error("No permission to access "
2465
          "notification semaphore identified "
2466
          "by cookie value %" PRIu32 " (0x%x)",
2467
          cookie, cookie);
2468
      break;
2469
    default:
2470
      log_error("Failed to access notification "
2471
           "semaphore identified by cookie "
2472
           "value %" PRIu32 " (0x%x): %s",
2473
          cookie, cookie, strerror(errno));
2474
      break;
2475
  }
2476
2477
  return 0;
2478
}
2479
2480
static int _udev_notify_sem_inc(uint32_t cookie, int semid)
2481
{
2482
  struct sembuf sb = {0, 1, 0};
2483
  int val;
2484
2485
  if (semop(semid, &sb, 1) < 0) {
2486
    log_error("cookie inc: semid %d: semop failed for cookie 0x%" PRIx32 ": %s",
2487
        semid, cookie, strerror(errno));
2488
    return 0;
2489
  }
2490
2491
  if ((val = semctl(semid, 0, GETVAL)) < 0) {
2492
    log_warn("cookie inc: semid %d: sem_ctl GETVAL failed for "
2493
        "cookie 0x%" PRIx32 ": %s",
2494
        semid, cookie, strerror(errno));
2495
    log_debug_activation("Udev cookie 0x%" PRIx32 " (semid %d) incremented.",
2496
              cookie, semid);
2497
  } else
2498
    log_debug_activation("Udev cookie 0x%" PRIx32 " (semid %d) incremented to %d",
2499
             cookie, semid, val);
2500
2501
  return 1;
2502
}
2503
2504
static int _udev_notify_sem_dec(uint32_t cookie, int semid)
2505
{
2506
  struct sembuf sb = {0, -1, IPC_NOWAIT};
2507
  int val;
2508
2509
  if ((val = semctl(semid, 0, GETVAL)) < 0)
2510
    log_warn("cookie dec: semid %d: sem_ctl GETVAL failed for "
2511
       "cookie 0x%" PRIx32 ": %s",
2512
       semid, cookie, strerror(errno));
2513
2514
  if (semop(semid, &sb, 1) < 0) {
2515
    switch (errno) {
2516
      case EAGAIN:
2517
        log_error("cookie dec: semid %d: semop failed for cookie "
2518
            "0x%" PRIx32 ": "
2519
            "incorrect semaphore state",
2520
            semid, cookie);
2521
        break;
2522
      default:
2523
        log_error("cookie dec: semid %d: semop failed for cookie "
2524
            "0x%" PRIx32 ": %s",
2525
            semid, cookie, strerror(errno));
2526
        break;
2527
    }
2528
    return 0;
2529
  }
2530
2531
  if (val < 0)
2532
    log_debug_activation("Udev cookie 0x%" PRIx32 " (semid %d) decremented.",
2533
             cookie, semid);
2534
  else
2535
    log_debug_activation("Udev cookie 0x%" PRIx32 " (semid %d) decremented to %d",
2536
             cookie, semid, val - 1);
2537
  return 1;
2538
}
2539
2540
static int _udev_notify_sem_destroy(uint32_t cookie, int semid)
2541
{
2542
  if (semctl(semid, 0, IPC_RMID, 0) < 0) {
2543
    log_error("Could not cleanup notification semaphore "
2544
        "identified by cookie value %" PRIu32 " (0x%x): %s",
2545
        cookie, cookie, strerror(errno));
2546
    return 0;
2547
  }
2548
2549
  log_debug_activation("Udev cookie 0x%" PRIx32 " (semid %d) destroyed", cookie,
2550
           semid);
2551
2552
  return 1;
2553
}
2554
2555
static int _udev_notify_sem_create(uint32_t *cookie, int *semid)
2556
{
2557
  int fd;
2558
  int gen_semid;
2559
  int val;
2560
  uint16_t base_cookie;
2561
  uint32_t gen_cookie;
2562
  union semun sem_arg;
2563
2564
  if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
2565
    log_error("Failed to open /dev/urandom "
2566
        "to create random cookie value");
2567
    *cookie = 0;
2568
    return 0;
2569
  }
2570
2571
  /* Generate random cookie value. Be sure it is unique and non-zero. */
2572
  do {
2573
    /* FIXME Handle non-error returns from read(). Move _io() into libdm? */
2574
    if (read(fd, &base_cookie, sizeof(base_cookie)) != sizeof(base_cookie)) {
2575
      log_error("Failed to initialize notification cookie");
2576
      goto bad;
2577
    }
2578
2579
    gen_cookie = DM_COOKIE_MAGIC << 16 | base_cookie;
2580
2581
    if (base_cookie && (gen_semid = semget((key_t) gen_cookie,
2582
            1, 0600 | IPC_CREAT | IPC_EXCL)) < 0) {
2583
      switch (errno) {
2584
        case EEXIST:
2585
          /* if the semaphore key exists, we
2586
           * simply generate another random one */
2587
          base_cookie = 0;
2588
          break;
2589
        case ENOMEM:
2590
          log_error("Not enough memory to create "
2591
              "notification semaphore");
2592
          goto bad;
2593
        case ENOSPC:
2594
          log_error("Limit for the maximum number "
2595
              "of semaphores reached. You can "
2596
              "check and set the limits in "
2597
              "/proc/sys/kernel/sem.");
2598
          goto bad;
2599
        default:
2600
          log_error("Failed to create notification "
2601
              "semaphore: %s", strerror(errno));
2602
          goto bad;
2603
      }
2604
    }
2605
  } while (!base_cookie);
2606
2607
  log_debug_activation("Udev cookie 0x%" PRIx32 " (semid %d) created",
2608
           gen_cookie, gen_semid);
2609
2610
  sem_arg.val = 1;
2611
2612
  if (semctl(gen_semid, 0, SETVAL, sem_arg) < 0) {
2613
    log_error("cookie create: semid %d: semctl failed: %s", gen_semid, strerror(errno));
2614
    /* We have to destroy just created semaphore
2615
     * so it won't stay in the system. */
2616
    (void) _udev_notify_sem_destroy(gen_cookie, gen_semid);
2617
    goto bad;
2618
  }
2619
2620
  if ((val = semctl(gen_semid, 0, GETVAL)) < 0) {
2621
    log_error("cookie create: semid %d: sem_ctl GETVAL failed for "
2622
        "cookie 0x%" PRIx32 ": %s",
2623
        gen_semid, gen_cookie, strerror(errno));
2624
    (void) _udev_notify_sem_destroy(gen_cookie, gen_semid);
2625
    goto bad;
2626
  }
2627
2628
  log_debug_activation("Udev cookie 0x%" PRIx32 " (semid %d) incremented to %d",
2629
           gen_cookie, gen_semid, val);
2630
2631
  if (close(fd))
2632
    stack;
2633
2634
  *semid = gen_semid;
2635
  *cookie = gen_cookie;
2636
2637
  return 1;
2638
2639
bad:
2640
  if (close(fd))
2641
    stack;
2642
2643
  *cookie = 0;
2644
2645
  return 0;
2646
}
2647
2648
int dm_udev_create_cookie(uint32_t *cookie)
2649
{
2650
  int semid;
2651
2652
  if (!dm_udev_get_sync_support()) {
2653
    *cookie = 0;
2654
    return 1;
2655
  }
2656
2657
  return _udev_notify_sem_create(cookie, &semid);
2658
}
2659
2660
static const char *_task_type_disp(int type)
2661
{
2662
  switch(type) {
2663
  case DM_DEVICE_CREATE:
2664
    return "CREATE";
2665
        case DM_DEVICE_RELOAD:
2666
    return "RELOAD";
2667
        case DM_DEVICE_REMOVE:
2668
    return "REMOVE";
2669
        case DM_DEVICE_REMOVE_ALL:
2670
    return "REMOVE_ALL";
2671
        case DM_DEVICE_SUSPEND:
2672
    return "SUSPEND";
2673
        case DM_DEVICE_RESUME:
2674
    return "RESUME";
2675
        case DM_DEVICE_INFO:
2676
    return "INFO";
2677
        case DM_DEVICE_DEPS:
2678
    return "DEPS";
2679
        case DM_DEVICE_RENAME:
2680
    return "RENAME";
2681
        case DM_DEVICE_VERSION:
2682
    return "VERSION";
2683
        case DM_DEVICE_STATUS:
2684
    return "STATUS";
2685
        case DM_DEVICE_TABLE:
2686
    return "TABLE";
2687
        case DM_DEVICE_WAITEVENT:
2688
    return "WAITEVENT";
2689
        case DM_DEVICE_LIST:
2690
    return "LIST";
2691
        case DM_DEVICE_CLEAR:
2692
    return "CLEAR";
2693
        case DM_DEVICE_MKNODES:
2694
    return "MKNODES";
2695
        case DM_DEVICE_LIST_VERSIONS:
2696
    return "LIST_VERSIONS";
2697
        case DM_DEVICE_TARGET_MSG:
2698
    return "TARGET_MSG";
2699
        case DM_DEVICE_SET_GEOMETRY:
2700
    return "SET_GEOMETRY";
2701
  }
2702
  return "unknown";
2703
}
2704
2705
int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags)
2706
{
2707
  int semid;
2708
2709
  _set_cookie_flags(dmt, flags);
2710
2711
  if (!dm_udev_get_sync_support()) {
2712
    *cookie = 0;
2713
    dmt->cookie_set = 1;
2714
    return 1;
2715
  }
2716
2717
  if (*cookie) {
2718
    if (!_get_cookie_sem(*cookie, &semid))
2719
      goto_bad;
2720
  } else if (!_udev_notify_sem_create(cookie, &semid))
2721
    goto_bad;
2722
2723
  if (!_udev_notify_sem_inc(*cookie, semid)) {
2724
    log_error("Could not set notification semaphore "
2725
        "identified by cookie value %" PRIu32 " (0x%x)",
2726
        *cookie, *cookie);
2727
    goto bad;
2728
  }
2729
2730
  dmt->event_nr |= ~DM_UDEV_FLAGS_MASK & *cookie;
2731
  dmt->cookie_set = 1;
2732
2733
  log_debug_activation("Udev cookie 0x%" PRIx32 " (semid %d) assigned to "
2734
           "%s task(%d) with flags%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s (0x%" PRIx16 ")",
2735
           *cookie, semid, _task_type_disp(dmt->type), dmt->type,
2736
           (flags & DM_UDEV_DISABLE_DM_RULES_FLAG) ? " DISABLE_DM_RULES" : "",
2737
           (flags & DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG) ? " DISABLE_SUBSYSTEM_RULES" : "",
2738
           (flags & DM_UDEV_DISABLE_DISK_RULES_FLAG) ? " DISABLE_DISK_RULES" : "",
2739
           (flags & DM_UDEV_DISABLE_OTHER_RULES_FLAG) ? " DISABLE_OTHER_RULES" : "",
2740
           (flags & DM_UDEV_LOW_PRIORITY_FLAG) ? " LOW_PRIORITY" : "",
2741
           (flags & DM_UDEV_DISABLE_LIBRARY_FALLBACK) ? " DISABLE_LIBRARY_FALLBACK" : "",
2742
           (flags & DM_UDEV_PRIMARY_SOURCE_FLAG) ? " PRIMARY_SOURCE" : "",
2743
           (flags & DM_SUBSYSTEM_UDEV_FLAG0) ? " SUBSYSTEM_0" : " ",
2744
           (flags & DM_SUBSYSTEM_UDEV_FLAG1) ? " SUBSYSTEM_1" : " ",
2745
           (flags & DM_SUBSYSTEM_UDEV_FLAG2) ? " SUBSYSTEM_2" : " ",
2746
           (flags & DM_SUBSYSTEM_UDEV_FLAG3) ? " SUBSYSTEM_3" : " ",
2747
           (flags & DM_SUBSYSTEM_UDEV_FLAG4) ? " SUBSYSTEM_4" : " ",
2748
           (flags & DM_SUBSYSTEM_UDEV_FLAG5) ? " SUBSYSTEM_5" : " ",
2749
           (flags & DM_SUBSYSTEM_UDEV_FLAG6) ? " SUBSYSTEM_6" : " ",
2750
           (flags & DM_SUBSYSTEM_UDEV_FLAG7) ? " SUBSYSTEM_7" : " ",
2751
           flags);
2752
2753
  return 1;
2754
2755
bad:
2756
  dmt->event_nr = 0;
2757
  return 0;
2758
}
2759
2760
int dm_udev_complete(uint32_t cookie)
2761
{
2762
  int semid;
2763
2764
  if (!cookie || !dm_udev_get_sync_support())
2765
    return 1;
2766
2767
  if (!_get_cookie_sem(cookie, &semid))
2768
    return_0;
2769
2770
  if (!_udev_notify_sem_dec(cookie, semid)) {
2771
    log_error("Could not signal waiting process using notification "
2772
        "semaphore identified by cookie value %" PRIu32 " (0x%x)",
2773
        cookie, cookie);
2774
    return 0;
2775
  }
2776
2777
  return 1;
2778
}
2779
2780
/*
2781
 * If *nowait is set, return immediately leaving it set if the semaphore
2782
 * is not ready to be decremented to 0.  *nowait is cleared if the wait
2783
 * succeeds.
2784
 */
2785
static int _udev_wait(uint32_t cookie, int *nowait)
2786
{
2787
  int semid;
2788
  struct sembuf sb = {0, 0, 0};
2789
  int val;
2790
2791
  if (!cookie || !dm_udev_get_sync_support())
2792
    return 1;
2793
2794
  if (!_get_cookie_sem(cookie, &semid))
2795
    return_0;
2796
2797
  /* Return immediately if the semaphore value exceeds 1? */
2798
  if (*nowait) {
2799
    if ((val = semctl(semid, 0, GETVAL)) < 0) {
2800
      log_error("semid %d: sem_ctl GETVAL failed for "
2801
          "cookie 0x%" PRIx32 ": %s",
2802
          semid, cookie, strerror(errno));
2803
      return 0;   
2804
    }
2805
2806
    if (val > 1)
2807
      return 1;
2808
2809
    *nowait = 0;
2810
  }
2811
2812
  if (!_udev_notify_sem_dec(cookie, semid)) {
2813
    log_error("Failed to set a proper state for notification "
2814
        "semaphore identified by cookie value %" PRIu32 " (0x%x) "
2815
        "to initialize waiting for incoming notifications.",
2816
        cookie, cookie);
2817
    (void) _udev_notify_sem_destroy(cookie, semid);
2818
    return 0;
2819
  }
2820
2821
  log_debug_activation("Udev cookie 0x%" PRIx32 " (semid %d) waiting for zero",
2822
           cookie, semid);
2823
2824
repeat_wait:
2825
  if (semop(semid, &sb, 1) < 0) {
2826
    if (errno == EINTR)
2827
      goto repeat_wait;
2828
    else if (errno == EIDRM)
2829
      return 1;
2830
2831
    log_error("Could not set wait state for notification semaphore "
2832
        "identified by cookie value %" PRIu32 " (0x%x): %s",
2833
        cookie, cookie, strerror(errno));
2834
    (void) _udev_notify_sem_destroy(cookie, semid);
2835
    return 0;
2836
  }
2837
2838
  return _udev_notify_sem_destroy(cookie, semid);
2839
}
2840
2841
int dm_udev_wait(uint32_t cookie)
2842
{
2843
  int nowait = 0;
2844
  int r = _udev_wait(cookie, &nowait);
2845
2846
  update_devs();
2847
2848
  return r;
2849
}
2850
2851
int dm_udev_wait_immediate(uint32_t cookie, int *ready)
2852
{
2853
  int nowait = 1;
2854
  int r = _udev_wait(cookie, &nowait);
2855
2856
  if (r && nowait) {
2857
    *ready = 0;
2858
    return 1;
2859
  }
2860
2861
  update_devs();
2862
  *ready = 1;
2863
2864
  return r;
2865
}
2866
#endif    /* UDEV_SYNC_SUPPORT */