3232#include <ccan/array_size/array_size.h>
3333#include <ccan/str/str.h>
3434
35+ #include "cleanup.h"
3536#include "fabrics.h"
3637#include "linux.h"
3738#include "ioctl.h"
@@ -445,15 +446,14 @@ static int inet4_pton(const char *src, uint16_t port,
445446static int inet6_pton (nvme_root_t r , const char * src , uint16_t port ,
446447 struct sockaddr_storage * addr )
447448{
448- int ret = - EINVAL ;
449449 struct sockaddr_in6 * addr6 = (struct sockaddr_in6 * )addr ;
450450 const char * scope = NULL ;
451451 char * p ;
452452
453453 if (strlen (src ) > INET6_ADDRSTRLEN )
454454 return - EINVAL ;
455455
456- char * tmp = strdup (src );
456+ _cleanup_free_ char * tmp = strdup (src );
457457 if (!tmp ) {
458458 nvme_msg (r , LOG_ERR , "cannot copy: %s\n" , src );
459459 return - ENOMEM ;
@@ -466,24 +466,20 @@ static int inet6_pton(nvme_root_t r, const char *src, uint16_t port,
466466 }
467467
468468 if (inet_pton (AF_INET6 , tmp , & addr6 -> sin6_addr ) != 1 )
469- goto free_tmp ;
469+ return - EINVAL ;
470470
471471 if (IN6_IS_ADDR_LINKLOCAL (& addr6 -> sin6_addr ) && scope ) {
472472 addr6 -> sin6_scope_id = if_nametoindex (scope );
473473 if (addr6 -> sin6_scope_id == 0 ) {
474474 nvme_msg (r , LOG_ERR ,
475475 "can't find iface index for: %s (%m)\n" , scope );
476- goto free_tmp ;
476+ return - EINVAL ;
477477 }
478478 }
479479
480480 addr6 -> sin6_family = AF_INET6 ;
481481 addr6 -> sin6_port = htons (port );
482- ret = 0 ;
483-
484- free_tmp :
485- free (tmp );
486- return ret ;
482+ return 0 ;
487483}
488484
489485/**
@@ -658,7 +654,7 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
658654static int __nvmf_supported_options (nvme_root_t r )
659655{
660656 char buf [0x1000 ], * options , * p , * v ;
661- int fd , ret ;
657+ _cleanup_fd_ int fd = -1 ;
662658 ssize_t len ;
663659
664660 if (r -> options )
@@ -687,14 +683,12 @@ static int __nvmf_supported_options(nvme_root_t r)
687683 "Cannot read %s, using default options\n" ,
688684 nvmf_dev );
689685 * r -> options = default_supported_options ;
690- ret = 0 ;
691- goto out_close ;
686+ return 0 ;
692687 }
693688
694689 nvme_msg (r , LOG_ERR , "Failed to read from %s: %s\n" ,
695690 nvmf_dev , strerror (errno ));
696- ret = - ENVME_CONNECT_READ ;
697- goto out_close ;
691+ return - ENVME_CONNECT_READ ;
698692 }
699693
700694 buf [len ] = '\0' ;
@@ -741,16 +735,13 @@ static int __nvmf_supported_options(nvme_root_t r)
741735 parse_option (r , v , trsvcid );
742736 }
743737 nvme_msg (r , LOG_DEBUG , "\n" );
744- ret = 0 ;
745-
746- out_close :
747- close (fd );
748- return ret ;
738+ return 0 ;
749739}
750740
751741static int __nvmf_add_ctrl (nvme_root_t r , const char * argstr )
752742{
753- int ret , fd , len = strlen (argstr );
743+ _cleanup_fd_ int fd ;
744+ int ret , len = strlen (argstr );
754745 char buf [0x1000 ], * options , * p ;
755746
756747 fd = open (nvmf_dev , O_RDWR );
@@ -768,40 +759,30 @@ static int __nvmf_add_ctrl(nvme_root_t r, const char *argstr)
768759 nvmf_dev , strerror (errno ));
769760 switch (errno ) {
770761 case EALREADY :
771- ret = - ENVME_CONNECT_ALREADY ;
772- break ;
762+ return - ENVME_CONNECT_ALREADY ;
773763 case EINVAL :
774- ret = - ENVME_CONNECT_INVAL ;
775- break ;
764+ return - ENVME_CONNECT_INVAL ;
776765 case EADDRINUSE :
777- ret = - ENVME_CONNECT_ADDRINUSE ;
778- break ;
766+ return - ENVME_CONNECT_ADDRINUSE ;
779767 case ENODEV :
780- ret = - ENVME_CONNECT_NODEV ;
781- break ;
768+ return - ENVME_CONNECT_NODEV ;
782769 case EOPNOTSUPP :
783- ret = - ENVME_CONNECT_OPNOTSUPP ;
784- break ;
770+ return - ENVME_CONNECT_OPNOTSUPP ;
785771 case ECONNREFUSED :
786- ret = - ENVME_CONNECT_CONNREFUSED ;
787- break ;
772+ return - ENVME_CONNECT_CONNREFUSED ;
788773 case EADDRNOTAVAIL :
789- ret = - ENVME_CONNECT_ADDRNOTAVAIL ;
790- break ;
774+ return - ENVME_CONNECT_ADDRNOTAVAIL ;
791775 default :
792- ret = - ENVME_CONNECT_WRITE ;
793- break ;
776+ return - ENVME_CONNECT_WRITE ;
794777 }
795- goto out_close ;
796778 }
797779
798780 memset (buf , 0x0 , sizeof (buf ));
799781 len = read (fd , buf , sizeof (buf ) - 1 );
800782 if (len < 0 ) {
801783 nvme_msg (r , LOG_ERR , "Failed to read from %s: %s\n" ,
802784 nvmf_dev , strerror (errno ));
803- ret = - ENVME_CONNECT_READ ;
804- goto out_close ;
785+ return - ENVME_CONNECT_READ ;
805786 }
806787 nvme_msg (r , LOG_DEBUG , "connect ctrl, response '%.*s'\n" ,
807788 (int )strcspn (buf , "\n" ), buf );
@@ -811,14 +792,11 @@ static int __nvmf_add_ctrl(nvme_root_t r, const char *argstr)
811792 if (!* p )
812793 continue ;
813794 if (sscanf (p , "instance=%d" , & ret ) == 1 )
814- goto out_close ;
795+ return ret ;
815796 }
816797
817798 nvme_msg (r , LOG_ERR , "Failed to parse ctrl info for \"%s\"\n" , argstr );
818- ret = - ENVME_CONNECT_PARSE ;
819- out_close :
820- close (fd );
821- return ret ;
799+ return - ENVME_CONNECT_PARSE ;
822800}
823801
824802static const char * lookup_context (nvme_root_t r , nvme_ctrl_t c )
@@ -848,7 +826,7 @@ int nvmf_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
848826{
849827 nvme_subsystem_t s ;
850828 const char * root_app , * app ;
851- char * argstr ;
829+ _cleanup_free_ char * argstr = NULL ;
852830 int ret ;
853831
854832 /* highest prio have configs from command line */
@@ -923,7 +901,6 @@ int nvmf_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
923901 return ret ;
924902
925903 ret = __nvmf_add_ctrl (h -> r , argstr );
926- free (argstr );
927904 if (ret < 0 ) {
928905 errno = - ret ;
929906 return -1 ;
@@ -1209,15 +1186,14 @@ struct nvmf_discovery_log *nvmf_get_discovery_wargs(struct nvme_get_discovery_ar
12091186static int uuid_from_device_tree (char * system_uuid )
12101187{
12111188 ssize_t len ;
1212- int f ;
1189+ _cleanup_fd_ int f ;
12131190
12141191 f = open (PATH_UUID_IBM , O_RDONLY );
12151192 if (f < 0 )
12161193 return - ENXIO ;
12171194
12181195 memset (system_uuid , 0 , NVME_UUID_LEN_STRING );
12191196 len = read (f , system_uuid , NVME_UUID_LEN_STRING - 1 );
1220- close (f );
12211197 if (len < 0 )
12221198 return - ENXIO ;
12231199
@@ -1254,7 +1230,7 @@ static bool is_dmi_uuid_valid(const char *buf, size_t len)
12541230static int uuid_from_dmi_entries (char * system_uuid )
12551231{
12561232 int f ;
1257- DIR * d ;
1233+ _cleanup_dir_ DIR * d ;
12581234 struct dirent * de ;
12591235 char buf [512 ] = {0 };
12601236
@@ -1305,7 +1281,6 @@ static int uuid_from_dmi_entries(char *system_uuid)
13051281 (uint8_t )buf [8 + 14 ], (uint8_t )buf [8 + 15 ]);
13061282 break ;
13071283 }
1308- closedir (d );
13091284 return strlen (system_uuid ) ? 0 : - ENXIO ;
13101285}
13111286
@@ -1319,10 +1294,9 @@ static int uuid_from_dmi_entries(char *system_uuid)
13191294 */
13201295static int uuid_from_product_uuid (char * system_uuid )
13211296{
1322- FILE * stream ;
1297+ _cleanup_file_ FILE * stream ;
13231298 ssize_t nread ;
1324- int ret ;
1325- char * line = NULL ;
1299+ _cleanup_free_ char * line = NULL ;
13261300 size_t len = 0 ;
13271301
13281302 stream = fopen (PATH_DMI_PROD_UUID , "re" );
@@ -1331,24 +1305,16 @@ static int uuid_from_product_uuid(char *system_uuid)
13311305 system_uuid [0 ] = '\0' ;
13321306
13331307 nread = getline (& line , & len , stream );
1334- if (nread != NVME_UUID_LEN_STRING ) {
1335- ret = - ENXIO ;
1336- goto out ;
1337- }
1308+ if (nread != NVME_UUID_LEN_STRING )
1309+ return - ENXIO ;
13381310
13391311 /* The kernel is handling the byte swapping according DMTF
13401312 * SMBIOS 3.0 Section 7.2.1 System UUID */
13411313
13421314 memcpy (system_uuid , line , NVME_UUID_LEN_STRING - 1 );
13431315 system_uuid [NVME_UUID_LEN_STRING - 1 ] = '\0' ;
13441316
1345- ret = 0 ;
1346-
1347- out :
1348- free (line );
1349- fclose (stream );
1350-
1351- return ret ;
1317+ return 0 ;
13521318}
13531319
13541320/**
@@ -1398,15 +1364,15 @@ char *nvmf_hostnqn_generate()
13981364static char * nvmf_read_file (const char * f , int len )
13991365{
14001366 char buf [len ];
1401- int ret , fd ;
1367+ _cleanup_fd_ int fd ;
1368+ int ret ;
14021369
14031370 fd = open (f , O_RDONLY );
14041371 if (fd < 0 )
14051372 return NULL ;
14061373
14071374 memset (buf , 0 , len );
14081375 ret = read (fd , buf , len - 1 );
1409- close (fd );
14101376
14111377 if (ret < 0 || !strlen (buf ))
14121378 return NULL ;
@@ -1530,7 +1496,7 @@ static int nvmf_dim(nvme_ctrl_t c, enum nvmf_dim_tas tas, __u8 trtype,
15301496 __u32 * result )
15311497{
15321498 nvme_root_t r = c -> s && c -> s -> h ? c -> s -> h -> r : NULL ;
1533- struct nvmf_dim_data * dim ;
1499+ _cleanup_free_ struct nvmf_dim_data * dim = NULL ;
15341500 struct nvmf_ext_die * die ;
15351501 __u32 tdl ;
15361502 __u32 tel ;
@@ -1617,11 +1583,7 @@ static int nvmf_dim(nvme_ctrl_t c, enum nvmf_dim_tas tas, __u8 trtype,
16171583
16181584 args .data_len = tdl ;
16191585 args .data = dim ;
1620- ret = nvme_dim_send (& args );
1621-
1622- free (dim );
1623-
1624- return ret ;
1586+ return nvme_dim_send (& args );
16251587}
16261588
16271589/**
@@ -1675,7 +1637,7 @@ static const char *dctype_str[] = {
16751637 */
16761638static int nvme_fetch_cntrltype_dctype_from_id (nvme_ctrl_t c )
16771639{
1678- struct nvme_id_ctrl * id ;
1640+ _cleanup_free_ struct nvme_id_ctrl * id ;
16791641 int ret ;
16801642
16811643 id = __nvme_alloc (sizeof (* id ));
@@ -1685,10 +1647,8 @@ static int nvme_fetch_cntrltype_dctype_from_id(nvme_ctrl_t c)
16851647 }
16861648
16871649 ret = nvme_ctrl_identify (c , id );
1688- if (ret ) {
1689- free (id );
1650+ if (ret )
16901651 return ret ;
1691- }
16921652
16931653 if (!c -> cntrltype ) {
16941654 if (id -> cntrltype > NVME_CTRL_CNTRLTYPE_ADMIN || !cntrltype_str [id -> cntrltype ])
@@ -1703,7 +1663,6 @@ static int nvme_fetch_cntrltype_dctype_from_id(nvme_ctrl_t c)
17031663 else
17041664 c -> dctype = strdup (dctype_str [id -> dctype ]);
17051665 }
1706- free (id );
17071666 return 0 ;
17081667}
17091668
0 commit comments