Skip to content

Commit fe83144

Browse files
committed
Fixing segfaults
1 parent 83aba1e commit fe83144

File tree

1 file changed

+53
-60
lines changed

1 file changed

+53
-60
lines changed

src/evdev/input_hook.c

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -143,89 +143,86 @@ struct hook_info {
143143
};
144144

145145
static int create_hook_info(char *path, struct hook_info **info) {
146-
*info = malloc(sizeof(struct hook_info));
147-
if (info == NULL) {
146+
struct hook_info *hook = *info = malloc(sizeof(struct hook_info));
147+
if (hook == NULL) {
148148
logger(LOG_LEVEL_ERROR, "%s [%u]: Failed to allocate memory for evdev buffer!\n",
149149
__FUNCTION__, __LINE__);
150150

151151
return UIOHOOK_ERROR_OUT_OF_MEMORY;
152152
}
153153

154-
int test = open(path, O_RDONLY | O_NONBLOCK);
155-
(*info)->fd = test;
156-
if ((*info)->fd < 0) {
157-
logger(LOG_LEVEL_ERROR, "%s [%u]: Failed to open file: %s! (%d)\n",
154+
hook->fd = open(path, O_RDONLY | O_NONBLOCK);
155+
if (hook->fd < 0) {
156+
logger(LOG_LEVEL_ERROR, "%s [%u]: Failed to open input device: %s! (%d)\n",
158157
__FUNCTION__, __LINE__,
159158
path, errno);
160-
161159
return UIOHOOK_FAILURE;
162160
}
163161

164-
fprintf(stderr, "Open FD Testing: %d\n", test);
165-
166162

167-
int err = libevdev_new_from_fd((*info)->fd, &(*info)->evdev);
163+
int err = libevdev_new_from_fd(hook->fd, &hook->evdev);
168164
if (err < 0) {
169165
logger(LOG_LEVEL_ERROR, "%s [%u]: Failed to create evdev from file descriptor! (%d)\n",
170166
__FUNCTION__, __LINE__,
171167
err);
172-
173168
return UIOHOOK_FAILURE;
174169
}
175170

176-
err = libevdev_uinput_create_from_device((*info)->evdev, LIBEVDEV_UINPUT_OPEN_MANAGED, &(*info)->uinput);
171+
err = libevdev_uinput_create_from_device(hook->evdev, LIBEVDEV_UINPUT_OPEN_MANAGED, &hook->uinput);
177172
if (err < 0) {
178173
logger(LOG_LEVEL_WARN, "%s [%u]: Failed to create uinput from device! (%d)\n",
179174
__FUNCTION__, __LINE__,
180175
err);
181176

182-
(*info)->uinput = NULL;
177+
hook->uinput = NULL;
183178
}
184179

185180
char *label;
186-
if (libevdev_has_event_type((*info)->evdev, EV_REP) && libevdev_has_event_code((*info)->evdev, EV_KEY, KEY_ESC)) {
181+
if (libevdev_has_event_type(hook->evdev, EV_REP) && libevdev_has_event_code(hook->evdev, EV_KEY, KEY_ESC)) {
187182
label = "keyboard";
188-
} else if (libevdev_has_event_type((*info)->evdev, EV_REL) && libevdev_has_event_code((*info)->evdev, EV_KEY, BTN_LEFT)) {
183+
} else if (libevdev_has_event_type(hook->evdev, EV_REL) && libevdev_has_event_code(hook->evdev, EV_KEY, BTN_LEFT)) {
189184
label = "pointing";
190185
} else {
191186
logger(LOG_LEVEL_DEBUG, "%s [%u]: Unsupported input device: %s.\n",
192187
__FUNCTION__, __LINE__,
193188
path);
194-
195189
return UIOHOOK_FAILURE;
196190
}
197191

198-
logger(LOG_LEVEL_INFO, "%s [%u]: Found %s device: %s.\n",
192+
logger(LOG_LEVEL_DEBUG, "%s [%u]: Found %s device: %s.\n",
199193
__FUNCTION__, __LINE__,
200194
label, path);
201195

202196
return UIOHOOK_SUCCESS;
203197
}
204198

205-
static void destroy_hook_info(struct hook_info *info) {
206-
if (info != NULL) {
207-
if (info->fd >= 0) {
208-
close(info->fd);
209-
info->fd = -1;
199+
static void destroy_hook_info(struct hook_info **info) {
200+
if (*info != NULL) {
201+
struct hook_info *hook = *info;
202+
203+
if (hook->fd >= 0) {
204+
close(hook->fd);
205+
hook->fd = -1;
210206
}
211207

212-
if (info->evdev != NULL) {
213-
libevdev_free(info->evdev);
214-
info->evdev = NULL;
208+
if (hook->evdev != NULL) {
209+
libevdev_free(hook->evdev);
210+
hook->evdev = NULL;
215211
}
216212

217-
if (info->uinput) {
218-
libevdev_uinput_destroy(info->uinput);
219-
info->uinput = NULL;
213+
if (hook->uinput) {
214+
libevdev_uinput_destroy(hook->uinput);
215+
hook->uinput = NULL;
220216
}
221217

222-
free(info);
218+
free(hook);
219+
*info = NULL;
223220
}
224221
}
225222

226223
/**********************************************************************************************************************/
227224
#define EVENT_GLOB_PATTERN "/dev/input/event*"
228-
static int create_glob(glob_t *glob_buffer) {
225+
static int create_glob_buffer(glob_t *glob_buffer) {
229226
int status = glob(EVENT_GLOB_PATTERN, GLOB_ERR | GLOB_NOSORT | GLOB_NOESCAPE, NULL, glob_buffer);
230227
switch (status) {
231228
case GLOB_NOSPACE:
@@ -234,7 +231,7 @@ static int create_glob(glob_t *glob_buffer) {
234231
return UIOHOOK_ERROR_OUT_OF_MEMORY;
235232

236233
default:
237-
logger(LOG_LEVEL_ERROR, "%s [%u]: Failed response for glob! (%d)\n",
234+
logger(LOG_LEVEL_ERROR, "%s [%u]: Failed to call glob()! (%d)\n",
238235
__FUNCTION__, __LINE__,
239236
status);
240237
return UIOHOOK_FAILURE;
@@ -252,15 +249,16 @@ static void destroy_glob(glob_t *glob_buffer) {
252249
/**********************************************************************************************************************/
253250

254251

255-
static int create_input_devices(int epoll_fd, struct epoll_event **devices) {
252+
static int create_event_listeners(int epoll_fd, struct epoll_event **listeners) {
256253
glob_t glob_buffer;
257-
int status = create_glob(&glob_buffer);
254+
int status = create_glob_buffer(&glob_buffer);
258255
if (status != UIOHOOK_SUCCESS) {
256+
destroy_glob(&glob_buffer);
259257
return status;
260258
}
261259

262-
*devices = malloc(sizeof(struct epoll_event) * glob_buffer.gl_pathc);
263-
if (*devices == NULL) {
260+
struct epoll_event *event_buffer = *listeners = malloc(sizeof(struct epoll_event) * glob_buffer.gl_pathc);
261+
if (event_buffer == NULL) {
264262
logger(LOG_LEVEL_ERROR, "%s [%u]: Failed to allocate memory for epoll event devices!\n",
265263
__FUNCTION__, __LINE__);
266264

@@ -270,38 +268,35 @@ static int create_input_devices(int epoll_fd, struct epoll_event **devices) {
270268

271269
int found = 0;
272270
for (int i = 0; i < glob_buffer.gl_pathc; i++) {
273-
fprintf(stderr, "Testing: %s... ", glob_buffer.gl_pathv[i]);
274-
275-
struct hook_info *info = devices[found]->data.ptr;
271+
struct hook_info *info;
276272
if (create_hook_info(glob_buffer.gl_pathv[i], &info) != UIOHOOK_SUCCESS) {
277-
// FIXME LOG Error
278-
fprintf(stderr, "fail\n");
279-
280-
destroy_hook_info(info);
281-
devices[found]->data.ptr = NULL;
273+
destroy_hook_info(&info);
282274
continue;
283275
}
284-
fprintf(stderr, "ok\n");
285276

286-
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, info->fd, devices[found]) < 0) {
287-
// FIXME LOG Error
288-
fprintf(stderr, "Failed to add file descriptor to epoll %d\n", info->fd);
277+
event_buffer[found].events = EPOLLIN;
278+
event_buffer[found].data.ptr = info;
279+
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, info->fd, &event_buffer[found]) < 0) {
280+
logger(LOG_LEVEL_ERROR, "%s [%u]: Failed to add file descriptor to epoll! (%d)\n",
281+
__FUNCTION__, __LINE__);
289282

290-
destroy_hook_info(info);
291-
devices[found]->data.ptr = NULL;
283+
destroy_hook_info(&info);
284+
event_buffer[found].data.ptr = NULL;
292285
continue;
293286
}
294287

295-
devices[found]->events = EPOLLIN;
296288
found++;
297289
}
298290

299291
destroy_glob(&glob_buffer);
300292

301-
struct epoll_event *new_devices = realloc(*devices, sizeof(struct epoll_event *) * found);
302-
if (new_devices != NULL) {
303-
*devices = new_devices;
304-
new_devices = NULL;
293+
*listeners = realloc(event_buffer, sizeof(struct epoll_event *) * found);
294+
if (*listeners == NULL) {
295+
logger(LOG_LEVEL_WARN, "%s [%u]: Failed to realloc event listeners! (%d)\n",
296+
__FUNCTION__, __LINE__);
297+
298+
*listeners = event_buffer;
299+
event_buffer = NULL;
305300
}
306301

307302
return UIOHOOK_SUCCESS;
@@ -312,18 +307,16 @@ static int create_input_devices(int epoll_fd, struct epoll_event **devices) {
312307
UIOHOOK_API int hook_run() {
313308
int epoll_fd = epoll_create1(0);
314309
if (epoll_fd == -1) {
315-
// FIXME Error
316-
fprintf(stderr, "Failed to create epoll file descriptor\n");
310+
logger(LOG_LEVEL_ERROR, "%s [%u]: Failed to call epoll_create1!\n",
311+
__FUNCTION__, __LINE__);
317312

318313
return UIOHOOK_ERROR_EPOLL_CREATE;
319314
}
320315

321-
struct epoll_event *devices = NULL;
322-
int status = create_input_devices(epoll_fd, &devices);
316+
struct epoll_event *listeners = NULL;
317+
int status = create_event_listeners(epoll_fd, &listeners);
323318
if (status != UIOHOOK_SUCCESS) {
324-
// TODO Log Failure?
325319
close(epoll_fd);
326-
327320
return status;
328321
}
329322

0 commit comments

Comments
 (0)