Esempio n. 1
0
  @Ignore
  @Test
  public void crash() throws Exception {
    // Reproduces SIGBUS crash. Requires writing direct buffers
    // to a mapped file when there is not enough space left. To avoid having
    // to fill up a primary device use a small ramdisk, or loop device. E.g.
    //   $ dd if=/dev/zero of=file.img bs=1M count=100
    //   $ losetup /dev/loop0 file.img
    //   $ mkfs.ext4 /dev/loop0
    //   $ mkdir -p root
    //   $ mount -t ext4 /dev/loop0 root

    assumeTrue(Platform.isLinux());
    File f = new File("root/test.file");
    if (f.exists()) f.delete();

    FileIO fio = FileIO.open(f, EnumSet.of(OpenOption.CREATE, OpenOption.PREALLOCATE));
    final long len = f.getTotalSpace() * 5L;
    fio.setLength(len);
    fio.map();

    ByteBuffer bb = ByteBuffer.allocateDirect(4097);
    for (long pos = 0; pos < len - bb.remaining(); pos += 512) {
      fio.write(pos, bb);
      bb.flip();
    }

    fio.close();
  }
Esempio n. 2
0
 {
   if (Platform.isWindows()) {
     this.currentPlatformEnum = PlatformEnum.WINDOWS;
   } else if (Platform.isLinux()) {
     this.currentPlatformEnum = PlatformEnum.LINUX;
   } else if (Platform.isMac()) {
     this.currentPlatformEnum = PlatformEnum.MACOSX;
   } else {
     this.currentPlatformEnum = PlatformEnum.UNKNOWN;
   }
 }
Esempio n. 3
0
 static {
   try {
     // libmediainfo for linux depends on libzen
     if (Platform.isLinux()) {
       // We need to load dependencies first, because we know where our native libs are (e.g. Java
       // Web Start Cache).
       // If we do not, the system will look for dependencies, but only in the library path.
       NativeLibrary.getInstance("zen");
     }
   } catch (Throwable e) {
     Logger.getLogger(MediaInfo.class.getName()).warning("Failed to preload libzen");
   }
 }
  // deve saber se win ou linux
  public static IComunicacaoSensores getConector() {
    IComunicacaoSensores conector = null;

    if (Platform.isWindows()) {
      conector =
          (IComunicacaoSensores)
              Native.loadLibrary("comunicacaosensores.dll", IComunicacaoSensores.class);

    } else if (Platform.isLinux()) {
      conector =
          (IComunicacaoSensores)
              Native.loadLibrary("comunicacaosensores.so", IComunicacaoSensores.class);
    }
    return conector;
  }
Esempio n. 5
0
  @Test
  public void preallocateTooLarge() throws Exception {
    assumeTrue(Platform.isLinux());
    FileIO fio =
        FileIO.open(file, EnumSet.of(OpenOption.CREATE, OpenOption.MAPPED, OpenOption.PREALLOCATE));

    FileStore fs = Files.getFileStore(file.toPath());
    assumeTrue("ext4".equals(fs.type()) && !fs.name().contains("docker"));

    long len = file.getTotalSpace() * 100L;
    // setLength traps the IOException and prevents resizing / remapping. Not remapping avoids
    // the SIGBUS issue.
    fio.setLength(len);
    assertEquals(0, fio.length());

    fio.close();
  }
  protected static int getPid(Process p) throws Exception {

    if (Platform.isWindows()) {
      Field f = p.getClass().getDeclaredField("handle");
      f.setAccessible(true);
      int pid = Kernel32.INSTANCE.GetProcessId((Long) f.get(p));
      return pid;

    } else if (Platform.isLinux()) {
      Field f = p.getClass().getDeclaredField("pid");
      f.setAccessible(true);
      int pid = (Integer) f.get(p);
      return pid;

    } else {
      throw new Exception("Cannot currently process pid for " + System.getProperty("os.name"));
    }
  }
