mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
Add C code + Header C + CGo + Go lib to change rLimit of NOFILE in Windows OD
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
10
njs-ioutils/maxstdio/maxstdio.c
Normal file
10
njs-ioutils/maxstdio/maxstdio.c
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "maxstdio.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int CGetMaxSTDIO() {
|
||||
return _getmaxstdio();
|
||||
}
|
||||
|
||||
int CSetMaxSTDIO(int new_max) {
|
||||
return _setmaxstdio(new_max);
|
||||
}
|
||||
16
njs-ioutils/maxstdio/maxstdio.go
Normal file
16
njs-ioutils/maxstdio/maxstdio.go
Normal 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)))
|
||||
}
|
||||
5
njs-ioutils/maxstdio/maxstdio.h
Normal file
5
njs-ioutils/maxstdio/maxstdio.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#ifndef _MAXSTDIO_H
|
||||
#define _MAXSTDIO_H
|
||||
int CGetMaxSTDIO();
|
||||
int CSetMaxSTDIO(int new_max);
|
||||
#endif
|
||||
BIN
njs-ioutils/maxstdio/maxstdio.o
Normal file
BIN
njs-ioutils/maxstdio/maxstdio.o
Normal file
Binary file not shown.
26
test/test-win-max-nofile/main.go
Normal file
26
test/test-win-max-nofile/main.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user