return 0;
}
-int get_ctty(pid_t pid, dev_t *_devnr, char **r) {
- char fn[STRLEN("/dev/char/") + 2*DECIMAL_STR_MAX(unsigned) + 1 + 1], *b = NULL;
- _cleanup_free_ char *s = NULL;
- const char *p;
+int get_ctty(pid_t pid, dev_t *ret_devnr, char **ret) {
+ _cleanup_free_ char *fn = NULL, *b = NULL;
dev_t devnr;
- int k;
-
- assert(r);
-
- k = get_ctty_devnr(pid, &devnr);
- if (k < 0)
- return k;
-
- sprintf(fn, "/dev/char/%u:%u", major(devnr), minor(devnr));
+ int r;
- k = readlink_malloc(fn, &s);
- if (k < 0) {
+ r = get_ctty_devnr(pid, &devnr);
+ if (r < 0)
+ return r;
- if (k != -ENOENT)
- return k;
+ r = device_path_make_canonical(S_IFCHR, devnr, &fn);
+ if (r < 0) {
+ if (r != -ENOENT) /* No symlink for this in /dev/char/? */
+ return r;
- /* This is an ugly hack */
if (major(devnr) == 136) {
+ /* This is an ugly hack: PTY devices are not listed in /dev/char/, as they don't follow the
+ * Linux device model. This means we have no nice way to match them up against their actual
+ * device node. Let's hence do the check by the fixed, assigned major number. Normally we try
+ * to avoid such fixed major/minor matches, but there appears to nother nice way to handle
+ * this. */
+
if (asprintf(&b, "pts/%u", minor(devnr)) < 0)
return -ENOMEM;
} else {
- /* Probably something like the ptys which have no
- * symlink in /dev/char. Let's return something
- * vaguely useful. */
+ /* Probably something similar to the ptys which have no symlink in /dev/char/. Let's return
+ * something vaguely useful. */
- b = strdup(fn + 5);
- if (!b)
- return -ENOMEM;
+ r = device_path_make_major_minor(S_IFCHR, devnr, &fn);
+ if (r < 0)
+ return r;
}
- } else {
- p = PATH_STARTSWITH_SET(s, "/dev/", "../");
- if (!p)
- p = s;
+ }
- b = strdup(p);
- if (!b)
- return -ENOMEM;
+ if (!b) {
+ const char *w;
+
+ w = path_startswith(fn, "/dev/");
+ if (w) {
+ b = strdup(w);
+ if (!b)
+ return -ENOMEM;
+ } else
+ b = TAKE_PTR(fn);
}
- *r = b;
- if (_devnr)
- *_devnr = devnr;
+ if (ret)
+ *ret = TAKE_PTR(b);
+
+ if (ret_devnr)
+ *ret_devnr = devnr;
return 0;
}
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
+#include "stat-util.h"
#include "strv.h"
static const char *arg_target = NULL;
#if HAVE_LIBCRYPTSETUP
static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) {
- char devpath[DEV_NUM_PATH_MAX], main_devpath[DEV_NUM_PATH_MAX];
- _cleanup_close_ int main_devfd = -1;
+ _cleanup_free_ char *devpath = NULL, *main_devpath = NULL;
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
+ _cleanup_close_ int main_devfd = -1;
uint64_t size;
int r;
- xsprintf_dev_num_path(main_devpath, "block", main_devno);
+ r = device_path_make_major_minor(S_IFBLK, main_devno, &main_devpath);
+ if (r < 0)
+ return log_error_errno(r, "Failed to format device major/minor path: %m");
+
main_devfd = open(main_devpath, O_RDONLY|O_CLOEXEC);
if (main_devfd < 0)
return log_error_errno(errno, "Failed to open \"%s\": %m", main_devpath);
main_devpath);
log_debug("%s is %"PRIu64" bytes", main_devpath, size);
+ r = device_path_make_major_minor(S_IFBLK, devno, &devpath);
+ if (r < 0)
+ return log_error_errno(r, "Failed to format major/minor path: %m");
- xsprintf_dev_num_path(devpath, "block", devno);
r = crypt_init(&cd, devpath);
if (r < 0)
return log_error_errno(r, "crypt_init(\"%s\") failed: %m", devpath);
#endif
static int maybe_resize_slave_device(const char *mountpath, dev_t main_devno) {
+ _cleanup_free_ char *fstype = NULL, *devpath = NULL;
dev_t devno;
- char devpath[DEV_NUM_PATH_MAX];
- _cleanup_free_ char *fstype = NULL;
int r;
#if HAVE_LIBCRYPTSETUP
if (devno == main_devno)
return 0;
- xsprintf_dev_num_path(devpath, "block", devno);
+ r = device_path_make_major_minor(S_IFBLK, devno, &devpath);
+ if (r < 0)
+ return log_error_errno(r, "Failed to format device major/minor path: %m");
+
r = probe_filesystem(devpath, &fstype);
if (r == -EUCLEAN)
return log_warning_errno(r, "Cannot reliably determine probe \"%s\", refusing to proceed.", devpath);
}
int main(int argc, char *argv[]) {
- dev_t devno;
_cleanup_close_ int mountfd = -1, devfd = -1;
- int blocksize;
+ _cleanup_free_ char *devpath = NULL;
uint64_t size, numblocks;
- char devpath[DEV_NUM_PATH_MAX], fb[FORMAT_BYTES_MAX];
+ char fb[FORMAT_BYTES_MAX];
struct statfs sfs;
+ dev_t devno;
+ int blocksize;
int r;
log_setup_service();
return EXIT_FAILURE;
}
- xsprintf_dev_num_path(devpath, "block", devno);
+ r = device_path_make_major_minor(S_IFBLK, devno, &devpath);
+ if (r < 0) {
+ log_error_errno(r, "Failed to format device major/minor path: %m");
+ return EXIT_FAILURE;
+ }
+
devfd = open(devpath, O_RDONLY|O_CLOEXEC);
if (devfd < 0) {
log_error_errno(errno, "Failed to open \"%s\": %m", devpath);
sd_id128_t *ret_uuid) {
#if HAVE_BLKID
_cleanup_(blkid_free_probep) blkid_probe b = NULL;
- char t[DEV_NUM_PATH_MAX];
+ _cleanup_free_ char *node = NULL;
const char *v;
#endif
uint64_t pstart = 0, psize = 0;
goto finish;
#if HAVE_BLKID
- xsprintf_dev_num_path(t, "block", st.st_dev);
+ r = device_path_make_major_minor(S_IFBLK, st.st_dev, &node);
+ if (r < 0)
+ return log_error_errno(r, "Failed to format major/minor device path: %m");
errno = 0;
- b = blkid_new_probe_from_filename(t);
+ b = blkid_new_probe_from_filename(node);
if (!b)
return log_error_errno(errno ?: ENOMEM, "Failed to open file system \"%s\": %m", p);