Skip to content

Commit 9c8810c

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 9c8810c

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

examples/ioctl.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,29 +152,26 @@ 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)
166+
if (ret)
167167
goto error;
168168

169169
pr_alert("%s driver(major: %d) installed.\n", DRIVER_NAME,
170170
test_ioctl_major);
171171
return 0;
172172
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;
173+
unregister_chrdev_region(dev, num_of_dev);
174+
return ret;
178175
}
179176

180177
static void __exit ioctl_exit(void)

0 commit comments

Comments
 (0)