Esempio n. 7
0
  @Test
  public void preallocate() throws Exception {
    // Assuming a filesystem with delayed block allocation,
    // e.g. ext3 / ext4 on Linux.
    assumeTrue(Platform.isLinux() || Platform.isMac());

    long len = 100L * (1 << 20); // 100 MB
    FileIO fio = FileIO.open(file, EnumSet.of(OpenOption.CREATE, OpenOption.PREALLOCATE));
    long startFree = file.getFreeSpace();

    fio.setLength(len);
    long alloc = startFree - file.getFreeSpace();

    // Free space should be reduced. Pad expectation since external
    // events may interfere.
    assertTrue(alloc > (len >> 1));

    fio.close();
  }
Esempio n. 8
0
  public JPicosat(String libDir) throws Exception {
    if (libDir == null || "".equals(libDir)) libDir = "lib";
    StringBuilder pref = new StringBuilder(libDir + DIR);

    if (Platform.isMac())
      if (Platform.is64Bit()) pref.append("/macosx/64");
      else pref.append("/macosx/32");
    else if (Platform.isLinux())
      if (Platform.is64Bit()) pref.append("/linux/64");
      else pref.append("/linux/32");
    else if (Platform.isWindows())
      if (Platform.is64Bit()) pref.append("/win/64");
      else pref.append("/win/32");
    else throw new Exception("JPicosat: Platform unsupported!");

    System.setProperty("jna.library.path", pref.toString());
    INSTANCE = (CPicosat) Native.loadLibrary("picosat", CPicosat.class);
    checkLibraryVersion(pref.toString());
  }
Esempio n. 9
0
  /** Initialize the tray menu */
  private void initializeTray() {
    // Add uploaders from the list we loaded earlier
    final PopupMenu tray = new PopupMenu();
    // Add the action menu
    tray.add(new ActionMenuItem(Language.getString("cropupload"), ScreenshotAction.CROP));
    tray.add(new ActionMenuItem(Language.getString("fullupload"), ScreenshotAction.FULL));

    if (Platform.isWindows() || Platform.isLinux()) {
      tray.add(new ActionMenuItem(Language.getString("activeupload"), ScreenshotAction.ACTIVE));
    }

    tray.addSeparator();

    tray.add(new ActionMenuItem(Language.getString("clipboardupload"), ScreenshotAction.CLIPBOARD));
    tray.add(new ActionMenuItem(Language.getString("fileupload"), ScreenshotAction.FILE));

    tray.addSeparator();

    final MenuItem settings = new MenuItem(Language.getString("options"));
    settings.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            if (!openSettings()) {
              icon.displayMessage(
                  Language.getString("error"),
                  Language.getString("optionsOpenError"),
                  TrayIcon.MessageType.ERROR);
            }
          }
        });
    tray.add(settings);

    final MenuItem exit = new MenuItem(Language.getString("exit"));
    exit.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            shutdown();
          }
        });
    tray.add(exit);

    icon =
        new TrayIcon(
            Toolkit.getDefaultToolkit().getImage(Resources.ICON),
            Application.NAME + " v" + Version.getVersionString());
    icon.setPopupMenu(tray);
    icon.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            if (lastUrl != null) {
              try {
                Util.openURL(new URL(lastUrl));
              } catch (final Exception e1) {
                showException(e1, "Unable to open URL");
              }
            }
          }
        });
    try {
      SystemTray.getSystemTray().add(icon);
    } catch (final AWTException e1) {
      this.showException(e1);
    }
  }
