Skip to contentSkip to conversation

perf tools: Add error handling

PATCH In review Firo Yang 3 participants 6 messages Jun 9, 2015

Subsystems (2)
lore
Share & tools
lore

Firo Yang

author
#0 lore raw

PATCH v2 1/1 perf tools: Add error handling

Add error handling code for snprintf and rename in check_backup.

---
Since there is no suitable error code snprintf, I just return the value returned by snprintf.

 tools/perf/util/data.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/perf/util/data.c +14 -3
index 1921942..26ab45a 100644
@@ -32,15 +32,26 @@ static bool check_pipe(struct perf_data_file *file)
 
 static int check_backup(struct perf_data_file *file)
 {
+	int ret;
 	struct stat st;
 
 	if (!stat(file->path, &st) && st.st_size) {
-		/* TODO check errors properly */
 		char oldname[PATH_MAX];
-		snprintf(oldname, sizeof(oldname), "%s.old",
+		ret = snprintf(oldname, sizeof(oldname), "%s.old",
 			 file->path);
+		if (ret < 0) {
+			pr_err("failed to make name %s.old\n", file->path);
+			return ret;
+		}
+
 		unlink(oldname);
-		rename(file->path, oldname);
+
+		ret = rename(file->path, oldname);
+		if (ret < 0) {
+			pr_err("failed to rename %s to %s\n", file->path,
+				oldname);
+			return -errno;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.4.2
#1 lore raw
Quoted 18 lines · Firo Yang

Add error handling code for snprintf and rename in check_backup.

---
Since there is no suitable error code snprintf, I just return the value returned by snprintf.

 tools/perf/util/data.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/perf/util/data.c +0 -0
index 1921942..26ab45a 100644
@@ -32,15 +32,26 @@ static bool check_pipe(struct perf_data_file *file)
 
 static int check_backup(struct perf_data_file *file)
 {

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 42

Quoted 10 lines
tools/perf/util/data.c +3 -2
+	int ret;
 	struct stat st;
 
 	if (!stat(file->path, &st) && st.st_size) {
-		/* TODO check errors properly */
 		char oldname[PATH_MAX];
-		snprintf(oldname, sizeof(oldname), "%s.old",
+		ret = snprintf(oldname, sizeof(oldname), "%s.old",
 			 file->path);
+		if (ret < 0) {

Can it ever return a negative value anyway? It seems clear that it can return a positive value. That value might not be an appropriate result for this function.

julia

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 53

Quoted 12 lines
tools/perf/util/data.c +10 -1
+			pr_err("failed to make name %s.old\n", file->path);
+			return ret;
+		}
+
 		unlink(oldname);
-		rename(file->path, oldname);
+
+		ret = rename(file->path, oldname);
+		if (ret < 0) {
+			pr_err("failed to rename %s to %s\n", file->path,
+				oldname);
+			return -errno;

What is

Quoted 12 lines
tools/perf/util/data.c +1 -1
+		}
 	}
 
 	return 0;
--

2.4.2

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#2 lore raw
In reply to Firo Yang
Quoted 23 lines · Julia Lawall
Quoted 21 lines · Firo Yang

Add error handling code for snprintf and rename in check_backup.

---
Since there is no suitable error code snprintf, I just return the value returned by snprintf.

 tools/perf/util/data.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/perf/util/data.c +1 -0
index 1921942..26ab45a 100644
@@ -32,15 +32,26 @@ static bool check_pipe(struct perf_data_file *file)
 
 static int check_backup(struct perf_data_file *file)
 {
+	int ret;
 	struct stat st;
 
Quoted 9 lines

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 42

Quoted 7 lines
tools/perf/util/data.c +2 -2
 	if (!stat(file->path, &st) && st.st_size) {
-		/* TODO check errors properly */
 		char oldname[PATH_MAX];
-		snprintf(oldname, sizeof(oldname), "%s.old",
+		ret = snprintf(oldname, sizeof(oldname), "%s.old",
 			 file->path);
+		if (ret < 0) {

Can it ever return a negative value anyway? It seems clear that it can

I misunderstand the content in man 3 snprintf:
If an output error is encountered, a negative value is returned.

It should be nothing related to snprintf.

Quoted 5 lines

return a positive value. That value might not be an appropriate result for this function.

julia

Quoted 14 lines

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 53

Quoted 12 lines
tools/perf/util/data.c +10 -1
+			pr_err("failed to make name %s.old\n", file->path);
+			return ret;
+		}
+
 		unlink(oldname);
-		rename(file->path, oldname);
+
+		ret = rename(file->path, oldname);
+		if (ret < 0) {
+			pr_err("failed to rename %s to %s\n", file->path,
+				oldname);
+			return -errno;

What is

If rename failed, it would set the errno to the corresponding error number. You can find it in other function in the same file.

Quoted 13 lines
Quoted 13 lines
tools/perf/util/data.c +1 -1
+		}
 	}
 
 	return 0;
--

2.4.2

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
In reply to Firo Yang
Quoted 33 lines · Firo Yang
Quoted 23 lines · Julia Lawall
Quoted 21 lines · Firo Yang

Add error handling code for snprintf and rename in check_backup.

---
Since there is no suitable error code snprintf, I just return the value returned by snprintf.

 tools/perf/util/data.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/perf/util/data.c +1 -0
index 1921942..26ab45a 100644
@@ -32,15 +32,26 @@ static bool check_pipe(struct perf_data_file *file)
 
 static int check_backup(struct perf_data_file *file)
 {
+	int ret;
 	struct stat st;
 
Quoted 9 lines

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 42

Quoted 7 lines
tools/perf/util/data.c +2 -2
 	if (!stat(file->path, &st) && st.st_size) {
-		/* TODO check errors properly */
 		char oldname[PATH_MAX];
-		snprintf(oldname, sizeof(oldname), "%s.old",
+		ret = snprintf(oldname, sizeof(oldname), "%s.old",
 			 file->path);
+		if (ret < 0) {

Can it ever return a negative value anyway? It seems clear that it can

Quoted 2 lines

I misunderstand the content in man 3 snprintf:
If an output error is encountered, a negative value is returned.

It does say that, but I didn't see it in the code. Snprintf just returns the value of vsnprintf. This can return 0 or str-buf. If str-buf can be negative, the value would not seem to be very meaningful. But probably it cannot be.

Quoted 5 lines

It should be nothing related to snprintf.

Quoted 2 lines

return a positive value. That value might not be an appropriate result for this function.

Sorry, this latter comment was nonsense :)

julia

Quoted 38 lines
Quoted 16 lines

julia

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 53

Quoted 12 lines
tools/perf/util/data.c +10 -1
+			pr_err("failed to make name %s.old\n", file->path);
+			return ret;
+		}
+
 		unlink(oldname);
-		rename(file->path, oldname);
+
+		ret = rename(file->path, oldname);
+		if (ret < 0) {
+			pr_err("failed to rename %s to %s\n", file->path,
+				oldname);
+			return -errno;

What is

If rename failed, it would set the errno to the corresponding error number. You can find it in other function in the same file.

Quoted 8 lines
Quoted 8 lines
tools/perf/util/data.c +1 -1
+		}
 	}
 
 	return 0;
--

2.4.2

Quoted 5 lines
Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#4 lore raw
In reply to Firo Yang
Quoted 37 lines · Julia Lawall
Quoted 33 lines · Firo Yang
Quoted 32 lines · Julia Lawall
Quoted 18 lines · Firo Yang

Add error handling code for snprintf and rename in check_backup.

---
Since there is no suitable error code snprintf, I just return the value returned by snprintf.

 tools/perf/util/data.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/perf/util/data.c +0 -0
index 1921942..26ab45a 100644
@@ -32,15 +32,26 @@ static bool check_pipe(struct perf_data_file *file)
 
 static int check_backup(struct perf_data_file *file)
 {

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 42

Quoted 10 lines
tools/perf/util/data.c +3 -2
+	int ret;
 	struct stat st;
 
 	if (!stat(file->path, &st) && st.st_size) {
-		/* TODO check errors properly */
 		char oldname[PATH_MAX];
-		snprintf(oldname, sizeof(oldname), "%s.old",
+		ret = snprintf(oldname, sizeof(oldname), "%s.old",
 			 file->path);
+		if (ret < 0) {

Can it ever return a negative value anyway? It seems clear that it can

Quoted 7 lines
Quoted 2 lines

I misunderstand the content in man 3 snprintf:
If an output error is encountered, a negative value is returned.

It does say that, but I didn't see it in the code. Snprintf just returns the value of vsnprintf. This can return 0 or str-buf. If str-buf can be negative, the value would not seem to be very meaningful. But probably it cannot be.

I got it. Thanks.

Quoted 49 lines
Quoted 6 lines

It should be nothing related to snprintf.

Quoted 2 lines

return a positive value. That value might not be an appropriate result for this function.

Sorry, this latter comment was nonsense :)

julia

Quoted 38 lines
Quoted 16 lines

julia

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 53

Quoted 12 lines
tools/perf/util/data.c +10 -1
+			pr_err("failed to make name %s.old\n", file->path);
+			return ret;
+		}
+
 		unlink(oldname);
-		rename(file->path, oldname);
+
+		ret = rename(file->path, oldname);
+		if (ret < 0) {
+			pr_err("failed to rename %s to %s\n", file->path,
+				oldname);
+			return -errno;

What is

If rename failed, it would set the errno to the corresponding error number. You can find it in other function in the same file.

Quoted 8 lines
Quoted 8 lines
tools/perf/util/data.c +1 -1
+		}
 	}
 
 	return 0;
--

2.4.2

Quoted 5 lines
Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
In reply to Firo Yang

Am 09.06.2015 09:49, schrieb Firo Yang:

Quoted 17 lines

Add error handling code for snprintf and rename in check_backup.

---
Since there is no suitable error code snprintf, I just return the value returned by snprintf.

 tools/perf/util/data.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/perf/util/data.c +0 -0
index 1921942..26ab45a 100644
@@ -32,15 +32,26 @@ static bool check_pipe(struct perf_data_file *file)
 
 static int check_backup(struct perf_data_file *file)

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 41

Quoted 10 lines
tools/perf/util/data.c +2 -2
 {
+	int ret;
 	struct stat st;
 
 	if (!stat(file->path, &st) && st.st_size) {
-		/* TODO check errors properly */
 		char oldname[PATH_MAX];
-		snprintf(oldname, sizeof(oldname), "%s.old",
+		ret = snprintf(oldname, sizeof(oldname), "%s.old",
 			 file->path);

i am a big fan of asprintf() since that allows you to have just the right size. Just do not forget to free the allocated buffer.

Quoted 4 lines
tools/perf/util/data.c +4 -0
+		if (ret < 0) {
+			pr_err("failed to make name %s.old\n", file->path);
+			return ret;
+		}

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 54

Quoted 10 lines
tools/perf/util/data.c +8 -1
+
 		unlink(oldname);
-		rename(file->path, oldname);
+
+		ret = rename(file->path, oldname);
+		if (ret < 0) {
+			pr_err("failed to rename %s to %s\n", file->path,
+				oldname);
+			return -errno;
+		}

short: it does not work this way. rename will return EXDEV if source and destination are on two file systems. (See man 2 rename) perfect recoverable.
(on the other side: can this happen ? i do not know)

just my 2 cents
re,
wh

Quoted 3 lines
tools/perf/util/data.c +0 -0
 	}
 
 	return 0;