Add C code + Header C + CGo + Go lib to change rLimit of NOFILE in Windows OD

This commit is contained in:
Nicolas JUHEL
2020-06-18 14:03:44 +02:00
parent 2c37b83bff
commit 2df4b4d3f1
6 changed files with 87 additions and 2 deletions

View File

@@ -27,8 +27,36 @@
package njs_ioutils
import "fmt"
import (
"github.com/nabbar/golib/njs-ioutils/maxstdio"
)
/*
* install package gcc-multilib gcc-mingw-w64
* - i686-w64-mingw32 for 32-bit Windows;
* - x86_64-w64-mingw32 for 64-bit Windows.
* call go build with env var :
* - CC=i686-w64-mingw32-gcc for win32
* - CC=x86_64-w64-mingw32-gcc for win64
* build :
* all : cd <repos_dir>/vendor/github/nabbar/golib/njs-ioutils/maxstdio && CC=x86_64-w64-mingw32 gcc -c maxstdio.c
* win64 : cd <repos_dir> && CC=/usr/bin/x86_64-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -a -v .
* win32 : cd <repos_dir> && CC=/usr/bin/i686-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -a -v .
*/
func systemFileDescriptor(newValue int) (current int, max int, err error) {
return 0, 0, fmt.Errorf("rLimit is nor implemented in current system")
rLimit := maxstdio.GetMaxStdio()
if rLimit < 0 {
// default windows value
rLimit = 512
}
if newValue > rLimit {
rLimit = int(maxstdio.SetMaxStdio(newValue))
return SystemFileDescriptor(0)
}
return rLimit, rLimit, nil
// return 0, 0, fmt.Errorf("rLimit is nor implemented in current system")
}

View File

@@ -0,0 +1,10 @@
#include "maxstdio.h"
#include <stdio.h>
int CGetMaxSTDIO() {
return _getmaxstdio();
}
int CSetMaxSTDIO(int new_max) {
return _setmaxstdio(new_max);
}

View File

@@ -0,0 +1,16 @@
// +build windows cgo
package maxstdio
// #cgo CFLAGS: -g -Wall
// #include <stdlib.h>
// #include "maxstdio.h"
import "C"
func GetMaxStdio() int {
return int(C.CGetMaxSTDIO())
}
func SetMaxStdio(newMax int) int {
return int(C.CSetMaxSTDIO(C.int(newMax)))
}

View File

@@ -0,0 +1,5 @@
#ifndef _MAXSTDIO_H
#define _MAXSTDIO_H
int CGetMaxSTDIO();
int CSetMaxSTDIO(int new_max);
#endif

Binary file not shown.

View File

@@ -0,0 +1,26 @@
package main
import (
"fmt"
"os"
njs_ioutils "github.com/nabbar/golib/njs-ioutils"
)
func main() {
println("test to print Max STDIO NOFILE capabilities !!")
c, _, e := njs_ioutils.SystemFileDescriptor(0)
println(fmt.Sprintf("Actual limit is : %v | err : %v", c, e))
if e != nil {
os.Exit(1)
}
println("test to Change Max STDIO NOFILE capabilities !!")
c, _, e = njs_ioutils.SystemFileDescriptor(c + 512)
println(fmt.Sprintf("New limit is : %v | err : %v", c, e))
if e != nil {
os.Exit(1)
}
}