Search in sources :

Example 1 with IntByReference

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;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Platform(com.facebook.buck.util.environment.Platform) Libc(com.facebook.buck.util.Libc)

Example 2 with IntByReference

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());
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) IOException(java.io.IOException)

Example 3 with IntByReference

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);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference)

Example 4 with IntByReference

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;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) TreeSet(java.util.TreeSet)

Example 5 with IntByReference

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));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ACCESS_ALLOWED_ACE(com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE) Memory(com.sun.jna.Memory) PointerByReference(com.sun.jna.ptr.PointerByReference) ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)199 PointerByReference (com.sun.jna.ptr.PointerByReference)38 Memory (com.sun.jna.Memory)33 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)26 File (java.io.File)19 Pointer (com.sun.jna.Pointer)15 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)14 PSID (com.sun.jna.platform.win32.WinNT.PSID)13 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)11 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)11 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)9 ACL (com.sun.jna.platform.win32.WinNT.ACL)8 Advapi32 (com.sun.jna.platform.win32.Advapi32)7 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)7 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)6 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)6 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)6 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)6 CredHandle (com.sun.jna.platform.win32.Sspi.CredHandle)5