Skip to content

Commit

Permalink
Define size_t.ByReference and fix macOS sysctl size_t* parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed May 29, 2021
1 parent f89905a commit 68f1195
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 78 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Features
Bug Fixes
---------
* [#1343](https://github.com/java-native-access/jna/issues/1343): `c.s.j.p.mac.CoreFoundation.CFStringRef#stringValue` buffer needs space for a null byte - [@dbwiddis](https://github.com/dbwiddis).
* [#1351](https://github.com/java-native-access/jna/issues/1351): Define `c.s.j.p.unix.size_t.ByReference` and fix macOS sysctl `size_t *` parameters - [@dbwiddis](https://github.com/dbwiddis).

Release 5.8.0
=============
Expand Down
56 changes: 40 additions & 16 deletions contrib/platform/src/com/sun/jna/platform/mac/SystemB.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,10 @@ class Timezone extends Structure {
* The sysctl() function retrieves system information and allows processes with
* appropriate privileges to set system information. The information available
* from sysctl() consists of integers, strings, and tables.
*
* <p>
* The state is described using a "Management Information Base" (MIB) style
* name, listed in name, which is a namelen length array of integers.
*
* <p>
* The information is copied into the buffer specified by oldp. The size of the
* buffer is given by the location specified by oldlenp before the call, and
* that location gives the amount of data copied after a successful call and
Expand All @@ -635,32 +635,41 @@ class Timezone extends Structure {
* as much data as fits in the buffer provided and returns with the error code
* ENOMEM. If the old value is not desired, oldp and oldlenp should be set to
* NULL.
*
* <p>
* The size of the available data can be determined by calling sysctl() with the
* NULL argument for oldp. The size of the available data will be returned in
* the location pointed to by oldlenp. For some operations, the amount of space
* may change often. For these operations, the system attempts to round up so
* that the returned size is large enough for a call to return the data shortly
* thereafter.
*
* <p>
* To set a new value, newp is set to point to a buffer of length newlen from
* which the requested value is to be taken. If a new value is not to be set,
* newp should be set to NULL and newlen set to 0.
*
* @param name
* MIB array of integers
* a Management Information Base (MIB) array of integers
* @param namelen
* length of the MIB array
* the length of the array in {@code name}
* @param oldp
* Information retrieved
* A buffer to hold the information retrieved
* @param oldlenp
* Size of information retrieved
* Size of the buffer, a pointer to a {@link size_t} value
* @param newp
* Information to be written
* To set a new value, a buffer of information to be written. May be
* null if no value is to be set.
* @param newlen
* Size of information to be written
* Size of the information to be written. May be 0 if no value is to
* be set.
* @return 0 on success; sets errno on failure
*/
int sysctl(int[] name, int namelen, Pointer oldp, size_t.ByReference oldlenp, Pointer newp, size_t newlen);

/**
* @deprecated Use
* {@link #sysctl(int[], int, Pointer, Pointer, Pointer, com.sun.jna.platform.unix.LibCAPI.size_t)}
*/
@Deprecated
int sysctl(int[] name, int namelen, Pointer oldp, IntByReference oldlenp, Pointer newp, int newlen);

/**
Expand All @@ -671,15 +680,24 @@ class Timezone extends Structure {
* @param name
* ASCII representation of the MIB name
* @param oldp
* Information retrieved
* A buffer to hold the information retrieved
* @param oldlenp
* Size of information retrieved
* Size of the buffer, a pointer to a {@link size_t} value
* @param newp
* Information to be written
* To set a new value, a buffer of information to be written. May be
* null if no value is to be set.
* @param newlen
* Size of information to be written
* Size of the information to be written. May be 0 if no value is to
* be set.
* @return 0 on success; sets errno on failure
*/
int sysctlbyname(String name, Pointer oldp, size_t.ByReference oldlenp, Pointer newp, size_t newlen);

/**
* @deprecated Use
* {@link #sysctlbyname(String, Pointer, Pointer, Pointer, com.sun.jna.platform.unix.LibCAPI.size_t)}
*/
@Deprecated
int sysctlbyname(String name, Pointer oldp, IntByReference oldlenp, Pointer newp, int newlen);

/**
Expand All @@ -705,12 +723,18 @@ class Timezone extends Structure {
* ASCII representation of the name
* @param mibp
* Integer array containing the corresponding name vector.
* @param size
* @param sizep
* On input, number of elements in the returned array; on output, the
* number of entries copied.
* @return 0 on success; sets errno on failure
*/
int sysctlnametomib(String name, Pointer mibp, IntByReference size);
int sysctlnametomib(String name, Pointer mibp, size_t.ByReference sizep);

/**
* @deprecated Use {@link #sysctlnametomib(String, Pointer, Pointer)}
*/
@Deprecated
int sysctlnametomib(String name, Pointer mibp, IntByReference sizep);

/**
* The host_processor_info function returns information about processors.
Expand Down
117 changes: 84 additions & 33 deletions contrib/platform/src/com/sun/jna/platform/unix/LibCAPI.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2015 Goldstein Lyor, All Rights Reserved
/* Copyright (c) 2015 Goldstein Lyor, 2021 Daniel Widdis. All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
Expand Down Expand Up @@ -31,6 +31,7 @@
* Note: we are using this &quot;intermediate&quot; API in order to allow
* Linux-like O/S-es to implement the same API, but maybe using a different
* library name
*
* @author Lyor Goldstein
*/
public interface LibCAPI extends Reboot, Resource {
Expand All @@ -43,6 +44,33 @@ class size_t extends IntegerType {

private static final long serialVersionUID = 1L;

public static class ByReference extends com.sun.jna.ptr.ByReference {
public ByReference() {
this(0);
}

public ByReference(long value) {
this(new size_t(value));
}

public ByReference(size_t value) {
super(Native.SIZE_T_SIZE);
setValue(value);
}

public void setValue(size_t value) {
if (Native.SIZE_T_SIZE > 4) {
getPointer().setLong(0, value.longValue());
} else {
getPointer().setInt(0, value.intValue());
}
}

public size_t getValue() {
return new size_t(Native.SIZE_T_SIZE > 4 ? getPointer().getLong(0) : getPointer().getInt(0));
}
}

public size_t() {
this(0);
}
Expand Down Expand Up @@ -72,70 +100,93 @@ public ssize_t(long value) {

// see man(2) get/set uid/gid
int getuid();

int geteuid();

int getgid();

int getegid();

int setuid(int uid);

int seteuid(int uid);

int setgid(int gid);

int setegid(int gid);

// see man(2) get/set hostname
int HOST_NAME_MAX = 255; // not including the '\0'

int gethostname(byte[] name, int len);

int sethostname(String name, int len);

// see man(2) get/set domainname
int getdomainname(byte[] name, int len);

int setdomainname(String name, int len);

/**
* @param name Environment variable name
* @return Returns the value in the environment, or {@code null} if there
* is no match for the name
* @see <A HREF="https://www.freebsd.org/cgi/man.cgi?query=setenv&sektion=3">getenv(3)</A>
* @param name
* Environment variable name
* @return Returns the value in the environment, or {@code null} if there is no
* match for the name
* @see <A HREF=
* "https://www.freebsd.org/cgi/man.cgi?query=setenv&sektion=3">getenv(3)</A>
*/
String getenv(String name);

/**
* Update or add a variable in the environment of the calling process.
* @param name Environment variable name
* @param value Required value
* @param overwrite If the environment variable already exists and the
* value of {@code overwrite} is non-zero, the function shall return
* success and the environment shall be updated. If the environment
* variable already exists and the value of {@code overwrite} is zero, the
* function shall return success and the environment shall remain unchanged.
* @return Upon successful completion, zero shall be returned. Otherwise,
* -1 shall be returned, {@code errno} set to indicate the error, and the
* environment shall be unchanged
* @see <A HREF="https://www.freebsd.org/cgi/man.cgi?query=setenv&sektion=3">getenv(3)</A>
*
* @param name
* Environment variable name
* @param value
* Required value
* @param overwrite
* If the environment variable already exists and the value of
* {@code overwrite} is non-zero, the function shall return success
* and the environment shall be updated. If the environment variable
* already exists and the value of {@code overwrite} is zero, the
* function shall return success and the environment shall remain
* unchanged.
* @return Upon successful completion, zero shall be returned. Otherwise, -1
* shall be returned, {@code errno} set to indicate the error, and the
* environment shall be unchanged
* @see <A HREF=
* "https://www.freebsd.org/cgi/man.cgi?query=setenv&sektion=3">getenv(3)</A>
*/
int setenv(String name, String value, int overwrite);

/**
* @param name Environment variable name - If the named variable does not
* exist in the current environment, the environment shall be unchanged
* and the function is considered to have completed successfully.
* @return Upon successful completion, zero shall be returned. Otherwise,
* -1 shall be returned, {@code errno} set to indicate the error, and the
* environment shall be unchanged
* @see <A HREF="https://www.freebsd.org/cgi/man.cgi?query=setenv&sektion=3">getenv(3)</A>
* @param name
* Environment variable name - If the named variable does not exist
* in the current environment, the environment shall be unchanged and
* the function is considered to have completed successfully.
* @return Upon successful completion, zero shall be returned. Otherwise, -1
* shall be returned, {@code errno} set to indicate the error, and the
* environment shall be unchanged
* @see <A HREF=
* "https://www.freebsd.org/cgi/man.cgi?query=setenv&sektion=3">getenv(3)</A>
*/
int unsetenv(String name);

/**
* The getloadavg() function returns the number of processes in the system
* run queue averaged over various periods of time. Up to nelem samples are
* retrieved and assigned to successive elements of loadavg[]. The system
* imposes a maximum of 3 samples, representing averages over the last 1, 5,
* and 15 minutes, respectively.
* @param loadavg An array of doubles which will be filled with the results
* @param nelem Number of samples to return
* @return If the load average was unobtainable, -1 is returned; otherwise,
* the number of samples actually retrieved is returned.
* @see <A HREF="https://www.freebsd.org/cgi/man.cgi?query=getloadavg&sektion=3">getloadavg(3)</A>
* The getloadavg() function returns the number of processes in the system run
* queue averaged over various periods of time. Up to nelem samples are
* retrieved and assigned to successive elements of loadavg[]. The system
* imposes a maximum of 3 samples, representing averages over the last 1, 5, and
* 15 minutes, respectively.
*
* @param loadavg
* An array of doubles which will be filled with the results
* @param nelem
* Number of samples to return
* @return If the load average was unobtainable, -1 is returned; otherwise, the
* number of samples actually retrieved is returned.
* @see <A HREF=
* "https://www.freebsd.org/cgi/man.cgi?query=getloadavg&sektion=3">getloadavg(3)</A>
*/
int getloadavg(double[] loadavg, int nelem);

Expand Down
Loading

0 comments on commit 68f1195

Please sign in to comment.