Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial code for a few Unix 'libc' API(s) #554

Merged
merged 1 commit into from
Dec 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Features
* [#548](https://github.com/java-native-access/jna/pull/548): Return 64-bit unsigned integer from `com.sun.jna.platform.win32.WinBase.FILETIME` - [@dbwiddis](https://github.com/dbwiddis).
* [#524](https://github.com/java-native-access/jna/pull/524): Added IShellFolder interface plus necessary utility functions to Windows platform, and a sample for enumerating objects in My Computer - [@lwahonen](https://github.com/lwahonen).
* [#484](https://github.com/twall/jna/pull/484): Added `XFetchName` to `X11` interface - [@pinaf](https://github.com/pinaf).
* [#554](https://github.com/java-native-access/jna/pull/554): Initial code for a few Unix 'libc' API(s) [@lgoldstein](https://github.com/lgoldstein)

Bug Fixes
---------
Expand Down
25 changes: 25 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/unix/LibC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright (c) 2015 Goldstein Lyor, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.unix;

import com.sun.jna.Library;
import com.sun.jna.Native;

/**
* <I>libc</I> API
* @author Lyor Goldstein
*/
public interface LibC extends LibCAPI, Library {
String NAME = "c";
LibC INSTANCE = (LibC) Native.loadLibrary(NAME, LibC.class);
}
76 changes: 76 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/unix/LibCAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* Copyright (c) 2015 Goldstein Lyor, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.unix;

/**
* 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 {
// 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(char[] name, int len);
int sethostname(char[] name, int len);

// see man(2) get/set domainname
int getdomainname(char[] name, int len);
int setdomainname(char[] 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>
*/
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>
*/
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>
*/
int unsetenv(String name);
}
43 changes: 43 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/unix/Reboot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Copyright (c) 2015 Goldstein Lyor, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.unix;

/**
* Contains definitions related to the {@code reboot} API
* @author Lyor Goldstein
*/
public interface Reboot {
/** Perform a hard reset now. */
int RB_AUTOBOOT = 0x01234567;
/* Halt the system. */
int RB_HALT_SYSTEM = 0xcdef0123;
/** Enable reboot using Ctrl-Alt-Delete keystroke. */
int RB_ENABLE_CAD = 0x89abcdef;
/** Disable reboot using Ctrl-Alt-Delete keystroke. */
int RB_DISABLE_CAD = 0;
/** Stop system and switch power off if possible. */
int RB_POWER_OFF = 0x4321fedc;
/** Suspend system using software suspend. */
int RB_SW_SUSPEND = 0xd000fce2;
/** Reboot system into new kernel. */
int RB_KEXEC = 0x45584543;

/**
* Stops/Reboots the machine
* @param cmd The command
* @return If successful, this call never returns. Otherwise, a -1
* is returned and an error is returned in the global variable {@code errno}.
* @see <A HREF="http://www.unix.com/man-page/freebsd/2/reboot/">man 2 reboot</A>
*/
int reboot(int cmd);
}
101 changes: 101 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/unix/Resource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* Copyright (c) 2015 Goldstein Lyor, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.unix;

import java.util.Arrays;
import java.util.List;

import com.sun.jna.Structure;

/**
* Definitions related to {@code getrlimit}/{@code setrlimit}
* @author Lyor Goldstein
*/
public interface Resource {
/** Per-process CPU limit, in seconds. */
int RLIMIT_CPU = 0;

/** Largest file that can be created, in bytes. */
int RLIMIT_FSIZE = 1;

/** Maximum size of data segment, in bytes. */
int RLIMIT_DATA = 2;

/** Maximum size of stack segment, in bytes. */
int RLIMIT_STACK = 3;

/** Largest core file that can be created, in bytes. */
int RLIMIT_CORE = 4;

/**
* Largest resident set size, in bytes. This affects swapping; processes
* that are exceeding their resident set size will be more likely to have
* physical memory taken from them.
*/
int RLIMIT_RSS = 5;

/** Number of open files. */
int RLIMIT_NOFILE = 7;

/** Address space limit. */
int RLIMIT_AS = 9;

/** Number of processes. */
int RLIMIT_NPROC = 6;

/** Locked-in-memory address space. */
int RLIMIT_MEMLOCK = 8;

/** Maximum number of file locks. */
int RLIMIT_LOCKS = 10;

/** Maximum number of pending signals. */
int RLIMIT_SIGPENDING = 11;

/** Maximum bytes in POSIX message queues. */
int RLIMIT_MSGQUEUE = 12;

/**
* Maximum nice priority allowed to raise to. Nice levels 19 .. -20
* correspond to 0 .. 39 values of this resource limit.
*/
int RLIMIT_NICE = 13;
int RLIMIT_RTPRIO = 14;

/**
* Maximum CPU time in microseconds that a process scheduled under a
* real-time scheduling policy may consume without making a blocking
* system call before being forcibly de-scheduled.
*/
int RLIMIT_RTTIME = 15;

/** Number of {@code rlimit} values */
int RLIMIT_NLIMITS = 16;

public static class Rlimit extends Structure {
/** The current (soft) limit. */
public long rlim_cur;

/** The hard limit. */
public long rlim_max;

@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "rlim_cur", "rlim_max" });
}
}

// see man(2) rlimit
int getrlimit(int resource, Rlimit rlim);
int setrlimit(int resource, Rlimit rlim);
}
Loading