From: Masatake YAMATO Date: Wed, 4 Feb 2026 20:42:16 +0000 (+0900) Subject: lsfd: (refactor) call ioctl(TUNGETDEVNETNS) from target_fd related methods X-Git-Tag: v2.43-devel~46^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1154bb50ea8d7e2b16ad1413ad7c2058af6745d2;p=thirdparty%2Futil-linux.git lsfd: (refactor) call ioctl(TUNGETDEVNETNS) from target_fd related methods In the original code, the attach_xinfo method of the cdev_tun_ops struct called the ioctl. The attach_xinfo method retrieved the fd passing to the ioctl by calling call_with_foreign_fd utility function. In the last commit, we added the target_fd related methods in cdev_tun_ops struct. The caller side of the target_fd related methods calls call_with_foreign_fd. We can reduce the code calling call_with_foreign_fd by moving the code calling ioctl from the attach_xinfo method to the target_fd related methods. Signed-off-by: Masatake YAMATO --- diff --git a/lsfd-cmd/cdev.c b/lsfd-cmd/cdev.c index 4c996100b..b9252e3bc 100644 --- a/lsfd-cmd/cdev.c +++ b/lsfd-cmd/cdev.c @@ -470,39 +470,42 @@ static int cdev_tun_handle_fdinfo(struct cdev *cdev, const char *key, const char } #ifdef TUNGETDEVNETNS -static int cdev_tun_get_devnetns(int tfd, void *data) +static bool cdev_tun_needs_target_fd(struct cdev *cdev) { - struct tundata *tundata = data; - int nsfd = ioctl(tfd, TUNGETDEVNETNS); + struct tundata *tundata = cdev->cdev_data; + + return (tundata->iff != NULL); +} +#else +static bool cdev_tun_needs_target_fd(struct cdev *cdev __attribute__((__unused__))) +{ + return false; +} +#endif /* TUNGETDEVNETNS */ + +#ifdef TUNGETDEVNETNS +static void cdev_tun_inspect_target_fd(struct cdev *cdev, int fd) +{ + struct tundata *tundata = cdev->cdev_data; + + int nsfd = ioctl(fd, TUNGETDEVNETNS); struct stat sb; if (nsfd < 0) - return -1; + return; if (fstat(nsfd, &sb) == 0) tundata->devnetns = sb.st_ino; close(nsfd); - - return 0; } -#endif - -static void cdev_tun_attach_xinfo(struct cdev *cdev) +#else +static void cdev_tun_inspect_target_fd(struct cdev *cdev __attribute__((__unused__)), + int fd __attribute__((__unused__))) { - struct tundata *tundata = cdev->cdev_data; - - if (tundata->iff == NULL) - return; - -#ifdef TUNGETDEVNETNS - { - struct file *file = &cdev->file; - call_with_foreign_fd(file->proc->pid, file->association, - cdev_tun_get_devnetns, tundata); - } -#endif + /* Do nothing */ } +#endif /* TUNGETDEVNETNS */ static struct cdev_ops cdev_tun_ops = { .parent = &cdev_misc_ops, @@ -511,7 +514,8 @@ static struct cdev_ops cdev_tun_ops = { .get_name = cdev_tun_get_name, .fill_column = cdev_tun_fill_column, .handle_fdinfo = cdev_tun_handle_fdinfo, - .attach_xinfo = cdev_tun_attach_xinfo, + .needs_target_fd = cdev_tun_needs_target_fd, + .inspect_target_fd = cdev_tun_inspect_target_fd, }; /*