From 26f7149525f67dc3e323724865e6c823cf215aa7 Mon Sep 17 00:00:00 2001 From: Anjali Kulkarni Date: Fri, 2 Feb 2024 17:08:26 -0800 Subject: [PATCH] Fix route.sh and some misc cleanup Check for fp NULL, review comment by Serge. Signed-off-by: Anjali Kulkarni --- net_arp.c | 35 +++++++++++++++++++---------- net_route.c | 48 +++++++++++++++++++++++----------------- resmem_cg.c | 20 ----------------- tests/ROUTE/route_test.c | 2 ++ 4 files changed, 53 insertions(+), 52 deletions(-) diff --git a/net_arp.c b/net_arp.c index b8e3eb2..01c7037 100644 --- a/net_arp.c +++ b/net_arp.c @@ -173,11 +173,14 @@ static int handle_arp_resp(int net_sock, void **out) struct rtattr *at[NDA_MAX + 1], *rta; struct arp_info *arps = NULL, *iarps = NULL; struct sockaddr_nl nladdr; + unsigned int type, family; #ifdef TESTING - FILE *fp = NULL; - fp = fopen ("./arp_info.txt", "w"); - if (!fp) + FILE *fp; + fp = fopen("./arp_info.txt", "w"); + if (!fp) { + eprintf("Error opening file arp_info.txt with errno: %d", errno); return -1; + } #endif msg.msg_name = &nladdr; @@ -239,8 +242,7 @@ static int handle_arp_resp(int net_sock, void **out) ecount++; free(buf); free(arps); - errno = nlmsg_err->error; - ret = -1; + ret = -(nlmsg_err->error); goto out; } else if (r->nlmsg_type == NLMSG_DONE) { #ifdef PRINTLOGS @@ -265,10 +267,9 @@ static int handle_arp_resp(int net_sock, void **out) len -= NLMSG_LENGTH(sizeof(*nd_msg)); if (len < 0) { eprintf("nlmsg_len incorrect"); - errno = EINVAL; free(buf); free(arps); - ret = -1; + ret = -EINVAL; goto out; } #ifdef PRINTLOGS @@ -276,13 +277,23 @@ static int handle_arp_resp(int net_sock, void **out) #endif rta = ((struct rtattr *) (((char *) (nd_msg)) + NLMSG_ALIGN(sizeof(struct ndmsg)))); parse_attr(at, rta, len); + family = nd_msg->ndm_family; + type = nd_msg->ndm_type; + if (family == AF_INET || family == AF_INET6) { + if ((type == RTN_BROADCAST) || + (type == RTN_MULTICAST) || + (type == RTN_LOCAL)) { + r = NLMSG_NEXT(r, recvl); + continue; + } #ifdef TESTING - get_attr(at, iarps, nd_msg, fp); + get_attr(at, iarps, nd_msg, fp); #else - get_attr(at, iarps, nd_msg); + get_attr(at, iarps, nd_msg); #endif - iarps++; - aind++; + iarps++; + aind++; + } r = NLMSG_NEXT(r, recvl); count++; } @@ -314,7 +325,7 @@ int get_net_arp(void **out) } err = handle_arp_resp(net_sock, out); - if (err == -1) { + if (err < 0) { close(net_sock); return -1; } diff --git a/net_route.c b/net_route.c index 87e5835..9542cfa 100644 --- a/net_route.c +++ b/net_route.c @@ -73,6 +73,7 @@ static int send_route_req(int net_sock) #ifdef TESTING #define IPA_F "%hhu.%hhu.%hhu.%hhu" #define IPA_V(str) str[0], str[1], str[2], str[3] +#define V4_PREFIX_LEN 32 static inline bool is_zero(char *str, int len) { @@ -118,7 +119,8 @@ static void print_rt_info(struct rt_info *rt, FILE *fp) fprintf(fp, "default"); } else { fprintf(fp, IPA_F, IPA_V(rt->dest)); - fprintf(fp, "/%02hhu", rt->dst_prefix_len); + if (rt->dst_prefix_len != V4_PREFIX_LEN) + fprintf(fp, "/%02hhu", rt->dst_prefix_len); } if (!is_zero(rt->gate, MAX_BYTES_IPV4)) { fprintf(fp, " via "); @@ -248,8 +250,16 @@ static int handle_route_resp(int net_sock, void **out) struct rtattr *at[RTA_MAX + 1]; struct rt_info *routes = NULL, *iroutes = NULL; struct sockaddr_nl nladdr; + unsigned int family; + int ret = 0; + #ifdef TESTING - FILE *fp = NULL; + FILE *fp; + fp = fopen("./route_info.txt", "w"); + if (!fp) { + eprintf("Error opening file route_info.txt with errno: %d", errno); + return -1; + } #endif memset(&msg, 0, sizeof(msg)); @@ -309,19 +319,16 @@ static int handle_route_resp(int net_sock, void **out) ecount++; free(buf); free(routes); - errno = nlmsg_err->error; - return -1; + ret = -(nlmsg_err->error); + goto out; } else if (r->nlmsg_type == NLMSG_DONE) { #ifdef PRINTLOGS printf("DONE\n"); #endif free(buf); *(struct rt_info **)out = routes; -#ifdef TESTING - if (fp) - fclose(fp); -#endif - return rtind; + ret = rtind; + goto out; } rt = (struct rtmsg *)NLMSG_DATA(r); len = r->nlmsg_len; @@ -338,31 +345,30 @@ static int handle_route_resp(int net_sock, void **out) len -= NLMSG_LENGTH(sizeof(*rt)); if (len < 0) { eprintf("nlmsg_len incorrect"); - errno = EINVAL; free(buf); free(routes); - return -1; + ret = -EINVAL; + goto out; } #ifdef PRINTLOGS printf("len after sub %lu is %d\n",sizeof(*rt), len); #endif parse_attr(at, RTM_RTA(rt), len); - if (rt->rtm_family == AF_INET || rt->rtm_family == AF_INET6) { + family = rt->rtm_family; + if (family == AF_INET || family == AF_INET6) { if ((rt->rtm_type == RTN_BROADCAST) || - (rt->rtm_type == RTN_MULTICAST)) + (rt->rtm_type == RTN_MULTICAST) || + (rt->rtm_type == RTN_LOCAL)) { + r = NLMSG_NEXT(r, recvl); continue; + } get_attr(at, iroutes, rt); #ifdef TESTING - fp = fopen ("./route_info.txt", "w"); print_rt_info(iroutes, fp); #endif iroutes++; rtind++; - } else { -#ifdef PRINTLOGS - printf("Family is %d\n",rt->rtm_family); -#endif } r = NLMSG_NEXT(r, recvl); count++; @@ -373,11 +379,13 @@ static int handle_route_resp(int net_sock, void **out) #endif } *(struct rt_info **)out = routes; + ret = rtind; +out: #ifdef TESTING if (fp) fclose(fp); #endif - return rtind; + return ret; } int get_net_route(void **out) @@ -394,7 +402,7 @@ int get_net_route(void **out) } err = handle_route_resp(net_sock, out); - if (err == -1) { + if (err < 0) { close(net_sock); return -1; } diff --git a/resmem_cg.c b/resmem_cg.c index da7f37b..d222d29 100644 --- a/resmem_cg.c +++ b/resmem_cg.c @@ -51,21 +51,6 @@ int get_info_infile(char *fname, char *res, void *out) return 0; } -/*static inline void scan_mem_str(str, info, res) -{ - char *loc = NULL; - - loc = strstr(buf, str); - if (loc == NULL) { - eprintf("%s is not found in file %s", str, MEMINFO_FILE); - res->res_unit[i]->status = ENODATA; - } else { - sscanf(loc, "%*s%zu", &info); - (res->res_unit[i]->data).sz = info; - res->res_unit[i]->status = RES_STATUS_FILLED; - } -}*/ - /* read information from a cgroup file. */ static inline size_t cgmemread(char *cg, char *name, char *elem) @@ -182,8 +167,6 @@ int getmeminfo_cg(int res_id, void *out, size_t sz, void **hint, int pid, int fl int populate_meminfo_cg(res_blk_t *res, int pid, int flags) { char *cg; - char buf[MEMBUF_2048]; - int err; cg = get_cgroup(pid, MEMCGNAME); if (!cg) { @@ -193,9 +176,6 @@ int populate_meminfo_cg(res_blk_t *res, int pid, int flags) clean_init(cg); - if ((err = file_to_buf(MEMINFO_FILE, buf, MEMBUF_2048)) < 0) - return err; - for (int i = 0; i < res->res_count; i++) { switch (res->res_unit[i]->res_id) { case RES_MEM_FREE: diff --git a/tests/ROUTE/route_test.c b/tests/ROUTE/route_test.c index 8d59e47..af8d0b4 100644 --- a/tests/ROUTE/route_test.c +++ b/tests/ROUTE/route_test.c @@ -32,12 +32,14 @@ int main(int argc, char **argv) exit(1); } rtn = rt; +#ifdef PRINTLOGS for (int i=0;idst_prefix_len != 0) { printf("%hhu.%hhu.%hhu.%hhu/%02hhu\n",rt->dest[0], rt->dest[1], rt->dest[2], rt->dest[3], rt->dst_prefix_len); } rt++; } +#endif if (rtn) free(rtn); exit(0);