libgfs2: Remove 'die' calls from __get_sysfs
PATCH 1/1 libgfs2: Remove 'die' calls from __get_sysfs
This patch removes the calls to die from __get_sysfs and adds error handling to it and its callers. To enable this, the prototype of get_sysfs_uint had to be changed so that error status can be returned. Callers to get_sysfs and get_sysfs_uint are also updated to handle error cases where appropriate.
--- gfs2/libgfs2/libgfs2.h | 2 +- gfs2/libgfs2/misc.c | 39 ++++++++++++++++++++++++++++++--------- gfs2/tool/df.c | 42 +++++++++++++++++++++++++++++------------- gfs2/tool/tune.c | 9 +++++++-- 4 files changed, 67 insertions(+), 25 deletions(-)
gfs2/libgfs2/libgfs2.h +1 -1
@@ -634,7 +634,7 @@ extern char *find_debugfs_mount(void);extern char *mp2fsname (char *mp);extern char *mp2fsname2 (char *devname);extern char *get_sysfs (char *fsname,char *filename); -extern unsigned int get_sysfs_uint (char *fsname,char *filename); +extern int get_sysfs_uint (char *fsname,char *filename,unsigned int *val);extern int set_sysfs (char *fsname,char *filename,char *val);extern int is_fsname (char *name);
gfs2/libgfs2/misc.c +30 -9
@@ -216,10 +216,10 @@ static char *__get_sysfs(char *fsname, char *filename) fd =open (path,O_RDONLY );if (fd <0 ) -die ("can't open %s: %s\n" , path,strerror (errno)); +return NULL ; rv =read (fd, sysfs_buf,PAGE_SIZE );if (rv <0 ) -die ("can't read from %s: %s\n" , path,strerror (errno)); +return NULL ;close (fd);return sysfs_buf;
@@ -227,17 +227,30 @@ static char *__get_sysfs(char *fsname, char *filename)char *get_sysfs (char *fsname,char *filename) { -char *p =strchr (__get_sysfs (fsname, filename),'\n' ); +char *s; +char *p; + + s =__get_sysfs (fsname, filename); +if (!s) +return NULL ; + p =strchr (s,'\n' );if (p) *p ='\0' ;return sysfs_buf; } -unsigned int get_sysfs_uint (char *fsname,char *filename) +int get_sysfs_uint (char *fsname,char *filename,unsigned int *val) { -unsigned int x; -sscanf (__get_sysfs (fsname, filename),"%u" , &x); -return x; +char *s =__get_sysfs (fsname, filename); +int ret; +if (!s) +return -1 ; + ret =sscanf (s,"%u" , val); +if (1 != ret) { + errno =ENOMSG ; +return -1 ; + } +return 0 ; }int set_sysfs (char *fsname,char *filename,char *val)
@@ -313,6 +326,7 @@ mp2fsname2(char *mp)char buffer[PATH_MAX ], device_name[PATH_MAX ];int fsdump, fspass, ret, found =0 ;char fspath[PATH_MAX ], fsoptions[PATH_MAX ], fstype[80 ]; +char *id;/* Take care of trailing '/' */ if (mp[strlen (mp) -1 ] =='/' )
@@ -361,7 +375,10 @@ mp2fsname2(char *mp)if (de->d_name[0 ] =='.' )continue ; -if (strcmp (get_sysfs (de->d_name,"id" ), device_id) ==0 ) { + id =get_sysfs (de->d_name,"id" ); +if (!id) +continue ; +if (strcmp (id, device_id) ==0 ) { fsname =strdup (de->d_name);break ; }
@@ -399,6 +416,7 @@ char *mp2fsname(char *mp)struct stat statbuf;DIR *d;struct dirent *de; +char *id;if (stat (mp, &statbuf))return NULL ;
@@ -415,7 +433,10 @@ char *mp2fsname(char *mp)if (de->d_name[0 ] =='.' )continue ; -if (strcmp (get_sysfs (de->d_name,"id" ), device_id) ==0 ) { + id =get_sysfs (de->d_name,"id" ); +if (!id) +continue ; +if (strcmp (id, device_id) ==0 ) { fsname =strdup (de->d_name);break ; }
gfs2/tool/df.c +29 -13
51 lines not on this page · raw message
gfs2/tool/tune.c +7 -2
@@ -39,6 +39,7 @@ get_tune(int argc, char **argv)double ratio;unsigned int num, den;struct gfs2_sbd sbd; +char *value;if (optind == argc)die ("Usage: gfs2_tool gettune <mountpoint>\n" );
@@ -65,8 +66,12 @@ get_tune(int argc, char **argv)continue ;snprintf (path,PATH_MAX -1 ,"tune/%s" , de->d_name);if (strcmp (de->d_name,"quota_scale" ) ==0 ) { -sscanf (get_sysfs (fs,"tune/quota_scale" ),"%u %u" , - &num, &den); +if (!(value =get_sysfs (fs,"tune/quota_scale" ))) { +printf ("quota_scale = (Not found: %s)\n" , +strerror (errno)); +continue ; + } +sscanf (value,"%u %u" , &num, &den); ratio = (double )num / den;printf ("quota_scale = %.4f (%u, %u)\n" , ratio, num, den);
Signature · 1 line
1.6.2
PATCH 1/1 libgfs2: Remove 'die' calls from __get_sysfs
Hi,
Quoted 32 lines · Andrew Price
This patch removes the calls to die from
__get_sysfsand adds error handling to it and its callers. To enable this, the prototype ofget_sysfs_uinthad to be changed so that error status can be returned. Callers toget_sysfsandget_sysfs_uintare also updated to handle error cases where appropriate.Signed-off-by Andrew Price <[email protected]>--- gfs2/libgfs2/libgfs2.h | 2 +- gfs2/libgfs2/misc.c | 39 ++++++++++++++++++++++++++++++--------- gfs2/tool/df.c | 42 +++++++++++++++++++++++++++++------------- gfs2/tool/tune.c | 9 +++++++-- 4 files changed, 67 insertions(+), 25 deletions(-)gfs2/libgfs2/libgfs2.h +1
-1@@ -634,7 +634,7 @@ extern char *find_debugfs_mount(void);extern char *mp2fsname (char *mp);extern char *mp2fsname2 (char *devname);extern char *get_sysfs (char *fsname,char *filename); -extern unsigned int get_sysfs_uint (char *fsname,char *filename); +extern int get_sysfs_uint (char *fsname,char *filename,unsigned int *val);extern int set_sysfs (char *fsname,char *filename,char *val);extern int is_fsname (char *name);gfs2/libgfs2/misc.c +0
-0
gfs2/libgfs2/misc.c static char *__get_sysfs(char *fsname, char *filename) · line 222
Quoted 9 lines
gfs2/libgfs2/misc.c +2
-2@@ -216,10 +216,10 @@ static char *__get_sysfs(char *fsname, char *filename) fd =open (path,O_RDONLY );if (fd <0 ) -die ("can't open %s: %s\n" , path,strerror (errno)); +return NULL ; rv =read (fd, sysfs_buf,PAGE_SIZE );if (rv <0 ) -die ("can't read from %s: %s\n" , path,strerror (errno)); +return NULL ;
Doesn't this leave the file still open?
Quoted 23 lines
gfs2/libgfs2/misc.c +8
-2close (fd);return sysfs_buf;@@ -227,17 +227,30 @@ static char *__get_sysfs(char *fsname, char *filename)char *get_sysfs (char *fsname,char *filename) { -char *p =strchr (__get_sysfs (fsname, filename),'\n' ); +char *s; +char *p; + + s =__get_sysfs (fsname, filename); +if (!s) +return NULL ; + p =strchr (s,'\n' );if (p) *p ='\0' ;return sysfs_buf; } -unsigned int get_sysfs_uint (char *fsname,char *filename) +int get_sysfs_uint (char *fsname,char *filename,unsigned int *val) {
gfs2/libgfs2/misc.c static char *__get_sysfs(char *fsname, char *filename) · line 250
Quoted 10 lines
gfs2/libgfs2/misc.c +7
-3-unsigned int x; -sscanf (__get_sysfs (fsname, filename),"%u" , &x); -return x; +char *s =__get_sysfs (fsname, filename); +int ret; +if (!s) +return -1 ; + ret =sscanf (s,"%u" , val); +if (1 != ret) { + errno =ENOMSG ;
Hmm, a bit odd, but I guess its distinctive ... :-)
Quoted 51 lines
gfs2/libgfs2/misc.c +13
-2+return -1 ; + } +return 0 ; }int set_sysfs (char *fsname,char *filename,char *val)@@ -313,6 +326,7 @@ mp2fsname2(char *mp)char buffer[PATH_MAX ], device_name[PATH_MAX ];int fsdump, fspass, ret, found =0 ;char fspath[PATH_MAX ], fsoptions[PATH_MAX ], fstype[80 ]; +char *id;/* Take care of trailing '/' */ if (mp[strlen (mp) -1 ] =='/' )@@ -361,7 +375,10 @@ mp2fsname2(char *mp)if (de->d_name[0 ] =='.' )continue ; -if (strcmp (get_sysfs (de->d_name,"id" ), device_id) ==0 ) { + id =get_sysfs (de->d_name,"id" ); +if (!id) +continue ; +if (strcmp (id, device_id) ==0 ) { fsname =strdup (de->d_name);break ; }@@ -399,6 +416,7 @@ char *mp2fsname(char *mp)struct stat statbuf;DIR *d;struct dirent *de; +char *id;if (stat (mp, &statbuf))return NULL ;@@ -415,7 +433,10 @@ char *mp2fsname(char *mp)if (de->d_name[0 ] =='.' )continue ; -if (strcmp (get_sysfs (de->d_name,"id" ), device_id) ==0 ) { + id =get_sysfs (de->d_name,"id" ); +if (!id) +continue ; +if (strcmp (id, device_id) ==0 ) { fsname =strdup (de->d_name);break ; }gfs2/tool/df.c +0
-0
gfs2/tool/df.c do_df_one(char *path) · line 150
Quoted 10 lines
gfs2/tool/df.c +1
-6@@ -147,22 +147,38 @@ do_df_one(char *path)printf (" Block size = %u\n" , sbd.sd_sb.sb_bsize);printf (" Journals = %u\n" , journals);printf (" Resource Groups = %" PRIu64"\n" , rgrps); -printf (" Mounted lock proto = \"%s\"\n" , - ((value =get_sysfs (fs,"args/lockproto" ))[0 ]) - ? value : sbd.sd_sb.sb_lockproto); -printf (" Mounted lock table = \"%s\"\n" , - ((value =get_sysfs (fs,"args/locktable" ))[0 ]) - ? value : sbd.sd_sb.sb_locktable); +if ((value =get_sysfs (fs,"args/lockproto" )))
Its better style to do:
value = get_sysfs(....); if (value) .....
Quoted 58 lines
gfs2/tool/df.c +28
-7+printf (" Mounted lock proto = \"%s\"\n" , + value[0 ] ? value : sbd.sd_sb.sb_lockproto); + else + printf(" Mounted lock proto = (Not found: %s)\n", + strerror(errno)); + +if ((value =get_sysfs (fs,"args/locktable" ))) +printf (" Mounted lock table = \"%s\"\n" , + value[0 ] ? value : sbd.sd_sb.sb_locktable); +else +printf (" Mounted lock table = (Not found: %s)\n" , +strerror (errno)); +printf (" Mounted host data = \"%s\"\n" ,get_sysfs (fs,"args/hostdata" ));printf (" Journal number = %s\n" ,get_sysfs (fs,"lockstruct/jid" )); - flags =get_sysfs_uint (fs,"lockstruct/flags" ); -printf (" Lock module flags = %x" , flags); -printf ("\n" ); -printf (" Local flocks = %s\n" , - (get_sysfs_uint (fs,"args/localflocks" )) ?"TRUE" :"FALSE" ); -printf (" Local caching = %s\n" , - (get_sysfs_uint (fs,"args/localcaching" )) ?"TRUE" :"FALSE" ); + +if (get_sysfs_uint (fs,"lockstruct/flags" , &flags)) +printf (" Lock module flags = (Not found: %s)\n" ,strerror (errno)); +else +printf (" Lock module flags = %x\n" , flags); + +if (get_sysfs_uint (fs,"args/localflocks" , &flags)) +printf (" Lock flocks = (Not found: %s)\n" ,strerror (errno)); +else +printf (" Local flocks = %s\n" , flags ?"TRUE" :"FALSE" ); + +if (get_sysfs_uint (fs,"args/localcaching" , &flags)) +printf (" Lock caching = (Not found: %s)\n" ,strerror (errno)); +else +printf (" Local caching = %s\n" , flags ?"TRUE" :"FALSE" );/* Read the master statfs file */ if (mount_gfs2_meta (&sbd)) {gfs2/tool/tune.c +1
-1@@ -39,6 +39,7 @@ get_tune(int argc, char **argv)double ratio;unsigned int num, den;struct gfs2_sbd sbd; +char *value;if (optind == argc)die ("Usage: gfs2_tool gettune <mountpoint>\n" );@@ -65,8 +66,12 @@ get_tune(int argc, char **argv)continue ;snprintf (path,PATH_MAX -1 ,"tune/%s" , de->d_name);if (strcmp (de->d_name,"quota_scale" ) ==0 ) { - sscanf(get_sysfs(fs, "tune/quota_scale"), "%u %u",
gfs2/tool/tune.c get_tune(int argc, char **argv) · line 77
Quoted 10 lines
gfs2/tool/tune.c +6
-1- &num, &den); +if (!(value =get_sysfs (fs,"tune/quota_scale" ))) { +printf ("quota_scale = (Not found: %s)\n" , +strerror (errno)); +continue ; + } +sscanf (value,"%u %u" , &num, &den); ratio = (double )num / den;printf ("quota_scale = %.4f (%u, %u)\n" , ratio, num, den);
Likewise here as well. Otherwise it looks good though,
Steve.
PATCH v2 1/1 libgfs2: Remove 'die' calls from __get_sysfs
This patch removes the calls to die from __get_sysfs and adds error handling to it and its callers. To enable this, the prototype of get_sysfs_uint had to be changed so that error status can be returned. Callers to get_sysfs and get_sysfs_uint are also updated to handle error cases where appropriate.
---
Thanks for the comments on the previous version, I'm glad one of us is wide awake today :-)
gfs2/libgfs2/libgfs2.h | 2 +- gfs2/libgfs2/misc.c | 43 +++++++++++++++++++++++++++++++++---------- gfs2/tool/df.c | 44 +++++++++++++++++++++++++++++++------------- gfs2/tool/tune.c | 10 ++++++++-- 4 files changed, 73 insertions(+), 26 deletions(-)
gfs2/libgfs2/libgfs2.h +1 -1
@@ -634,7 +634,7 @@ extern char *find_debugfs_mount(void);extern char *mp2fsname (char *mp);extern char *mp2fsname2 (char *devname);extern char *get_sysfs (char *fsname,char *filename); -extern unsigned int get_sysfs_uint (char *fsname,char *filename); +extern int get_sysfs_uint (char *fsname,char *filename,unsigned int *val);extern int set_sysfs (char *fsname,char *filename,char *val);extern int is_fsname (char *name);
gfs2/libgfs2/misc.c +33 -10
86 lines not on this page · raw message
gfs2/tool/df.c +31 -13
53 lines not on this page · raw message
gfs2/tool/tune.c +8 -2
@@ -39,6 +39,7 @@ get_tune(int argc, char **argv)double ratio;unsigned int num, den;struct gfs2_sbd sbd; +char *value;if (optind == argc)die ("Usage: gfs2_tool gettune <mountpoint>\n" );
@@ -65,8 +66,13 @@ get_tune(int argc, char **argv)continue ;snprintf (path,PATH_MAX -1 ,"tune/%s" , de->d_name);if (strcmp (de->d_name,"quota_scale" ) ==0 ) { -sscanf (get_sysfs (fs,"tune/quota_scale" ),"%u %u" , - &num, &den); + value =get_sysfs (fs,"tune/quota_scale" ); +if (!value) { +printf ("quota_scale = (Not found: %s)\n" , +strerror (errno)); +continue ; + } +sscanf (value,"%u %u" , &num, &den); ratio = (double )num / den;printf ("quota_scale = %.4f (%u, %u)\n" , ratio, num, den);
Signature · 1 line
1.6.2
PATCH v2 1/1 libgfs2: Remove 'die' calls from __get_sysfs
Hi,
Ok, that looks good now,
Steve.
Quoted 212 lines · Andrew Price
This patch removes the calls to die from
__get_sysfsand adds error handling to it and its callers. To enable this, the prototype ofget_sysfs_uinthad to be changed so that error status can be returned. Callers toget_sysfsandget_sysfs_uintare also updated to handle error cases where appropriate.Signed-off-by Andrew Price <[email protected]>---
Thanks for the comments on the previous version, I'm glad one of us is wide awake today :-)gfs2/libgfs2/libgfs2.h | 2 +- gfs2/libgfs2/misc.c | 43 +++++++++++++++++++++++++++++++++---------- gfs2/tool/df.c | 44 +++++++++++++++++++++++++++++++------------- gfs2/tool/tune.c | 10 ++++++++-- 4 files changed, 73 insertions(+), 26 deletions(-)gfs2/libgfs2/libgfs2.h +1
-1@@ -634,7 +634,7 @@ extern char *find_debugfs_mount(void);extern char *mp2fsname (char *mp);extern char *mp2fsname2 (char *devname);extern char *get_sysfs (char *fsname,char *filename); -extern unsigned int get_sysfs_uint (char *fsname,char *filename); +extern int get_sysfs_uint (char *fsname,char *filename,unsigned int *val);extern int set_sysfs (char *fsname,char *filename,char *val);extern int is_fsname (char *name);gfs2/libgfs2/misc.c +33
-10@@ -216,10 +216,12 @@ static char *__get_sysfs(char *fsname, char *filename) fd =open (path,O_RDONLY );if (fd <0 ) -die ("can't open %s: %s\n" , path,strerror (errno)); +return NULL ; rv =read (fd, sysfs_buf,PAGE_SIZE ); -if (rv <0 ) -die ("can't read from %s: %s\n" , path,strerror (errno)); +if (rv <0 ) { +close (fd); +return NULL ; + }close (fd);return sysfs_buf;@@ -227,17 +229,30 @@ static char *__get_sysfs(char *fsname, char *filename)char *get_sysfs (char *fsname,char *filename) { -char *p =strchr (__get_sysfs (fsname, filename),'\n' ); +char *s; +char *p; + + s =__get_sysfs (fsname, filename); +if (!s) +return NULL ; + p =strchr (s,'\n' );if (p) *p ='\0' ;return sysfs_buf; } -unsigned int get_sysfs_uint (char *fsname,char *filename) +int get_sysfs_uint (char *fsname,char *filename,unsigned int *val) { -unsigned int x; -sscanf (__get_sysfs (fsname, filename),"%u" , &x); -return x; +char *s =__get_sysfs (fsname, filename); +int ret; +if (!s) +return -1 ; + ret =sscanf (s,"%u" , val); +if (1 != ret) { + errno =ENOMSG ; +return -1 ; + } +return 0 ; }int set_sysfs (char *fsname,char *filename,char *val)@@ -313,6 +328,7 @@ mp2fsname2(char *mp)char buffer[PATH_MAX ], device_name[PATH_MAX ];int fsdump, fspass, ret, found =0 ;char fspath[PATH_MAX ], fsoptions[PATH_MAX ], fstype[80 ]; +char *id;/* Take care of trailing '/' */ if (mp[strlen (mp) -1 ] =='/' )@@ -361,7 +377,10 @@ mp2fsname2(char *mp)if (de->d_name[0 ] =='.' )continue ; -if (strcmp (get_sysfs (de->d_name,"id" ), device_id) ==0 ) { + id =get_sysfs (de->d_name,"id" ); +if (!id) +continue ; +if (strcmp (id, device_id) ==0 ) { fsname =strdup (de->d_name);break ; }@@ -399,6 +418,7 @@ char *mp2fsname(char *mp)struct stat statbuf;DIR *d;struct dirent *de; +char *id;if (stat (mp, &statbuf))return NULL ;@@ -415,7 +435,10 @@ char *mp2fsname(char *mp)if (de->d_name[0 ] =='.' )continue ; -if (strcmp (get_sysfs (de->d_name,"id" ), device_id) ==0 ) { + id =get_sysfs (de->d_name,"id" ); +if (!id) +continue ; +if (strcmp (id, device_id) ==0 ) { fsname =strdup (de->d_name);break ; }gfs2/tool/df.c +31
-13@@ -147,22 +147,40 @@ do_df_one(char *path)printf (" Block size = %u\n" , sbd.sd_sb.sb_bsize);printf (" Journals = %u\n" , journals);printf (" Resource Groups = %" PRIu64"\n" , rgrps); -printf (" Mounted lock proto = \"%s\"\n" , - ((value =get_sysfs (fs,"args/lockproto" ))[0 ]) - ? value : sbd.sd_sb.sb_lockproto); -printf (" Mounted lock table = \"%s\"\n" , - ((value =get_sysfs (fs,"args/locktable" ))[0 ]) - ? value : sbd.sd_sb.sb_locktable); + value =get_sysfs (fs,"args/lockproto" ); +if (value) +printf (" Mounted lock proto = \"%s\"\n" , + value[0 ] ? value : sbd.sd_sb.sb_lockproto); +else +printf (" Mounted lock proto = (Not found: %s)\n" , +strerror (errno)); + + value =get_sysfs (fs,"args/locktable" ); +if (value) +printf (" Mounted lock table = \"%s\"\n" , + value[0 ] ? value : sbd.sd_sb.sb_locktable); +else +printf (" Mounted lock table = (Not found: %s)\n" , +strerror (errno)); +printf (" Mounted host data = \"%s\"\n" ,get_sysfs (fs,"args/hostdata" ));printf (" Journal number = %s\n" ,get_sysfs (fs,"lockstruct/jid" )); - flags =get_sysfs_uint (fs,"lockstruct/flags" ); -printf (" Lock module flags = %x" , flags); -printf ("\n" ); -printf (" Local flocks = %s\n" , - (get_sysfs_uint (fs,"args/localflocks" )) ?"TRUE" :"FALSE" ); -printf (" Local caching = %s\n" , - (get_sysfs_uint (fs,"args/localcaching" )) ?"TRUE" :"FALSE" ); + +if (get_sysfs_uint (fs,"lockstruct/flags" , &flags)) +printf (" Lock module flags = (Not found: %s)\n" ,strerror (errno)); +else +printf (" Lock module flags = %x\n" , flags); + +if (get_sysfs_uint (fs,"args/localflocks" , &flags)) +printf (" Lock flocks = (Not found: %s)\n" ,strerror (errno)); +else +printf (" Local flocks = %s\n" , flags ?"TRUE" :"FALSE" ); + +if (get_sysfs_uint (fs,"args/localcaching" , &flags)) +printf (" Lock caching = (Not found: %s)\n" ,strerror (errno)); +else +printf (" Local caching = %s\n" , flags ?"TRUE" :"FALSE" );/* Read the master statfs file */ if (mount_gfs2_meta (&sbd)) {gfs2/tool/tune.c +8
-2@@ -39,6 +39,7 @@ get_tune(int argc, char **argv)double ratio;unsigned int num, den;struct gfs2_sbd sbd; +char *value;if (optind == argc)die ("Usage: gfs2_tool gettune <mountpoint>\n" );@@ -65,8 +66,13 @@ get_tune(int argc, char **argv)continue ;snprintf (path,PATH_MAX -1 ,"tune/%s" , de->d_name);if (strcmp (de->d_name,"quota_scale" ) ==0 ) { -sscanf (get_sysfs (fs,"tune/quota_scale" ),"%u %u" , - &num, &den); + value =get_sysfs (fs,"tune/quota_scale" ); +if (!value) { +printf ("quota_scale = (Not found: %s)\n" , +strerror (errno)); +continue ; + } +sscanf (value,"%u %u" , &num, &den); ratio = (double )num / den;printf ("quota_scale = %.4f (%u, %u)\n" , ratio, num, den);