use of com.sun.jna.ptr.IntByReference in project buck by facebook.
the class Main method daemonizeIfPossible.
private static void daemonizeIfPossible() {
String osName = System.getProperty("os.name");
Libc.OpenPtyLibrary openPtyLibrary;
Platform platform = Platform.detect();
if (platform == Platform.LINUX) {
Libc.Constants.rTIOCSCTTY = Libc.Constants.LINUX_TIOCSCTTY;
Libc.Constants.rFDCLOEXEC = Libc.Constants.LINUX_FD_CLOEXEC;
Libc.Constants.rFGETFD = Libc.Constants.LINUX_F_GETFD;
Libc.Constants.rFSETFD = Libc.Constants.LINUX_F_SETFD;
openPtyLibrary = (Libc.OpenPtyLibrary) Native.loadLibrary("libutil", Libc.OpenPtyLibrary.class);
} else if (platform == Platform.MACOS) {
Libc.Constants.rTIOCSCTTY = Libc.Constants.DARWIN_TIOCSCTTY;
Libc.Constants.rFDCLOEXEC = Libc.Constants.DARWIN_FD_CLOEXEC;
Libc.Constants.rFGETFD = Libc.Constants.DARWIN_F_GETFD;
Libc.Constants.rFSETFD = Libc.Constants.DARWIN_F_SETFD;
openPtyLibrary = (Libc.OpenPtyLibrary) Native.loadLibrary(com.sun.jna.Platform.C_LIBRARY_NAME, Libc.OpenPtyLibrary.class);
} else {
LOG.info("not enabling process killing on nailgun exit: unknown OS %s", osName);
return;
}
// Making ourselves a session leader with setsid disconnects us from our controlling terminal
int ret = Libc.INSTANCE.setsid();
if (ret < 0) {
LOG.warn("cannot enable background process killing: %s", Native.getLastError());
return;
}
LOG.info("enabling background process killing for buckd");
IntByReference master = new IntByReference();
IntByReference slave = new IntByReference();
if (openPtyLibrary.openpty(master, slave, Pointer.NULL, Pointer.NULL, Pointer.NULL) != 0) {
throw new RuntimeException("Failed to open pty");
}
// Deliberately leak the file descriptors for the lifetime of this process; NuProcess can
// sometimes leak file descriptors to children, so make sure these FDs are marked close-on-exec.
markFdCloseOnExec(master.getValue());
markFdCloseOnExec(slave.getValue());
// Make the pty our controlling terminal; works because we disconnected above with setsid.
if (Libc.INSTANCE.ioctl(slave.getValue(), Pointer.createConstant(Libc.Constants.rTIOCSCTTY), 0) == -1) {
throw new RuntimeException("Failed to set pty");
}
LOG.info("enabled background process killing for buckd");
isSessionLeader = true;
}
use of com.sun.jna.ptr.IntByReference in project buck by facebook.
the class HostnameFetching method getHostnameWin32.
private static String getHostnameWin32() throws IOException {
char[] hostnameBuf = new char[HOSTNAME_MAX_LEN];
IntByReference hostnameBufLen = new IntByReference(hostnameBuf.length);
boolean result = HostnameFetchingWin32Library.INSTANCE.GetComputerNameEx(HostnameFetchingWin32Library.NAME_TYPE_DNS_HOSTNAME, hostnameBuf, hostnameBufLen);
if (!result) {
throw new IOException(String.format("Call to GetComputerNameEx failed with code %d", Native.getLastError()));
}
return new String(hostnameBuf, 0, hostnameBufLen.getValue());
}
use of com.sun.jna.ptr.IntByReference in project hudson-2.x by hudson.
the class RegistryKey method getValue.
private byte[] getValue(String valueName) {
IntByReference pType, lpcbData;
byte[] lpData = new byte[1];
pType = new IntByReference();
lpcbData = new IntByReference();
OUTER: while (true) {
int r = Advapi32.INSTANCE.RegQueryValueEx(handle, valueName, null, pType, lpData, lpcbData);
switch(r) {
case WINERROR.ERROR_MORE_DATA:
lpData = new byte[lpcbData.getValue()];
continue OUTER;
case WINERROR.ERROR_SUCCESS:
return lpData;
}
throw new JnaException(r);
}
}
use of com.sun.jna.ptr.IntByReference in project hudson-2.x by hudson.
the class RegistryKey method getSubKeys.
/**
* Get all sub keys of a key.
*
* @return array with all sub key names
*/
public Collection<String> getSubKeys() {
WINBASE.FILETIME lpftLastWriteTime;
TreeSet<String> subKeys = new TreeSet<String>();
char[] lpName = new char[256];
IntByReference lpcName = new IntByReference(256);
lpftLastWriteTime = new WINBASE.FILETIME();
int dwIndex = 0;
while (Advapi32.INSTANCE.RegEnumKeyEx(handle, dwIndex, lpName, lpcName, null, null, null, lpftLastWriteTime) == WINERROR.ERROR_SUCCESS) {
subKeys.add(new String(lpName, 0, lpcName.getValue()));
lpcName.setValue(256);
dwIndex++;
}
return subKeys;
}
use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class Advapi32Test method testGetAce.
public void testGetAce() throws IOException {
ACL pAcl;
int cbAcl = 0;
PSID pSid = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
IntByReference cbSid = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
assertTrue("Failed to create well-known SID", Advapi32.INSTANCE.CreateWellKnownSid(WELL_KNOWN_SID_TYPE.WinBuiltinAdministratorsSid, null, pSid, cbSid));
int sidLength = Advapi32.INSTANCE.GetLengthSid(pSid);
cbAcl = Native.getNativeSize(ACL.class, null);
cbAcl += Native.getNativeSize(ACCESS_ALLOWED_ACE.class, null);
cbAcl += (sidLength - DWORD.SIZE);
cbAcl = Advapi32Util.alignOnDWORD(cbAcl);
pAcl = new ACL(cbAcl);
assertTrue(Advapi32.INSTANCE.InitializeAcl(pAcl, cbAcl, WinNT.ACL_REVISION));
assertTrue(Advapi32.INSTANCE.AddAccessAllowedAce(pAcl, WinNT.ACL_REVISION, WinNT.STANDARD_RIGHTS_ALL, pSid));
PointerByReference pAce = new PointerByReference(new Memory(16));
assertTrue(Advapi32.INSTANCE.GetAce(pAcl, 0, pAce));
ACCESS_ALLOWED_ACE pAceGet = new ACCESS_ALLOWED_ACE(pAce.getValue());
assertTrue(pAceGet.Mask == WinNT.STANDARD_RIGHTS_ALL);
assertTrue(Advapi32.INSTANCE.EqualSid(pAceGet.psid, pSid));
}
Aggregations