udev-rules: use _cleanup_ for fclose

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2016-02-21 10:04:36 -05:00
parent 19a8e656a9
commit fdd21be6f5

View file

@ -33,6 +33,7 @@
#include "conf-files.h" #include "conf-files.h"
#include "escape.h" #include "escape.h"
#include "fd-util.h" #include "fd-util.h"
#include "fs-util.h"
#include "glob-util.h" #include "glob-util.h"
#include "path-util.h" #include "path-util.h"
#include "stat-util.h" #include "stat-util.h"
@ -2039,7 +2040,7 @@ void udev_rules_apply_to_event(struct udev_rules *rules,
break; break;
} }
case TK_M_IMPORT_CMDLINE: { case TK_M_IMPORT_CMDLINE: {
FILE *f; _cleanup_fclose_ FILE *f = NULL;
bool imported = false; bool imported = false;
f = fopen("/proc/cmdline", "re"); f = fopen("/proc/cmdline", "re");
@ -2052,12 +2053,12 @@ void udev_rules_apply_to_event(struct udev_rules *rules,
pos = strstr(cmdline, key); pos = strstr(cmdline, key);
if (pos != NULL) { if (pos != NULL) {
imported = true;
pos += strlen(key); pos += strlen(key);
if (pos[0] == '\0' || isspace(pos[0])) { if (pos[0] == '\0' || isspace(pos[0]))
/* we import simple flags as 'FLAG=1' */ /* we import simple flags as 'FLAG=1' */
udev_device_add_property(event->dev, key, "1"); udev_device_add_property(event->dev, key, "1");
imported = true; else if (pos[0] == '=') {
} else if (pos[0] == '=') {
const char *value; const char *value;
pos++; pos++;
@ -2066,11 +2067,9 @@ void udev_rules_apply_to_event(struct udev_rules *rules,
pos++; pos++;
pos[0] = '\0'; pos[0] = '\0';
udev_device_add_property(event->dev, key, value); udev_device_add_property(event->dev, key, value);
imported = true;
} }
} }
} }
fclose(f);
} }
if (!imported && cur->key.op != OP_NOMATCH) if (!imported && cur->key.op != OP_NOMATCH)
goto nomatch; goto nomatch;
@ -2366,7 +2365,7 @@ void udev_rules_apply_to_event(struct udev_rules *rules,
const char *key_name = rules_str(rules, cur->key.attr_off); const char *key_name = rules_str(rules, cur->key.attr_off);
char attr[UTIL_PATH_SIZE]; char attr[UTIL_PATH_SIZE];
char value[UTIL_NAME_SIZE]; char value[UTIL_NAME_SIZE];
FILE *f; _cleanup_fclose_ FILE *f = NULL;
if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0) if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL); strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
@ -2377,13 +2376,10 @@ void udev_rules_apply_to_event(struct udev_rules *rules,
rules_str(rules, rule->rule.filename_off), rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line); rule->rule.filename_line);
f = fopen(attr, "we"); f = fopen(attr, "we");
if (f != NULL) { if (f == NULL)
if (fprintf(f, "%s", value) <= 0)
log_error_errno(errno, "error writing ATTR{%s}: %m", attr);
fclose(f);
} else {
log_error_errno(errno, "error opening ATTR{%s} for writing: %m", attr); log_error_errno(errno, "error opening ATTR{%s} for writing: %m", attr);
} else if (fprintf(f, "%s", value) <= 0)
log_error_errno(errno, "error writing ATTR{%s}: %m", attr);
break; break;
} }
case TK_A_SYSCTL: { case TK_A_SYSCTL: {
@ -2449,7 +2445,7 @@ int udev_rules_apply_static_dev_perms(struct udev_rules *rules) {
char **t; char **t;
FILE *f = NULL; FILE *f = NULL;
_cleanup_free_ char *path = NULL; _cleanup_free_ char *path = NULL;
int r = 0; int r;
if (rules->tokens == NULL) if (rules->tokens == NULL)
return 0; return 0;
@ -2520,8 +2516,6 @@ int udev_rules_apply_static_dev_perms(struct udev_rules *rules) {
if (r < 0 && errno != EEXIST) if (r < 0 && errno != EEXIST)
return log_error_errno(errno, "failed to create symlink %s -> %s: %m", return log_error_errno(errno, "failed to create symlink %s -> %s: %m",
tag_symlink, device_node); tag_symlink, device_node);
else
r = 0;
} }
} }
@ -2573,12 +2567,11 @@ finish:
fflush(f); fflush(f);
fchmod(fileno(f), 0644); fchmod(fileno(f), 0644);
if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) { if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
r = -errno; unlink_noerrno("/run/udev/static_node-tags");
unlink("/run/udev/static_node-tags"); unlink_noerrno(path);
unlink(path); return -errno;
} }
fclose(f);
} }
return r; return 0;
} }