Skip to content

Commit 9814e55

Browse files
kcwong-verseonmheon
authored andcommitted
golang: Use seccomp_version API to obtain library version
* add three C functions to retrieve the major, minor and micro version values from `seccomp_version()` when that's available under v2.3.x or newer * has to change type from `int` to `uint` as that's the type used in `struct scmp_version` Signed-off-by: K.C. Wong <[email protected]> [MH: Tweaked commit message] Signed-off-by: Matthew Heon <[email protected]>
1 parent e3496e3 commit 9814e55

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

seccomp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func (a ScmpAction) GetReturnCode() int16 {
324324
// GetLibraryVersion returns the version of the library the bindings are built
325325
// against.
326326
// The version is formatted as follows: Major.Minor.Micro
327-
func GetLibraryVersion() (major, minor, micro int) {
327+
func GetLibraryVersion() (major, minor, micro uint) {
328328
return verMajor, verMinor, verMicro
329329
}
330330

seccomp_internal.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,37 @@ const int C_CMP_GE = (int)SCMP_CMP_GE;
120120
const int C_CMP_GT = (int)SCMP_CMP_GT;
121121
const int C_CMP_MASKED_EQ = (int)SCMP_CMP_MASKED_EQ;
122122
123-
const int C_VERSION_MAJOR = SCMP_VER_MAJOR;
124-
const int C_VERSION_MINOR = SCMP_VER_MINOR;
125-
const int C_VERSION_MICRO = SCMP_VER_MICRO;
123+
#if SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 3
124+
unsigned int get_major_version()
125+
{
126+
return seccomp_version()->major;
127+
}
128+
129+
unsigned int get_minor_version()
130+
{
131+
return seccomp_version()->minor;
132+
}
133+
134+
unsigned int get_micro_version()
135+
{
136+
return seccomp_version()->micro;
137+
}
138+
#else
139+
unsigned int get_major_version()
140+
{
141+
return (unsigned int)SCMP_MAJOR_VERSION;
142+
}
143+
144+
unsigned int get_minor_version()
145+
{
146+
return (unsigned int)SCMP_MINOR_VERSION;
147+
}
148+
149+
unsigned int get_micro_version()
150+
{
151+
return (unsigned int)SCMP_MICRO_VERSION;
152+
}
153+
#endif
126154
127155
typedef struct scmp_arg_cmp* scmp_cast_t;
128156
@@ -177,15 +205,15 @@ var (
177205
// Error thrown on bad filter context
178206
errBadFilter = fmt.Errorf("filter is invalid or uninitialized")
179207
// Constants representing library major, minor, and micro versions
180-
verMajor = int(C.C_VERSION_MAJOR)
181-
verMinor = int(C.C_VERSION_MINOR)
182-
verMicro = int(C.C_VERSION_MICRO)
208+
verMajor = uint(C.get_major_version())
209+
verMinor = uint(C.get_minor_version())
210+
verMicro = uint(C.get_micro_version())
183211
)
184212

185213
// Nonexported functions
186214

187215
// Check if library version is greater than or equal to the given one
188-
func checkVersionAbove(major, minor, micro int) bool {
216+
func checkVersionAbove(major, minor, micro uint) bool {
189217
return (verMajor > major) ||
190218
(verMajor == major && verMinor > minor) ||
191219
(verMajor == major && verMinor == minor && verMicro >= micro)

0 commit comments

Comments
 (0)