Last active
September 29, 2017 14:27
-
-
Save aeppert/489eea84010abd2907a67b7f985490a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ Aaron Eppert - 2017 | |
// golang wrapper around libkmod with lsmod as an example | |
// | |
package main | |
/* | |
#cgo pkg-config: libkmod | |
#include <libkmod.h> | |
*/ | |
import "C" | |
import ( | |
"errors" | |
"fmt" | |
) | |
func lsmod() error { | |
ctx := C.kmod_new(nil, nil) | |
if ctx == nil { | |
return errors.New("Could not obtain kmod context") | |
} | |
var list *C.struct_kmod_list | |
errVal := C.kmod_module_new_from_loaded(ctx, &list) | |
if errVal < 0 { | |
C.kmod_unref(ctx) | |
return errors.New("Could not get list of modules") | |
} | |
fmt.Println("Module Size Used by") | |
for itr := list; itr != nil; itr = C.kmod_list_next(list, itr) { | |
mod := C.kmod_module_get_module(itr) | |
name := C.kmod_module_get_name(mod) | |
useCount := C.kmod_module_get_refcnt(mod) | |
size := C.kmod_module_get_size(mod) | |
fmt.Printf("%-19s %8v %d", C.GoString(name), size, useCount) | |
holders := C.kmod_module_get_holders(mod) | |
first := true | |
if holders != nil { | |
for hitr := holders; hitr != nil; hitr = C.kmod_list_next(holders, hitr) { | |
hm := C.kmod_module_get_module(hitr) | |
if !first { | |
fmt.Printf(",") | |
} else { | |
fmt.Printf(" ") | |
first = false | |
} | |
fmt.Printf("%s", C.GoString(C.kmod_module_get_name(hm))) | |
C.kmod_module_unref(hm) | |
} | |
} | |
fmt.Println() | |
C.kmod_module_unref_list(holders) | |
C.kmod_module_unref(mod) | |
} | |
C.kmod_module_unref_list(list) | |
C.kmod_unref(ctx) | |
return nil | |
} | |
func main() { | |
err := lsmod() | |
if err != nil { | |
fmt.Println(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment