com.sun.jna.Native.getLastError()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(327)

本文整理了Java中com.sun.jna.Native.getLastError()方法的一些代码示例,展示了Native.getLastError()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Native.getLastError()方法的具体详情如下:
包路径:com.sun.jna.Native
类名称:Native
方法名:getLastError

Native.getLastError介绍

[英]Retrieve the last error set by the OS. This corresponds to GetLastError() on Windows, and errno on most other platforms. The value is preserved per-thread, but whether the original value is per-thread depends on the underlying OS. The result is undefined If #getPreserveLastError is false.
[中]检索操作系统设置的最后一个错误。这在Windows上对应于GetLastError(),在大多数其他平台上对应于errno。每个线程保留该值,但原始值是否为每个线程取决于底层操作系统。如果#getPreserveLastError为[$2$],则结果未定义。

代码示例

代码示例来源:origin: voldemort/voldemort

/**
 * The <errno.h> header file defines the integer variable errno, which is
 * set by system calls and some library functions in the event of an error
 * to indicate what went wrong. Its value is significant only when the call
 * returned an error (usually -1), and a function that does succeed is
 * allowed to change errno.
 * 
 * Sometimes, when -1 is also a valid successful return value one has to
 * zero errno before the call in order to detect possible errors.
 * 
 * errno is defined by the ISO C standard to be a modifiable lvalue of type
 * int, and must not be explicitly declared; errno may be a macro. errno is
 * thread-local; setting it in one thread does not affect its value in any
 * other thread.
 * 
 * Valid error numbers are all non-zero; errno is never set to zero by any
 * library function. All the error names specified by POSIX.1 must have
 * distinct values, with the exception of EAGAIN and EWOULDBLOCK, which may
 * be the same.
 * 
 * Below is a list of the symbolic error names that are defined on Linux.
 * Some of these are marked POSIX.1, indicating that the name is defined by
 * POSIX.1-2001, or C99, indicating that the name is defined by C99.
 * 
 */
public static int errno() {
  return Native.getLastError();
}

代码示例来源:origin: apache/ignite

/**
 * Retrieve last error set by the OS as string. This corresponds to and <code>errno</code> on
 * *nix platforms.
 * @return displayable string with OS error info.
 */
private static String getLastError() {
  return IgniteNativeIoLib.strerror(Native.getLastError());
}

代码示例来源:origin: apache/geode

public static void lockMemory() {
 try {
  Native.register(Platform.C_LIBRARY_NAME);
  int result = mlockall(1);
  if (result == 0) {
   return;
  }
 } catch (Throwable t) {
  throw new IllegalStateException("Error trying to lock memory", t);
 }
 int lastError = Native.getLastError();
 String message = "mlockall failed: " + lastError;
 if (lastError == 1 || lastError == 12) { // EPERM || ENOMEM
  message = "Unable to lock memory due to insufficient free space or privileges.  "
    + "Please check the RLIMIT_MEMLOCK soft resource limit (ulimit -l) and "
    + "increase the available memory if needed";
 }
 throw new IllegalStateException(message);
}

代码示例来源:origin: jenkinsci/jenkins

sei.nShow = SW_HIDE;
if (!Shell32.INSTANCE.ShellExecuteEx(sei))
  throw new IOException("Failed to shellExecute: "+ Native.getLastError());

代码示例来源:origin: jenkinsci/jenkins

throw new IOException("Failed to obtain memory requirement: "+LIBC.strerror(Native.getLastError()));
if(Native.getLastError()==ENOMEM && nRetry++<16)
  continue; // retry
throw new IOException("Failed to call kern.proc.all: "+LIBC.strerror(Native.getLastError()));

代码示例来源:origin: jenkinsci/jenkins

