From: Jean Delvare <khali@linux-fr.org>
To: lm-sensors@vger.kernel.org
Subject: Re: [lm-sensors] [PATCH 8/8] sensord: Refactoring of rrdInit()
Date: Fri, 08 May 2009 14:54:06 +0000 [thread overview]
Message-ID: <20090508165406.5482334b@hyperion.delvare> (raw)
In-Reply-To: <20090406075810.GI4734@ubuntu>
On Mon, 6 Apr 2009 09:58:10 +0200, Andre Prendel wrote:
> Refactoring of the rrdInit() function.
>
> * Fix deep indentation levels
> * Return with unique error code (-1)
> ---
>
> rrd.c | 85 ++++++++++++++++++++++++++++++------------------------------------
> 1 file changed, 39 insertions(+), 46 deletions(-)
>
> --- quilt-sensors.orig/prog/sensord/rrd.c 2009-04-04 20:40:28.000000000 +0200
> +++ quilt-sensors/prog/sensord/rrd.c 2009-04-04 23:17:17.000000000 +0200
> @@ -242,58 +242,51 @@
>
> int rrdInit(void)
> {
> - int ret = 0;
> + int ret;
> struct stat tmp;
> + char stepBuff[STEP_BUFF], rraBuff[RRA_BUFF];
> + int argc = 4, num;
> + const char *argv[6 + MAX_RRD_SENSORS] = {
> + "sensord", sensord_args.rrdFile, "-s", stepBuff
> + };
>
> sensorLog(LOG_DEBUG, "sensor RRD init");
> - if (stat(sensord_args.rrdFile, &tmp)) {
> - if (errno = ENOENT) {
> - char stepBuff[STEP_BUFF], rraBuff[RRA_BUFF];
> - int argc = 4, num;
> - const char *argv[6 + MAX_RRD_SENSORS] = {
> - "sensord", sensord_args.rrdFile, "-s", stepBuff
> - };
> -
> - sensorLog(LOG_INFO, "creating round robin database");
> - num = rrdGetSensors(argv + argc);
> - if (num = 0) {
> - sensorLog(LOG_ERR,
> - "Error creating RRD: %s: %s",
> - sensord_args.rrdFile,
> - "No sensors detected");
> - ret = 2;
> - } else if (num < 0) {
> - ret = -num;
> - } else {
> - sprintf(stepBuff, "%d", sensord_args.rrdTime);
> - sprintf(rraBuff, "RRA:%s:%f:%d:%d",
> - sensord_args.rrdNoAverage ? "LAST" :
> - "AVERAGE",
> - 0.5 /* fraction of non-unknown samples needed per entry */,
> - 1 /* samples per entry */,
> - 7 * 24 * 60 * 60 /
> - sensord_args.rrdTime /* 1 week */);
> -
> - argc += num;
> - argv[argc ++] = rraBuff;
> - argv[argc] = NULL;
> - if ((ret = rrd_create(argc,
> - (char **) /* WEAK */ argv))) {
> - sensorLog(LOG_ERR,
> - "Error creating RRD file: %s: %s",
> - sensord_args.rrdFile,
> - rrd_get_error());
> - }
> - }
> - } else {
> - sensorLog(LOG_ERR, "Error stat()ing RRD: %s: %s",
> - sensord_args.rrdFile, strerror(errno));
> - ret = 1;
> +
> + /* Create RRD if it does not exist. */
> + ret = stat(sensord_args.rrdFile, &tmp);
> + if (ret = -1 && errno != ENOENT) {
> + sensorLog(LOG_ERR, "Could not stat rrd file: %s\n",
> + sensord_args.rrdFile);
> + return -1;
> + } else if (errno = ENOENT) {
"else" after a return statement isn't that useful ;)
But more importantly, ret may be != -1 at this point, in which case you
have no guarantee that errno is up-to-date. It could carry an old error
value. The original code did handle this properly... at the price of an
additional level of indentation. Doesn't look unfeasible though:
if (stat(sensord_args.rrdFile, &tmp)) {
if (errno != ENOENT) {
sensorLog(LOG_ERR, "Error stat()ing RRD: %s: %s",
sensord_args.rrdFile, strerror(errno));
return -1;
}
sensorLog(LOG_INFO, "Creating round robin database");
[etc.]
}
> + sensorLog(LOG_INFO, "Creating round robin database");
> +
> + num = rrdGetSensors(argv + argc);
> + if (num < 1) {
> + sensorLog(LOG_ERR, "Error creating RRD: %s: %s",
> + sensord_args.rrdFile, "No sensors detected");
> + return -1;
> + }
> +
> + sprintf(stepBuff, "%d", sensord_args.rrdTime);
> + sprintf(rraBuff, "RRA:%s:%f:%d:%d",
> + sensord_args.rrdNoAverage ? "LAST" :"AVERAGE",
> + 0.5, 1, 7 * 24 * 60 * 60 / sensord_args.rrdTime);
> +
> + argc += num;
> + argv[argc++] = rraBuff;
> + argv[argc] = NULL;
> +
> + ret = rrd_create(argc, (char**) argv);
> + if (ret = -1) {
> + sensorLog(LOG_ERR, "Error creating RRD file: %s: %s",
> + sensord_args.rrdFile, rrd_get_error());
> + return -1;
> }
> }
> - sensorLog(LOG_DEBUG, "sensor RRD inited");
>
> - return ret;
> + sensorLog(LOG_DEBUG, "sensor RRD initialized");
> + return 0;
> }
>
> #define RRDCGI "/usr/bin/rrdcgi"
Other than that, your cleanup look reasonable.
--
Jean Delvare
_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
next prev parent reply other threads:[~2009-05-08 14:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-06 7:58 [lm-sensors] [PATCH 8/8] sensord: Refactoring of rrdInit() Andre Prendel
2009-05-08 14:54 ` Jean Delvare [this message]
2009-05-11 8:32 ` Andre Prendel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090508165406.5482334b@hyperion.delvare \
--to=khali@linux-fr.org \
--cc=lm-sensors@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.