Merge pull request #24622 from yuwata/udev-open-with-noctty

udev: open with O_NOCTTY
This commit is contained in:
Lennart Poettering
2022-09-17 19:03:15 +02:00
committed by GitHub
12 changed files with 33 additions and 27 deletions

View File

@@ -439,7 +439,7 @@ int main(int argc, char *argv[]) {
return 1;
}
fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY);
if (fd < 0) {
log_error("unable to open '%s'", node);
return 1;

View File

@@ -743,7 +743,7 @@ static int open_drive(Context *c) {
assert(c->fd < 0);
for (int cnt = 0;; cnt++) {
fd = open(arg_node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
fd = open(arg_node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY);
if (fd >= 0)
break;
if (++cnt >= 20 || errno != EBUSY)

View File

@@ -67,7 +67,7 @@ static int run(int argc, char **argv) {
if (!desc_path)
return log_oom();
fd = open(desc_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
fd = open(desc_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NOCTTY);
if (fd < 0)
return log_device_error_errno(hid_device, errno,
"Failed to open report descriptor at '%s': %m", desc_path);

View File

@@ -41,7 +41,7 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}
mtd_fd = open(argv[1], O_RDONLY|O_CLOEXEC);
mtd_fd = open(argv[1], O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (mtd_fd < 0) {
log_error_errno(errno, "Failed to open: %m");
return EXIT_FAILURE;

View File

@@ -751,7 +751,7 @@ int scsi_std_inquiry(struct scsi_id_device *dev_scsi, const char *devname) {
struct stat statbuf;
int err = 0;
fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC | O_NOCTTY);
if (fd < 0) {
log_debug_errno(errno, "scsi_id: cannot open %s: %m", devname);
return 1;
@@ -795,7 +795,7 @@ int scsi_get_serial(struct scsi_id_device *dev_scsi, const char *devname,
for (cnt = 20; cnt > 0; cnt--) {
struct timespec duration;
fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC | O_NOCTTY);
if (fd >= 0 || errno != EBUSY)
break;
duration.tv_sec = 0;

View File

@@ -310,7 +310,7 @@ static int builtin_blkid(sd_device *dev, sd_netlink **rtnl, int argc, char *argv
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get device name: %m");
fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd < 0) {
bool ignore = ERRNO_IS_DEVICE_ABSENT(fd);
log_device_debug_errno(dev, fd, "Failed to open block device %s%s: %m",

View File

@@ -21,7 +21,7 @@ static int builtin_btrfs(sd_device *dev, sd_netlink **rtnl, int argc, char *argv
if (argc != 3 || !streq(argv[1], "ready"))
return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invalid arguments");
fd = open("/dev/btrfs-control", O_RDWR|O_CLOEXEC);
fd = open("/dev/btrfs-control", O_RDWR|O_CLOEXEC|O_NOCTTY);
if (fd < 0) {
if (ERRNO_IS_DEVICE_ABSENT(errno)) {
/* Driver not installed? Then we aren't ready. This is useful in initrds that lack

View File

@@ -50,7 +50,7 @@ static void extract_info(sd_device *dev, bool test) {
struct input_absinfo xabsinfo = {}, yabsinfo = {};
_cleanup_close_ int fd = -1;
fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd < 0)
return;

View File

@@ -229,23 +229,29 @@ static int dev_pci_onboard(sd_device *dev, const LinkInfo *info, NetNames *names
}
/* read the 256 bytes PCI configuration space to check the multi-function bit */
static bool is_pci_multifunction(sd_device *dev) {
_cleanup_close_ int fd = -1;
static int is_pci_multifunction(sd_device *dev) {
_cleanup_free_ uint8_t *config = NULL;
const char *filename, *syspath;
uint8_t config[64];
size_t len;
int r;
if (sd_device_get_syspath(dev, &syspath) < 0)
return false;
r = sd_device_get_syspath(dev, &syspath);
if (r < 0)
return r;
filename = strjoina(syspath, "/config");
fd = open(filename, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return false;
if (read(fd, &config, sizeof(config)) != sizeof(config))
return false;
r = read_virtual_file(filename, PCI_HEADER_TYPE + 1, (char **) &config, &len);
if (r < 0)
return r;
if (len < PCI_HEADER_TYPE + 1)
return -EINVAL;
#ifndef PCI_HEADER_TYPE_MULTIFUNC
#define PCI_HEADER_TYPE_MULTIFUNC 0x80
#endif
/* bit 0-6 header type, bit 7 multi/single function device */
return config[PCI_HEADER_TYPE] & 0x80;
return config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTIFUNC;
}
static bool is_pci_ari_enabled(sd_device *dev) {
@@ -384,7 +390,7 @@ static int dev_pci_slot(sd_device *dev, const LinkInfo *info, NetNames *names) {
if (domain > 0)
l = strpcpyf(&s, l, "P%u", domain);
l = strpcpyf(&s, l, "p%us%u", bus, slot);
if (func > 0 || is_pci_multifunction(names->pcidev))
if (func > 0 || is_pci_multifunction(names->pcidev) > 0)
l = strpcpyf(&s, l, "f%u", func);
if (!isempty(info->phys_port_name))
/* kernel provided front panel port name for multi-port PCI device */
@@ -452,7 +458,7 @@ static int dev_pci_slot(sd_device *dev, const LinkInfo *info, NetNames *names) {
* devices that will try to claim the same index and that would create name
* collision. */
if (naming_scheme_has(NAMING_BRIDGE_NO_SLOT) && is_pci_bridge(hotplug_slot_dev)) {
if (naming_scheme_has(NAMING_BRIDGE_MULTIFUNCTION_SLOT) && !is_pci_multifunction(names->pcidev)) {
if (naming_scheme_has(NAMING_BRIDGE_MULTIFUNCTION_SLOT) && is_pci_multifunction(names->pcidev) <= 0) {
log_device_debug(dev, "Not using slot information because the PCI device associated with the hotplug slot is a bridge and the PCI device has single function.");
return 0;
}
@@ -479,7 +485,7 @@ static int dev_pci_slot(sd_device *dev, const LinkInfo *info, NetNames *names) {
if (domain > 0)
l = strpcpyf(&s, l, "P%u", domain);
l = strpcpyf(&s, l, "s%"PRIu32, hotplug_slot);
if (func > 0 || is_pci_multifunction(names->pcidev))
if (func > 0 || is_pci_multifunction(names->pcidev) > 0)
l = strpcpyf(&s, l, "f%u", func);
if (!isempty(info->phys_port_name))
l = strpcpyf(&s, l, "n%s", info->phys_port_name);

View File

@@ -158,7 +158,7 @@ static int dev_if_packed_info(sd_device *dev, char *ifs_str, size_t len) {
return r;
filename = strjoina(syspath, "/descriptors");
fd = open(filename, O_RDONLY|O_CLOEXEC);
fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return log_device_debug_errno(dev, errno, "Failed to open \"%s\": %m", filename);

View File

@@ -180,7 +180,7 @@ static int lock_device(
struct stat st;
int r;
fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd < 0)
return log_error_errno(errno, "Failed to open '%s': %m", path);

View File

@@ -544,7 +544,7 @@ static int worker_lock_whole_disk(sd_device *dev, int *ret_fd) {
if (r == 0)
goto nolock;
fd = sd_device_open(dev_whole_disk, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
fd = sd_device_open(dev_whole_disk, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd < 0) {
bool ignore = ERRNO_IS_DEVICE_ABSENT(fd);
@@ -599,7 +599,7 @@ static int worker_mark_block_device_read_only(sd_device *dev) {
if (STARTSWITH_SET(val, "dm-", "md", "drbd", "loop", "nbd", "zram"))
return 0;
fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd < 0)
return log_device_debug_errno(dev, fd, "Failed to open '%s', ignoring: %m", val);