[lxc-devel] [lxd/master] tree-wide: cgo: add -Wunused and fix errors detected by this option

brauner on Github lxc-bot at linuxcontainers.org
Thu Oct 24 10:29:33 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20191024/90d10a7b/attachment-0001.bin>
-------------- next part --------------
From a5b2a35d1f437db8d1f568300dc84d1bec5cd421 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 24 Oct 2019 12:28:34 +0200
Subject: [PATCH] tree-wide: cgo: add -Wunused and fix errors detected by this
 option

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 lxd-p2c/cgo.go                     |  2 +-
 lxd/cgo.go                         |  2 +-
 lxd/include/compiler.h             |  4 ++++
 lxd/main_forkfile.go               |  2 --
 lxd/main_forkmount.go              |  3 +--
 lxd/main_forksyscall.go            | 15 ++-------------
 lxd/seccomp/cgo.go                 |  2 +-
 lxd/seccomp/seccomp.go             | 22 +---------------------
 lxd/storage/cgo.go                 |  2 +-
 lxd/storage/quota/cgo.go           |  2 +-
 lxd/ucred/cgo.go                   |  2 +-
 shared/cgo.go                      |  2 +-
 shared/idmap/cgo.go                |  2 +-
 shared/netutils/cgo.go             |  2 +-
 shared/netutils/netns_getifaddrs.c | 10 ++++++----
 shared/netutils/network.c          |  3 ++-
 shared/termios/cgo.go              |  2 +-
 shared/util_linux_cgo.go           |  5 -----
 18 files changed, 26 insertions(+), 58 deletions(-)

diff --git a/lxd-p2c/cgo.go b/lxd-p2c/cgo.go
index d5ec8ae17d..7a001deac6 100644
--- a/lxd-p2c/cgo.go
+++ b/lxd-p2c/cgo.go
@@ -4,7 +4,7 @@ package main
 
 // #cgo CFLAGS: -std=gnu11 -Wvla -Werror -fvisibility=hidden -Winit-self
 // #cgo CFLAGS: -Wformat=2 -Wshadow -Wendif-labels -fasynchronous-unwind-tables
-// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g
+// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g -Wunused
 // #cgo CFLAGS: -Werror=implicit-function-declaration
 // #cgo CFLAGS: -Werror=return-type -Wendif-labels -Werror=overflow
 // #cgo CFLAGS: -Wnested-externs -fexceptions
diff --git a/lxd/cgo.go b/lxd/cgo.go
index 927fc711f5..b8f79c7a51 100644
--- a/lxd/cgo.go
+++ b/lxd/cgo.go
@@ -4,7 +4,7 @@ package main
 
 // #cgo CFLAGS: -std=gnu11 -Wvla -Werror -fvisibility=hidden -Winit-self
 // #cgo CFLAGS: -Wformat=2 -Wshadow -Wendif-labels -fasynchronous-unwind-tables
-// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g
+// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g -Wunused
 // #cgo CFLAGS: -Werror=implicit-function-declaration
 // #cgo CFLAGS: -Werror=return-type -Wendif-labels -Werror=overflow
 // #cgo CFLAGS: -Wnested-externs -fexceptions
diff --git a/lxd/include/compiler.h b/lxd/include/compiler.h
index fc507ac791..94ef4af1ea 100644
--- a/lxd/include/compiler.h
+++ b/lxd/include/compiler.h
@@ -57,6 +57,10 @@
 #	define __hot __attribute__((hot))
 #endif
 
+#ifndef __unused
+#	define __unused __attribute__((unused))
+#endif
+
 #define __cgfsng_ops
 
 #endif /* __LXC_COMPILER_H */
diff --git a/lxd/main_forkfile.go b/lxd/main_forkfile.go
index 3911fcc2a7..28d547ac88 100644
--- a/lxd/main_forkfile.go
+++ b/lxd/main_forkfile.go
@@ -269,7 +269,6 @@ int manip_file_in_ns(char *rootfs, int pid, char *host, char *container, bool is
 		fprintf(stderr, "type: %s", S_ISDIR(st.st_mode) ? "directory" : "file");
 	}
 
-out:
 	return ret;
 }
 
