Skip to content

Commit 5087227

Browse files
committed
ioctl: Clean up error handling and return codes
Simplify the init routine by: - Removing unnecessary temporary variables - Adding spacing between declarations and statements - Using early return on alloc_chrdev_region() failure - Propagating the actual error code instead of always returning -1 - Dropping redundant checks in the error path This makes the error handling clearer and more consistent with common kernel coding style.
1 parent a8b652b commit 5087227

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

examples/ioctl.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,29 +152,25 @@ static struct file_operations fops = {
152152
static int __init ioctl_init(void)
153153
{
154154
dev_t dev;
155-
int alloc_ret = -1;
156-
int cdev_ret = -1;
157-
alloc_ret = alloc_chrdev_region(&dev, 0, num_of_dev, DRIVER_NAME);
155+
int ret;
158156

159-
if (alloc_ret)
160-
goto error;
157+
ret = alloc_chrdev_region(&dev, 0, num_of_dev, DRIVER_NAME);
158+
159+
if (ret)
160+
return ret;
161161

162162
test_ioctl_major = MAJOR(dev);
163163
cdev_init(&test_ioctl_cdev, &fops);
164-
cdev_ret = cdev_add(&test_ioctl_cdev, dev, num_of_dev);
164+
ret = cdev_add(&test_ioctl_cdev, dev, num_of_dev);
165165

166-
if (cdev_ret)
167-
goto error;
166+
if (ret) {
167+
unregister_chrdev_region(dev, num_of_dev);
168+
return ret;
169+
}
168170

169171
pr_alert("%s driver(major: %d) installed.\n", DRIVER_NAME,
170172
test_ioctl_major);
171173
return 0;
172-
error:
173-
if (cdev_ret == 0)
174-
cdev_del(&test_ioctl_cdev);
175-
if (alloc_ret == 0)
176-
unregister_chrdev_region(dev, num_of_dev);
177-
return -1;
178174
}
179175

180176
static void __exit ioctl_exit(void)

0 commit comments

Comments
 (0)