Re: [PATCH v2 1/2] selftests/resctrl: Fix schemata write error check

From: Maciej Wieczór-Retman
Date: Mon Aug 28 2023 - 08:56:09 EST


On 2023-08-28 at 13:43:05 +0300, Ilpo Järvinen wrote:
>On Mon, 28 Aug 2023, Wieczor-Retman, Maciej wrote:
>> diff --git a/tools/testing/selftests/resctrl/resctrlfs.c b/tools/testing/selftests/resctrl/resctrlfs.c
>> index bd36ee206602..0f9644e5a25e 100644
>> --- a/tools/testing/selftests/resctrl/resctrlfs.c
>> +++ b/tools/testing/selftests/resctrl/resctrlfs.c
>> @@ -488,9 +488,8 @@ int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
>> */
>> int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
>> {
>> - char controlgroup[1024], schema[1024], reason[64];
>> - int resource_id, ret = 0;
>> - FILE *fp;
>> + char controlgroup[1024], schema[1024], reason[128];
>> + int resource_id, fp, ret = 0;
>
>fp -> fd to follow the usual convention.

Okay, I'll change it

>>
>> - fp = fopen(controlgroup, "w");
>> + fp = open(controlgroup, O_WRONLY);
>> if (!fp) {
>> sprintf(reason, "Failed to open control group");
>> ret = -1;
>>
>> goto out;
>> }
>> -
>> - if (fprintf(fp, "%s\n", schema) < 0) {
>> - sprintf(reason, "Failed to write schemata in control group");
>> - fclose(fp);
>> + if (write(fp, schema, strlen(schema)) < 0) {
>
>snprintf() returns you the length, just store the return value and there's
>no need to calculate it here.

Thanks for the suggestion, tested it and it works fine, I'll add it in
the next version.

>> + snprintf(reason, sizeof(reason),
>> + "write() failed : %s", strerror(errno));
>> + close(fp);
>> ret = -1;
>>
>> goto out;
>> }
>> - fclose(fp);
>> + close(fp);
>> + schema[strcspn(schema, "\n")] = 0;
>
>Here too you can use the known length/index instead of strcspr().

Same as above

--
Kind regards
Maciej Wieczór-Retman