main: Use new g_auto* to close file descriptors

This commit is contained in:
Bastien Nocera
2021-01-21 15:44:43 +01:00
parent b60d9d3556
commit e3594b879c
6 changed files with 23 additions and 31 deletions

View File

@@ -85,8 +85,9 @@ prepare_output (SensorDevice *sensor_device,
{ {
DrvData *drv_data = (DrvData *) sensor_device->priv; DrvData *drv_data = (DrvData *) sensor_device->priv;
IIOSensorData data; IIOSensorData data;
g_auto(IioFd) fp = -1;
int fp, buf_len = 127; int buf_len = 127;
data.data = g_malloc0(drv_data->buffer_data->scan_size * buf_len); data.data = g_malloc0(drv_data->buffer_data->scan_size * buf_len);
@@ -106,8 +107,6 @@ prepare_output (SensorDevice *sensor_device,
process_scan (data, sensor_device); process_scan (data, sensor_device);
} }
close(fp);
bail: bail:
g_free(data.data); g_free(data.data);
} }

View File

@@ -9,6 +9,7 @@
#include "drivers.h" #include "drivers.h"
#include "iio-buffer-utils.h" #include "iio-buffer-utils.h"
#include "utils.h"
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@@ -68,8 +69,9 @@ prepare_output (SensorDevice *sensor_device,
{ {
DrvData *drv_data = (DrvData *) sensor_device->priv; DrvData *drv_data = (DrvData *) sensor_device->priv;
IIOSensorData data; IIOSensorData data;
g_auto(IioFd) fp = -1;
int fp, buf_len = 127; int buf_len = 127;
data.data = g_malloc (drv_data->buffer_data->scan_size * buf_len); data.data = g_malloc (drv_data->buffer_data->scan_size * buf_len);
@@ -88,8 +90,6 @@ prepare_output (SensorDevice *sensor_device,
process_scan (data, sensor_device); process_scan (data, sensor_device);
} }
close(fp);
bail: bail:
g_free(data.data); g_free(data.data);
} }

View File

@@ -9,6 +9,7 @@
#include "drivers.h" #include "drivers.h"
#include "iio-buffer-utils.h" #include "iio-buffer-utils.h"
#include "utils.h"
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@@ -73,8 +74,9 @@ prepare_output (SensorDevice *sensor_device,
{ {
DrvData *drv_data = (DrvData *) sensor_device->priv; DrvData *drv_data = (DrvData *) sensor_device->priv;
IIOSensorData data; IIOSensorData data;
g_auto(IioFd) fp = 1;
int fp, buf_len = 127; int buf_len = 127;
data.data = g_malloc(drv_data->buffer_data->scan_size * buf_len); data.data = g_malloc(drv_data->buffer_data->scan_size * buf_len);
@@ -93,8 +95,6 @@ prepare_output (SensorDevice *sensor_device,
process_scan (data, sensor_device); process_scan (data, sensor_device);
} }
close(fp);
bail: bail:
g_free(data.data); g_free(data.data);
} }

View File

@@ -8,6 +8,7 @@
#include "drivers.h" #include "drivers.h"
#include "accel-mount-matrix.h" #include "accel-mount-matrix.h"
#include "utils.h"
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@@ -121,7 +122,8 @@ accelerometer_changed (gpointer user_data)
DrvData *drv_data = (DrvData *) sensor_device->priv; DrvData *drv_data = (DrvData *) sensor_device->priv;
struct input_absinfo abs_info; struct input_absinfo abs_info;
int accel_x = 0, accel_y = 0, accel_z = 0; int accel_x = 0, accel_y = 0, accel_z = 0;
int fd, r; g_auto(IioFd) fd = -1;
int r;
AccelReadings readings; AccelReadings readings;
AccelVec3 tmp; AccelVec3 tmp;
@@ -136,8 +138,6 @@ accelerometer_changed (gpointer user_data)
READ_AXIS(ABS_Y, accel_y); READ_AXIS(ABS_Y, accel_y);
READ_AXIS(ABS_Z, accel_z); READ_AXIS(ABS_Z, accel_z);
close (fd);
/* Scale from 1G ~= 256 to a value in m/s² */ /* Scale from 1G ~= 256 to a value in m/s² */
set_accel_scale (&readings.scale, 1.0 / 256 * 9.81); set_accel_scale (&readings.scale, 1.0 / 256 * 9.81);

