// Code generated by cmd/cgo; DO NOT EDIT.

//line /home/jenkins/workspace/pop-os/repos/_build/ci/git/libnvidia-container/d17aa6f4960e1d386abadf2288403efa4c1753ab/jammy/partial.source/archive/deps/src/nvcgo/main.go:1:1
/*
 * Copyright (c) 2021, NVIDIA CORPORATION.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"fmt"

	"nvcgo/internal/cgroup"
)

// #include "ctypes.h"
import _ "unsafe"

// Empty main for c-shared library.
func main() {}

// Alias for a C-based device_rule struct
type CDeviceRule =  /*line :32:20*/_Ctype_struct_device_rule /*line :32:40*/

// Convert a C-based DeviceRule to a Go-based cgroup.DeviceRule
func (r *CDeviceRule) convert() cgroup.DeviceRule {
	return cgroup.DeviceRule{
		Allow:  bool(r.allow),
		Type:   ( /*line :38:11*/_Cfunc_GoString /*line :38:20*/)(r._type),
		Access: ( /*line :39:11*/_Cfunc_GoString /*line :39:20*/)(r.access),
		Major:  func() *int64 { m := int64(r.major); return &m }(),
		Minor:  func() *int64 { m := int64(r.minor); return &m }(),
	}
}

//export GetDeviceCGroupVersion
func GetDeviceCGroupVersion(rootPath * /*line :46:39*/_Ctype_char /*line :46:45*/, pid  /*line :46:51*/_Ctype_pid_t /*line :46:58*/, version * /*line :46:69*/_Ctype_int /*line :46:74*/, rerr ** /*line :46:83*/_Ctype_char /*line :46:89*/)  /*line :46:91*/_Ctype_int /*line :46:96*/ {
	v, err := cgroup.GetDeviceCGroupVersion(( /*line :47:42*/_Cfunc_GoString /*line :47:51*/)(rootPath), int(pid))
	if err != nil {
		*rerr = ( /*line :49:11*/_Cfunc_CString /*line :49:19*/)(err.Error())
		return -1
	}
	*version =  /*line :52:13*/_Ctype_int /*line :52:18*/(v)
	return 0
}

//export GetDeviceCGroupMountPath
func GetDeviceCGroupMountPath(version  /*line :57:39*/_Ctype_int /*line :57:44*/, procRootPath * /*line :57:60*/_Ctype_char /*line :57:66*/, pid  /*line :57:72*/_Ctype_pid_t /*line :57:79*/, cgroupMountPath ** /*line :57:99*/_Ctype_char /*line :57:105*/, cgroupRootPrefix ** /*line :57:126*/_Ctype_char /*line :57:132*/, rerr ** /*line :57:141*/_Ctype_char /*line :57:147*/)  /*line :57:149*/_Ctype_int /*line :57:154*/ {
	api, err := cgroup.New(int(version))
	if err != nil {
		*rerr = ( /*line :60:11*/_Cfunc_CString /*line :60:19*/)(fmt.Sprintf("unable to create cgroupv%v interface: %v", version, err))
		return -1
	}

	p, r, err := api.GetDeviceCGroupMountPath(( /*line :64:44*/_Cfunc_GoString /*line :64:53*/)(procRootPath), int(pid))
	if err != nil {
		*rerr = ( /*line :66:11*/_Cfunc_CString /*line :66:19*/)(err.Error())
		return -1
	}
	*cgroupMountPath = ( /*line :69:21*/_Cfunc_CString /*line :69:29*/)(p)
	*cgroupRootPrefix= ( /*line :70:21*/_Cfunc_CString /*line :70:29*/)(r)

	return 0
}

//export GetDeviceCGroupRootPath
func GetDeviceCGroupRootPath(version  /*line :76:38*/_Ctype_int /*line :76:43*/, procRootPath * /*line :76:59*/_Ctype_char /*line :76:65*/, cgroupRootPrefix * /*line :76:85*/_Ctype_char /*line :76:91*/, pid  /*line :76:97*/_Ctype_int /*line :76:102*/, cgroupRootPath ** /*line :76:121*/_Ctype_char /*line :76:127*/, rerr ** /*line :76:136*/_Ctype_char /*line :76:142*/)  /*line :76:144*/_Ctype_int /*line :76:149*/ {
	api, err := cgroup.New(int(version))
	if err != nil {
		*rerr = ( /*line :79:11*/_Cfunc_CString /*line :79:19*/)(fmt.Sprintf("unable to create cgroupv%v interface: %v", version, err))
		return -1
	}

	p, err := api.GetDeviceCGroupRootPath(( /*line :83:40*/_Cfunc_GoString /*line :83:49*/)(procRootPath), ( /*line :83:66*/_Cfunc_GoString /*line :83:75*/)(cgroupRootPrefix), int(pid))
	if err != nil {
		*rerr = ( /*line :85:11*/_Cfunc_CString /*line :85:19*/)(err.Error())
		return -1
	}
	*cgroupRootPath = ( /*line :88:20*/_Cfunc_CString /*line :88:28*/)(p)

	return 0
}

//export AddDeviceRules
func AddDeviceRules(version  /*line :94:29*/_Ctype_int /*line :94:34*/, cgroupPath * /*line :94:48*/_Ctype_char /*line :94:54*/, crules []CDeviceRule, rerr ** /*line :94:85*/_Ctype_char /*line :94:91*/)  /*line :94:93*/_Ctype_int /*line :94:98*/ {
	api, err := cgroup.New(int(version))
	if err != nil {
		*rerr = ( /*line :97:11*/_Cfunc_CString /*line :97:19*/)(fmt.Sprintf("unable to create cgroupv%v interface: %v", version, err))
		return -1
	}

	rules := make([]cgroup.DeviceRule, len(crules))
	for i, cr := range crules {
		rules[i] = cr.convert()
	}

	err = api.AddDeviceRules(( /*line :106:27*/_Cfunc_GoString /*line :106:36*/)(cgroupPath), rules)
	if err != nil {
		*rerr = ( /*line :108:11*/_Cfunc_CString /*line :108:19*/)(err.Error())
		return -1
	}

	return 0
}
