Hi Balbir,

Balbir Singh wrote:
>> If writing an invalid value on a file of cgroup fs, write(2) returns -1
>> with a right errno. But fprintf(3) does not return it, because fprintf(3)
>> write on some buffer. So it is better that we check the return value of
>> write(2).
>>
> 
> Hi, Kenichi,
> 
> Does the code fragment (diff) below work for you?
> Please note, it is not an appliable diff, it is a part of bigger
> patch.
> 
>  
>  static int cg_set_control_value(char *path, char *val)
>  {
> +     int ret = 0;
>       FILE *control_file = NULL;
>       if (!cg_test_mounted_fs())
>               return ECGROUPNOTMOUNTED;
> @@ -1078,9 +1080,14 @@ static int cg_set_control_value(char *path, char *val)
>               return ECGROUPVALUENOTEXIST;
>       }
>  
> -     fprintf(control_file, "%s", val);
> +     ret = fprintf(control_file, "%s", val);
> +     if (ret < 0 || ret < strlen(val) + 1) {
> +             last_errno = errno;
> +             ret = ECGOTHER;
> +     } else
> +             ret = 0;
>       fclose(control_file);
> -     return 0;
> +     return ret;
>  }

If the following /etc/cgconfig.conf, cgconfig should succeed.
But it failed after applying your patch:

# cat /etc/cgconfig.conf
mount {
        cpuset = /mnt/cgroups/cpuset;
}
group usergroup {
        cpuset {
                cpuset.cpus = 0;
                cpuset.mems = 0;
        }
}

# service cgconfig stop
Stopping cgconfig service:                                 [  OK  ]
# service cgconfig start
Starting cgconfig service: Loading configuration file /etc/cgconfig.conf failed
Success
Failed to parse /etc/cgconfig.conf                         [FAILED]
#


If you want to use fprintf(3), how about the attached patch ?
The error handling against fclose(3) is added into the patch.
fclose(3) write the buffer to a file, and it returns -1 if a writing
error happens.


Thanks
Ken'ichi Ohmichi

----
[PATCH-v2] Add the write error handling to cg_set_control_value().

Changelog since v1:
 o Use fopen/fprintf/fclose instead of open/write/close.
 o Add the error handling against fclose.

cg_set_control_value() is the function for setting a value to a file
of cgroup file system. And current function does not handle the error
of writing to a file. So we cannot know whether setting value is
enable or not. This patch add the error handling for knowing it.
    
Signed-off-by: Ken'ichi Ohmichi <[email protected]>

diff --git a/src/api.c b/src/api.c
index 28c0c3d..0b1bec0 100644
--- a/src/api.c
+++ b/src/api.c
@@ -1078,8 +1078,15 @@ static int cg_set_control_value(char *path, char *val)
                return ECGROUPVALUENOTEXIST;
        }
 
-       fprintf(control_file, "%s", val);
-       fclose(control_file);
+       if (fprintf(control_file, "%s", val) < 0) {
+               last_errno = errno;
+               fclose(control_file);
+               return ECGOTHER;
+       }
+       if (fclose(control_file) < 0) {
+               last_errno = errno;
+               return ECGOTHER;
+       }
        return 0;
 }
 

------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing. 
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to