View File

@@ -18,6 +18,7 @@
#include <gudev/gudev.h> #include <gudev/gudev.h>
#include "uinput.h" #include "uinput.h"
#include "utils.h"
#define ONEG 256 #define ONEG 256
@@ -57,7 +58,7 @@ write_sysfs_string (char *filename,
char *val) char *val)
{ {
int ret = 0; int ret = 0;
FILE *sysfsfp; g_autoptr(FILE) sysfsfp = NULL;
char *temp; char *temp;
temp = g_build_filename (basedir, filename, NULL); temp = g_build_filename (basedir, filename, NULL);
@@ -67,7 +68,6 @@ write_sysfs_string (char *filename,
goto error_free; goto error_free;
} }
fprintf(sysfsfp, "%s", val); fprintf(sysfsfp, "%s", val);
fclose(sysfsfp);
error_free: error_free:
g_free(temp); g_free(temp);

View File

@@ -2,7 +2,7 @@
* Modified from industrialio buffer test code, and Lenovo Yoga (2 Pro) orientation helper * Modified from industrialio buffer test code, and Lenovo Yoga (2 Pro) orientation helper
* Copyright (c) 2008 Jonathan Cameron * Copyright (c) 2008 Jonathan Cameron
* Copyright (c) 2014 Peter F. Patel-Schneider * Copyright (c) 2014 Peter F. Patel-Schneider
* Copyright (c) 2014 Bastien Nocera <hadess@hadess.net> * Copyright (c) 2014, 2021 Bastien Nocera <hadess@hadess.net>
* Copyright (c) 2015 Elad Alfassa <elad@fedoraproject.org> * Copyright (c) 2015 Elad Alfassa <elad@fedoraproject.org>
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@@ -11,6 +11,7 @@
*/ */
#include "iio-buffer-utils.h" #include "iio-buffer-utils.h"
#include "utils.h"
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
@@ -98,7 +99,7 @@ iioutils_get_type (unsigned *is_signed,
char *filename; char *filename;
char signchar, endianchar; char signchar, endianchar;
unsigned padint; unsigned padint;
FILE *sysfsfp; g_autoptr(FILE) sysfsfp = NULL;
builtname = g_strdup_printf ("%s_type", name); builtname = g_strdup_printf ("%s_type", name);
filename = g_build_filename (device_dir, "scan_elements", builtname, NULL); filename = g_build_filename (device_dir, "scan_elements", builtname, NULL);
@@ -127,11 +128,9 @@ iioutils_get_type (unsigned *is_signed,
if (ret < 0 || ret != 5) { if (ret < 0 || ret != 5) {
g_warning ("Failed to pass scan type description for %s", filename); g_warning ("Failed to pass scan type description for %s", filename);
fclose (sysfsfp);
g_free (filename); g_free (filename);
return FALSE; return FALSE;
} }
fclose (sysfsfp);
*be = (endianchar == 'b'); *be = (endianchar == 'b');
*bytes = padint / 8; *bytes = padint / 8;
@@ -156,7 +155,7 @@ iioutils_get_param_float (float *output,
const char *name, const char *name,
const char *generic_name) const char *generic_name)
{ {
FILE *sysfsfp; g_autoptr(FILE) sysfsfp = NULL;
char *builtname, *filename; char *builtname, *filename;
int ret = 0; int ret = 0;
@@ -169,7 +168,6 @@ iioutils_get_param_float (float *output,
sysfsfp = fopen (filename, "r"); sysfsfp = fopen (filename, "r");
if (sysfsfp) { if (sysfsfp) {
ret = fscanf (sysfsfp, "%f", output); ret = fscanf (sysfsfp, "%f", output);
fclose (sysfsfp);
g_free (filename); g_free (filename);
if (ret == 1) if (ret == 1)
return 0; return 0;
@@ -191,7 +189,6 @@ iioutils_get_param_float (float *output,
g_debug ("Failed to read float from %s", filename); g_debug ("Failed to read float from %s", filename);
ret = -EINVAL; ret = -EINVAL;
} }
fclose (sysfsfp);
} else { } else {
ret = -errno; ret = -errno;
if (ret != -ENOENT) if (ret != -ENOENT)
@@ -228,7 +225,6 @@ build_channel_array (const char *device_dir,
int *counter) int *counter)
{ {
GDir *dp; GDir *dp;
FILE *sysfsfp;
int ret; int ret;
const char *name; const char *name;
char *scan_el_dir; char *scan_el_dir;
@@ -251,6 +247,7 @@ build_channel_array (const char *device_dir,
while ((name = g_dir_read_name (dp)) != NULL) { while ((name = g_dir_read_name (dp)) != NULL) {
if (g_str_has_suffix (name, "_en")) { if (g_str_has_suffix (name, "_en")) {
g_autoptr(FILE) sysfsfp = NULL;
char *filename, *index_name; char *filename, *index_name;
iio_channel_info *current; iio_channel_info *current;
@@ -262,13 +259,12 @@ build_channel_array (const char *device_dir,
continue; continue;
} }
if (fscanf (sysfsfp, "%d", &ret) != 1) { if (fscanf (sysfsfp, "%d", &ret) != 1) {
fclose (sysfsfp);
g_debug ("Could not read from scan_elements file '%s'", filename); g_debug ("Could not read from scan_elements file '%s'", filename);
g_free (filename); g_free (filename);
continue; continue;
} }
fclose (sysfsfp);
g_free (filename); g_free (filename);
g_clear_pointer (&sysfsfp, fclose);
current = g_new0 (iio_channel_info, 1); current = g_new0 (iio_channel_info, 1);
@@ -288,7 +284,6 @@ build_channel_array (const char *device_dir,
if (sysfsfp == NULL) if (sysfsfp == NULL)
goto error; goto error;
ret = fscanf (sysfsfp, "%u", &current->index); ret = fscanf (sysfsfp, "%u", &current->index);
fclose (sysfsfp);
g_free (filename); g_free (filename);
if (ret != 1) if (ret != 1)
goto error; goto error;
@@ -361,7 +356,7 @@ _write_sysfs_int (const char *filename,
int val2) int val2)
{ {
int ret = 0; int ret = 0;
FILE *sysfsfp; g_autoptr(FILE) sysfsfp = NULL;
int test; int test;
char *temp; char *temp;
temp = g_build_filename (basedir, filename, NULL); temp = g_build_filename (basedir, filename, NULL);
@@ -375,8 +370,8 @@ _write_sysfs_int (const char *filename,
fprintf(sysfsfp, "%d %d", val, val2); fprintf(sysfsfp, "%d %d", val, val2);
else else
fprintf(sysfsfp, "%d", val); fprintf(sysfsfp, "%d", val);
g_clear_pointer (&sysfsfp, fclose);
fclose(sysfsfp);
if (verify) { if (verify) {
sysfsfp = fopen(temp, "r"); sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
@@ -390,7 +385,6 @@ _write_sysfs_int (const char *filename,
val, temp); val, temp);
ret = -1; ret = -1;
} }
fclose(sysfsfp);
} }
error_free: error_free:
g_free (temp); g_free (temp);
@@ -412,7 +406,7 @@ _write_sysfs_string (const char *filename,
int verify) int verify)
{ {
int ret = 0; int ret = 0;
FILE *sysfsfp; g_autoptr(FILE) sysfsfp = NULL;
char *temp; char *temp;
temp = g_build_filename (basedir, filename, NULL); temp = g_build_filename (basedir, filename, NULL);
@@ -422,7 +416,7 @@ _write_sysfs_string (const char *filename,
goto error_free; goto error_free;
} }
fprintf(sysfsfp, "%s", val); fprintf(sysfsfp, "%s", val);
fclose(sysfsfp); g_clear_pointer (&sysfsfp, fclose);
/* Verify? */ /* Verify? */
if (!verify) if (!verify)
@@ -438,7 +432,6 @@ _write_sysfs_string (const char *filename,
temp, val, basedir, filename); temp, val, basedir, filename);
ret = -1; ret = -1;
} }
fclose(sysfsfp);
error_free: error_free:
g_free(temp); g_free(temp);