@@ -287,7 +286,6 @@ void forkdofile(bool is_put, char *rootfs, pid_t pid) {
 
 	char *source = NULL;
 	char *target = NULL;
-	char *writeMode = NULL;
 	char *type = NULL;
 
 	bool append = false;
diff --git a/lxd/main_forkmount.go b/lxd/main_forkmount.go
index 473bd42f35..a7929dd2da 100644
--- a/lxd/main_forkmount.go
+++ b/lxd/main_forkmount.go
@@ -131,7 +131,7 @@ void create(char *src, char *dest)
 
 static void do_lxd_forkmount(pid_t pid)
 {
-	char *src, *dest, *opts, *shiftfs;
+	char *src, *dest, *shiftfs;
 
 	attach_userns(pid);
 
@@ -328,7 +328,6 @@ void forkmount(void)
 	char *cur = NULL;
 
 	char *command = NULL;
-	char *rootfs = NULL;
 	pid_t pid = 0;
 
 	// Get the subcommand
diff --git a/lxd/main_forksyscall.go b/lxd/main_forksyscall.go
index f2599947cd..b0e7903f3f 100644
--- a/lxd/main_forksyscall.go
+++ b/lxd/main_forksyscall.go
@@ -37,17 +37,6 @@ static inline bool same_fsinfo(struct stat *s1, struct stat *s2,
 	return ((sfs1->f_type == sfs2->f_type) && (s1->st_dev == s2->st_dev));
 }
 
-static int fstat_fstatfs(int fd, struct stat *s, struct statfs *sfs)
-{
-	if (fstat(fd, s))
-		return -1;
-
-	if (fstatfs(fd, sfs))
-		return -1;
-
-	return 0;
-}
-
 static bool chdirchroot_in_mntns(int cwd_fd, int root_fd)
 {
 	ssize_t len;
@@ -98,7 +87,7 @@ static bool acquire_basic_creds(pid_t pid)
 static void forkmknod(void)
 {
 	__do_close_prot_errno int target_dir_fd = -EBADF;
-	char *cur = NULL, *target = NULL, *target_dir = NULL, *target_host = NULL;
+	char *target = NULL, *target_dir = NULL;
 	int ret;
 	char path[PATH_MAX];
 	mode_t mode;
@@ -113,7 +102,7 @@ static void forkmknod(void)
 	target = advance_arg(true);
 	mode = atoi(advance_arg(true));
 	dev = atoi(advance_arg(true));
-	target_host = advance_arg(true);
+	advance_arg(true);
 	uid = atoi(advance_arg(true));
 	gid = atoi(advance_arg(true));
 	fsuid = atoi(advance_arg(true));
diff --git a/lxd/seccomp/cgo.go b/lxd/seccomp/cgo.go
index 34332c022e..61e60ae17c 100644
--- a/lxd/seccomp/cgo.go
+++ b/lxd/seccomp/cgo.go
@@ -4,7 +4,7 @@ package seccomp
 
 // #cgo CFLAGS: -std=gnu11 -Wvla -Werror -fvisibility=hidden -Winit-self
 // #cgo CFLAGS: -Wformat=2 -Wshadow -Wendif-labels -fasynchronous-unwind-tables
-// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g
+// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g -Wunused
 // #cgo CFLAGS: -Werror=implicit-function-declaration
 // #cgo CFLAGS: -Werror=return-type -Wendif-labels -Werror=overflow
 // #cgo CFLAGS: -Wnested-externs -fexceptions
diff --git a/lxd/seccomp/seccomp.go b/lxd/seccomp/seccomp.go
index 5b047735d9..afac2022a4 100644
--- a/lxd/seccomp/seccomp.go
+++ b/lxd/seccomp/seccomp.go
@@ -10,7 +10,6 @@ import (
 	"net"
 	"os"
 	"path"
-	"path/filepath"
 	"regexp"
 	"strconv"
 	"strings"
@@ -814,33 +813,14 @@ func TaskIDs(pid int) (int64, int64, int64, int64, error) {
 
 // CallForkmknod executes fork mknod.
 func CallForkmknod(c Instance, dev deviceConfig.Device, requestPID int) int {
-	rootLink := fmt.Sprintf("/proc/%d/root", requestPID)
-	rootPath, err := os.Readlink(rootLink)
-	if err != nil {
-		return int(-C.EPERM)
-	}
-
 	uid, gid, fsuid, fsgid, err := TaskIDs(requestPID)
 	if err != nil {
 		return int(-C.EPERM)
 	}
 
-	if !path.IsAbs(dev["path"]) {
-		cwdLink := fmt.Sprintf("/proc/%d/cwd", requestPID)
-		prefixPath, err := os.Readlink(cwdLink)
-		if err != nil {
-			return int(-C.EPERM)
-		}
-
-		prefixPath = strings.TrimPrefix(prefixPath, rootPath)
-		dev["hostpath"] = filepath.Join(c.RootfsPath(), rootPath, prefixPath, dev["path"])
-	} else {
-		dev["hostpath"] = filepath.Join(c.RootfsPath(), rootPath, dev["path"])
-	}
-
 	_, stderr, err := shared.RunCommandSplit(nil, util.GetExecPath(),
 		"forksyscall", "mknod", dev["pid"], dev["path"],
-		dev["mode_t"], dev["dev_t"], dev["hostpath"],
+		dev["mode_t"], dev["dev_t"],
 		fmt.Sprintf("%d", uid), fmt.Sprintf("%d", gid),
 		fmt.Sprintf("%d", fsuid), fmt.Sprintf("%d", fsgid))
 	if err != nil {
diff --git a/lxd/storage/cgo.go b/lxd/storage/cgo.go
index 436529d3f1..e3d1a41efe 100644
--- a/lxd/storage/cgo.go
+++ b/lxd/storage/cgo.go
@@ -4,7 +4,7 @@ package storage
 
 // #cgo CFLAGS: -std=gnu11 -Wvla -Werror -fvisibility=hidden -Winit-self
 // #cgo CFLAGS: -Wformat=2 -Wshadow -Wendif-labels -fasynchronous-unwind-tables
-// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g
+// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g -Wunused
 // #cgo CFLAGS: -Werror=implicit-function-declaration
 // #cgo CFLAGS: -Werror=return-type -Wendif-labels -Werror=overflow
 // #cgo CFLAGS: -Wnested-externs -fexceptions
diff --git a/lxd/storage/quota/cgo.go b/lxd/storage/quota/cgo.go
index 3309ed3f0b..e8320a76b7 100644
--- a/lxd/storage/quota/cgo.go
+++ b/lxd/storage/quota/cgo.go
@@ -4,7 +4,7 @@ package quota
 
 // #cgo CFLAGS: -std=gnu11 -Wvla -Werror -fvisibility=hidden -Winit-self
 // #cgo CFLAGS: -Wformat=2 -Wshadow -Wendif-labels -fasynchronous-unwind-tables
-// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g
+// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g -Wunused
 // #cgo CFLAGS: -Werror=implicit-function-declaration
 // #cgo CFLAGS: -Werror=return-type -Wendif-labels -Werror=overflow
 // #cgo CFLAGS: -Wnested-externs -fexceptions
diff --git a/lxd/ucred/cgo.go b/lxd/ucred/cgo.go
index 053c84f376..6692b68aa0 100644
--- a/lxd/ucred/cgo.go
+++ b/lxd/ucred/cgo.go
@@ -4,7 +4,7 @@ package ucred
 
 // #cgo CFLAGS: -std=gnu11 -Wvla -Werror -fvisibility=hidden -Winit-self
 // #cgo CFLAGS: -Wformat=2 -Wshadow -Wendif-labels -fasynchronous-unwind-tables
-// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g
+// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g -Wunused
 // #cgo CFLAGS: -Werror=implicit-function-declaration
 // #cgo CFLAGS: -Werror=return-type -Wendif-labels -Werror=overflow
 // #cgo CFLAGS: -Wnested-externs -fexceptions
diff --git a/shared/cgo.go b/shared/cgo.go
index ba73f106ba..cc79857c41 100644
--- a/shared/cgo.go
+++ b/shared/cgo.go
@@ -4,7 +4,7 @@ package shared
 
 // #cgo CFLAGS: -std=gnu11 -Wvla -Werror -fvisibility=hidden -Winit-self
 // #cgo CFLAGS: -Wformat=2 -Wshadow -Wendif-labels -fasynchronous-unwind-tables
-// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g
+// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g -Wunused
 // #cgo CFLAGS: -Werror=implicit-function-declaration
 // #cgo CFLAGS: -Werror=return-type -Wendif-labels -Werror=overflow
 // #cgo CFLAGS: -Wnested-externs -fexceptions
diff --git a/shared/idmap/cgo.go b/shared/idmap/cgo.go
index c732855ccf..4ca7a8320f 100644
--- a/shared/idmap/cgo.go
+++ b/shared/idmap/cgo.go
@@ -4,7 +4,7 @@ package idmap
 
 // #cgo CFLAGS: -std=gnu11 -Wvla -Werror -fvisibility=hidden -Winit-self
 // #cgo CFLAGS: -Wformat=2 -Wshadow -Wendif-labels -fasynchronous-unwind-tables
-// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g
+// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g -Wunused
 // #cgo CFLAGS: -Werror=implicit-function-declaration
 // #cgo CFLAGS: -Werror=return-type -Wendif-labels -Werror=overflow
 // #cgo CFLAGS: -Wnested-externs -fexceptions
diff --git a/shared/netutils/cgo.go b/shared/netutils/cgo.go
index b13622af9c..60bc19f70b 100644
--- a/shared/netutils/cgo.go
+++ b/shared/netutils/cgo.go
@@ -4,7 +4,7 @@ package netutils
 
 // #cgo CFLAGS: -std=gnu11 -Wvla -Werror -fvisibility=hidden -Winit-self
 // #cgo CFLAGS: -Wformat=2 -Wshadow -Wendif-labels -fasynchronous-unwind-tables
-// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g
+// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g -Wunused
 // #cgo CFLAGS: -Werror=implicit-function-declaration
 // #cgo CFLAGS: -Werror=return-type -Wendif-labels -Werror=overflow
 // #cgo CFLAGS: -Wnested-externs -fexceptions
diff --git a/shared/netutils/netns_getifaddrs.c b/shared/netutils/netns_getifaddrs.c
index 8db051a4d3..7ba1326182 100644
--- a/shared/netutils/netns_getifaddrs.c
+++ b/shared/netutils/netns_getifaddrs.c
@@ -21,6 +21,7 @@
 #include <sys/socket.h>
 #include <unistd.h>
 
+#include "../../lxd/include/compiler.h"
 #include "network.c"
 
 struct netns_ifaddrs {
@@ -460,8 +461,8 @@ static int __rtnl_enumerate(int link_af, int addr_af, __s32 netns_id,
 	return r;
 }
 
-static int netns_getifaddrs(struct netns_ifaddrs **ifap, __s32 netns_id,
-			    bool *netnsid_aware)
+__unused static int netns_getifaddrs(struct netns_ifaddrs **ifap,
+				     __s32 netns_id, bool *netnsid_aware)
 {
 	int r, saved_errno;
 	struct ifaddrs_ctx _ctx;
@@ -482,7 +483,7 @@ static int netns_getifaddrs(struct netns_ifaddrs **ifap, __s32 netns_id,
 }
 
 // Get a pointer to the address structure from a sockaddr.
-static void *get_addr_ptr(struct sockaddr *sockaddr_ptr)
+__unused static void *get_addr_ptr(struct sockaddr *sockaddr_ptr)
 {
 	if (sockaddr_ptr->sa_family == AF_INET)
 		return &((struct sockaddr_in *)sockaddr_ptr)->sin_addr;
@@ -493,7 +494,8 @@ static void *get_addr_ptr(struct sockaddr *sockaddr_ptr)
 	return NULL;
 }
 
-static char *get_packet_address(struct sockaddr *sockaddr_ptr, char *buf, size_t buflen)
+__unused static char *get_packet_address(struct sockaddr *sockaddr_ptr,
+					 char *buf, size_t buflen)
 {
 	char *slider = buf;
 	unsigned char *m = ((struct sockaddr_ll *)sockaddr_ptr)->sll_addr;
diff --git a/shared/netutils/network.c b/shared/netutils/network.c
index ea2d789473..e4a3011f81 100644
--- a/shared/netutils/network.c
+++ b/shared/netutils/network.c
@@ -21,6 +21,7 @@
 #include <sys/socket.h>
 #include <unistd.h>
 
+#include "../../lxd/include/compiler.h"
 #include "../../lxd/include/macro.h"
 
 #ifndef NETNS_RTA
@@ -250,7 +251,7 @@ static int addattr(struct nlmsghdr *n, size_t maxlen, int type,
 	return 0;
 }
 
-static __s32 netns_get_nsid(__s32 netns_fd)
+__unused static __s32 netns_get_nsid(__s32 netns_fd)
 {
 	int fd, ret;
 	ssize_t len;
diff --git a/shared/termios/cgo.go b/shared/termios/cgo.go
index 1ea94b6520..f7517e32b2 100644
--- a/shared/termios/cgo.go
+++ b/shared/termios/cgo.go
@@ -4,7 +4,7 @@ package termios
 
 // #cgo CFLAGS: -std=gnu11 -Wvla -Werror -fvisibility=hidden -Winit-self
 // #cgo CFLAGS: -Wformat=2 -Wshadow -Wendif-labels -fasynchronous-unwind-tables
-// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g
+// #cgo CFLAGS: -pipe -Wcast-align --param=ssp-buffer-size=4 -g -Wunused
 // #cgo CFLAGS: -Werror=implicit-function-declaration
 // #cgo CFLAGS: -Werror=return-type -Wendif-labels -Werror=overflow
 // #cgo CFLAGS: -Wnested-externs -fexceptions
diff --git a/shared/util_linux_cgo.go b/shared/util_linux_cgo.go
index 7c46c02bca..bcf7e4f1cb 100644
--- a/shared/util_linux_cgo.go
+++ b/shared/util_linux_cgo.go
@@ -42,11 +42,6 @@ import (
 // This is an adaption from https://codereview.appspot.com/4589049, to be
 // included in the stdlib with the stdlib's license.
 
-static int mygetgrgid_r(int gid, struct group *grp,
-	char *buf, size_t buflen, struct group **result) {
-	return getgrgid_r(gid, grp, buf, buflen, result);
-}
-
 void configure_pty(int fd) {
 	struct termios term_settings;
 	struct winsize win;


More information about the lxc-devel mailing list