Esempio n. 10
0
 /**
  * Initialize the input using JKeymaster
  *
  * @throws JSONException
  */
 public void initializeInput() {
   JSONObject keys = snapper.getConfiguration().getJSONObject("hotkeys");
   // A little sanity check, just in case we don't have ANY keys set
   if (keys == null) {
     return;
   }
   if (keys.has("crop")) {
     provider.register(
         KeyStroke.getKeyStroke(keys.getString("crop")),
         new HotKeyListener() {
           @Override
           public void onHotKey(HotKey hotKey) {
             snapper.crop();
           }
         });
   }
   if (keys.has("full")) {
     provider.register(
         KeyStroke.getKeyStroke(keys.getString("full")),
         new HotKeyListener() {
           @Override
           public void onHotKey(HotKey hotKey) {
             snapper.full();
           }
         });
   }
   if (keys.has("clipboard")) {
     provider.register(
         KeyStroke.getKeyStroke(keys.getString("clipboard")),
         new HotKeyListener() {
           @Override
           public void onHotKey(HotKey hotKey) {
             snapper.clipboard();
           }
         });
   }
   if (keys.has("file")) {
     provider.register(
         KeyStroke.getKeyStroke(keys.getString("file")),
         new HotKeyListener() {
           @Override
           public void onHotKey(HotKey hotKey) {
             snapper.selectFile();
           }
         });
   }
   if (keys.has("options")) {
     provider.register(
         KeyStroke.getKeyStroke(keys.getString("options")),
         new HotKeyListener() {
           @Override
           public void onHotKey(HotKey hotKey) {
             if (!snapper.openSettings()) {
               snapper
                   .getTrayIcon()
                   .displayMessage(
                       "Error",
                       "Could not open settings, is there another window open?",
                       TrayIcon.MessageType.ERROR);
             }
           }
         });
   }
   // We support active windows only on windows/linux, but OSX SOON!
   if ((Platform.isWindows() || Platform.isLinux()) && keys.has("active")) {
     provider.register(
         KeyStroke.getKeyStroke(keys.getString("active")),
         new HotKeyListener() {
           @Override
           public void onHotKey(HotKey hotKey) {
             snapper.active();
           }
         });
   }
   initialized = true;
 }
Esempio n. 11
0
  private PcapNetworkInterface(pcap_if pif, boolean local) {
    this.name = pif.name;
    this.description = pif.description;

    for (pcap_addr pcapAddr = pif.addresses; pcapAddr != null; pcapAddr = pcapAddr.next) {
      if (pcapAddr.addr == null
          && pcapAddr.netmask == null
          && pcapAddr.broadaddr == null
          && pcapAddr.dstaddr == null) {
        logger.warn("Empty pcap_addr on {} ({}). Ignore it.", name, description);
        continue;
      }

      short sa_family =
          pcapAddr.addr != null
              ? pcapAddr.addr.getSaFamily()
              : pcapAddr.netmask != null
                  ? pcapAddr.netmask.getSaFamily()
                  : pcapAddr.broadaddr != null
                      ? pcapAddr.broadaddr.getSaFamily()
                      : pcapAddr.dstaddr != null
                          ? pcapAddr.dstaddr.getSaFamily()
                          /* default */ : Inets.AF_UNSPEC; // Never get here.
      if (sa_family == Inets.AF_INET) {
        addresses.add(PcapIpV4Address.newInstance(pcapAddr, sa_family, name));
      } else if (sa_family == Inets.AF_INET6) {
        addresses.add(PcapIpV6Address.newInstance(pcapAddr, sa_family, name));
      } else {
        if (Platform.isLinux() && sa_family == Inets.AF_PACKET) {
          sockaddr_ll sll = new sockaddr_ll(pcapAddr.addr.getPointer());
          byte[] addr = sll.sll_addr;
          int addrLength = sll.sll_halen & 0xFF;
          if (addrLength == 6) {
            linkLayerAddresses.add(ByteArrays.getMacAddress(addr, 0));
          } else if (addr.length == 0) {
            continue;
          } else {
            linkLayerAddresses.add(
                LinkLayerAddress.getByAddress(ByteArrays.getSubArray(addr, 0, addrLength)));
          }
        } else if ((Platform.isMac() || Platform.isFreeBSD() || Platform.isOpenBSD())
            || Platform.iskFreeBSD() && sa_family == Inets.AF_LINK) {
          sockaddr_dl sdl = new sockaddr_dl(pcapAddr.addr.getPointer());
          byte[] addr = sdl.getAddress();
          if (addr.length == 6) {
            linkLayerAddresses.add(MacAddress.getByAddress(addr));
          } else if (addr.length == 0) {
            continue;
          } else {
            linkLayerAddresses.add(LinkLayerAddress.getByAddress(addr));
          }
        } else {
          logger.warn("{} is not supported address family. Ignore it.", sa_family);
        }
      }
    }

    if (pif.flags == PCAP_IF_LOOPBACK) {
      this.loopBack = true;
    } else {
      this.loopBack = false;
    }

    this.local = local;
  }