public void restart() throws Exception {
  // close all files upon exec, except stdin, stdout, and stderr
  int sz = LIBC.getdtablesize();
  for (int i = 3; i < sz; i++) {
    int flags = LIBC.fcntl(i, F_GETFD);
    if (flags < 0) continue;
    LIBC.fcntl(i, F_SETFD, flags | FD_CLOEXEC);
  }
  // exec to self
  String exe = Daemon.getCurrentExecutable();
  LIBC.execv(exe, new StringArray(args.toArray(new String[args.size()])));
  throw new IOException("Failed to exec '" + exe + "' " + LIBC.strerror(Native.getLastError()));
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public void restart() throws IOException, InterruptedException {
  Jenkins jenkins = Jenkins.getInstanceOrNull(); // guard against repeated concurrent calls to restart
  try {
    if (jenkins != null) {
      jenkins.cleanUp();
    }
  } catch (Exception e) {
    LOGGER.log(Level.SEVERE, "Failed to clean up. Restart will continue.", e);
  }
  // close all files upon exec, except stdin, stdout, and stderr
  int sz = LIBC.getdtablesize();
  for(int i=3; i<sz; i++) {
    int flags = LIBC.fcntl(i, F_GETFD);
    if(flags<0) continue;
    LIBC.fcntl(i, F_SETFD,flags| FD_CLOEXEC);
  }
  // exec to self
  String exe = args.get(0);
  LIBC.execvp(exe, new StringArray(args.toArray(new String[args.size()])));
  throw new IOException("Failed to exec '"+exe+"' "+LIBC.strerror(Native.getLastError()));
}

代码示例来源:origin: apache/ignite

int error = Native.getLastError();
String msg = "Error opening file [" + pathname + "] with flags [0x"
  + String.format("%2X", openFlags) + ": DIRECT & " + Arrays.asList(modes)

代码示例来源:origin: apache/incubator-druid

if (ret_code != 0) {
 log.warn("failed on syncing fd [%d], offset [%d], bytes [%d], ret_code [%d], errno [%d]",
   fd, offset, nbytes, ret_code, Native.getLastError());
 return;

代码示例来源:origin: jenkinsci/jenkins

throw new IOException("Failed to get kern.argmax: "+LIBC.strerror(Native.getLastError()));
size.setValue(argmax);
if(LIBC.sysctl(new int[]{CTL_KERN,KERN_PROCARGS2,pid},3, m, size, NULL, intByRef)!=0)
  throw new IOException("Failed to obtain ken.procargs2: "+LIBC.strerror(Native.getLastError()));

代码示例来源:origin: net.java.dev.jna/jna

@Override
  Object invoke(Method invokingMethod, Class<?>[] paramTypes, Class<?> returnType, Object[] inArgs, Map<String, ?> options) {
    return Integer.valueOf(Native.getLastError());
  }
};

代码示例来源:origin: net.java.dev.jna/jna

@Override
Object invoke(Object[] args, Class<?> returnType, boolean b, int fixedArgs) {
  return Integer.valueOf(Native.getLastError());
}

代码示例来源:origin: org.elasticsearch/elasticsearch

static void solarisImpl() {
  // first be defensive: we can give nice errors this way, at the very least.
  boolean supported = Constants.SUN_OS;
  if (supported == false) {
    throw new IllegalStateException("bug: should not be trying to initialize priv_set for an unsupported OS");
  }
  // we couldn't link methods, could be some really ancient Solaris or some bug
  if (libc_solaris == null) {
    throw new UnsupportedOperationException("priv_set unavailable: could not link methods. requires Solaris 10+");
  }
  // drop a null-terminated list of privileges
  if (libc_solaris.priv_set(PRIV_OFF, PRIV_ALLSETS, PRIV_PROC_FORK, PRIV_PROC_EXEC, null) != 0) {
    throw new UnsupportedOperationException("priv_set unavailable: priv_set(): " + JNACLibrary.strerror(Native.getLastError()));
  }
  logger.debug("Solaris priv_set initialization successful");
}

代码示例来源:origin: org.elasticsearch/elasticsearch

static void addConsoleCtrlHandler(ConsoleCtrlHandler handler) {
  // The console Ctrl handler is necessary on Windows platforms only.
  if (Constants.WINDOWS) {
    try {
      boolean result = JNAKernel32Library.getInstance().addConsoleCtrlHandler(handler);
      if (result) {
        logger.debug("console ctrl handler correctly set");
      } else {
        logger.warn("unknown error {} when adding console ctrl handler", Native.getLastError());
      }
    } catch (UnsatisfiedLinkError e) {
      // this will have already been logged by Kernel32Library, no need to repeat it
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

static void bsdImpl() {
  boolean supported = Constants.FREE_BSD || OPENBSD || Constants.MAC_OS_X;
  if (supported == false) {
    throw new IllegalStateException("bug: should not be trying to initialize RLIMIT_NPROC for an unsupported OS");
  }
  JNACLibrary.Rlimit limit = new JNACLibrary.Rlimit();
  limit.rlim_cur.setValue(0);
  limit.rlim_max.setValue(0);
  if (JNACLibrary.setrlimit(RLIMIT_NPROC, limit) != 0) {
    throw new UnsupportedOperationException("RLIMIT_NPROC unavailable: " + JNACLibrary.strerror(Native.getLastError()));
  }
  logger.debug("BSD RLIMIT_NPROC initialization successful");
}

代码示例来源:origin: org.elasticsearch/elasticsearch

static void trySetMaxSizeVirtualMemory() {
  if (Constants.LINUX || Constants.MAC_OS_X) {
    final JNACLibrary.Rlimit rlimit = new JNACLibrary.Rlimit();
    if (JNACLibrary.getrlimit(JNACLibrary.RLIMIT_AS, rlimit) == 0) {
      MAX_SIZE_VIRTUAL_MEMORY = rlimit.rlim_cur.longValue();
    } else {
      logger.warn("unable to retrieve max size virtual memory [" + JNACLibrary.strerror(Native.getLastError()) + "]");
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

static void trySetMaxFileSize() {
  if (Constants.LINUX || Constants.MAC_OS_X) {
    final JNACLibrary.Rlimit rlimit = new JNACLibrary.Rlimit();
    if (JNACLibrary.getrlimit(JNACLibrary.RLIMIT_FSIZE, rlimit) == 0) {
      MAX_FILE_SIZE = rlimit.rlim_cur.longValue();
    } else {
      logger.warn("unable to retrieve max file size [" + JNACLibrary.strerror(Native.getLastError()) + "]");
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

static void trySetMaxNumberOfThreads() {
  if (Constants.LINUX) {
    // this is only valid on Linux and the value *is* different on OS X
    // see /usr/include/sys/resource.h on OS X
    // on Linux the resource RLIMIT_NPROC means *the number of threads*
    // this is in opposition to BSD-derived OSes
    final int rlimit_nproc = 6;
    final JNACLibrary.Rlimit rlimit = new JNACLibrary.Rlimit();
    if (JNACLibrary.getrlimit(rlimit_nproc, rlimit) == 0) {
      MAX_NUMBER_OF_THREADS = rlimit.rlim_cur.longValue();
    } else {
      logger.warn("unable to retrieve max number of threads [" + JNACLibrary.strerror(Native.getLastError()) + "]");
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

/**
 * Retrieves the short path form of the specified path.
 *
 * @param path the path
 * @return the short path name (or the original path if getting the short path name fails for any reason)
 */
static String getShortPathName(String path) {
  assert Constants.WINDOWS;
  try {
    final WString longPath = new WString("\\\\?\\" + path);
    // first we get the length of the buffer needed
    final int length = JNAKernel32Library.getInstance().GetShortPathNameW(longPath, null, 0);
    if (length == 0) {
      logger.warn("failed to get short path name: {}", Native.getLastError());
      return path;
    }
    final char[] shortPath = new char[length];
    // knowing the length of the buffer, now we get the short name
    if (JNAKernel32Library.getInstance().GetShortPathNameW(longPath, shortPath, length) > 0) {
      return Native.toString(shortPath);
    } else {
      logger.warn("failed to get short path name: {}", Native.getLastError());
      return path;
    }
  } catch (final UnsatisfiedLinkError e) {
    return path;
  }
}

代码示例来源:origin: kaitoy/pcap4j

private MacAddress getMacAddress(String nifName) {
 Pointer lpAdapter = NativePacketDllMappings.PacketOpenAdapter(nifName);
 long hFile = -1;
 if (lpAdapter != null) {
  if (Native.POINTER_SIZE == 4) {
   hFile = lpAdapter.getInt(0);
  } else {
   hFile = lpAdapter.getLong(0);
  }
 }
 if (hFile == -1L) {
  int err = Native.getLastError();
  logger.error("Unable to open the NIF {}, Error Code: {}", nifName, err);
  return null;
 }
 Memory mem = new Memory(NativePacketDllMappings.PACKET_OID_DATA_SIZE);
 mem.clear();
 PACKET_OID_DATA oidData = new PACKET_OID_DATA(mem);
 oidData.Length = new NativeLong(6L);
 oidData.Oid = new NativeLong(0x01010102L);
 int status = NativePacketDllMappings.PacketRequest(lpAdapter, 0, oidData);
 NativePacketDllMappings.PacketCloseAdapter(lpAdapter);
 if (status == 0) {
  logger.error("Failed to retrieve the link layer address of the NIF: {}", nifName);
  return null;
 } else {
  return MacAddress.getByAddress(oidData.Data);
 }
}

相关文章

Native类方法