...

Source file src/github.com/coreos/go-systemd/v22/internal/dlopen/dlopen_example.go

Documentation: github.com/coreos/go-systemd/v22/internal/dlopen

     1  // Copyright 2015 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  //go:build linux
    16  // +build linux
    17  
    18  package dlopen
    19  
    20  // #include <string.h>
    21  // #include <stdlib.h>
    22  //
    23  // int
    24  // my_strlen(void *f, const char *s)
    25  // {
    26  //   size_t (*strlen)(const char *);
    27  //
    28  //   strlen = (size_t (*)(const char *))f;
    29  //   return strlen(s);
    30  // }
    31  import "C"
    32  
    33  import (
    34  	"fmt"
    35  	"unsafe"
    36  )
    37  
    38  func strlen(libs []string, s string) (int, error) {
    39  	h, err := GetHandle(libs)
    40  	if err != nil {
    41  		return -1, fmt.Errorf(`couldn't get a handle to the library: %v`, err)
    42  	}
    43  	defer h.Close()
    44  
    45  	f := "strlen"
    46  	cs := C.CString(s)
    47  	defer C.free(unsafe.Pointer(cs))
    48  
    49  	strlen, err := h.GetSymbolPointer(f)
    50  	if err != nil {
    51  		return -1, fmt.Errorf(`couldn't get symbol %q: %v`, f, err)
    52  	}
    53  
    54  	len := C.my_strlen(strlen, cs)
    55  
    56  	return int(len), nil
    57  }
    58  

View as plain text