modules/jdk.packager/src/main/native/library/common/LinuxPlatform.cpp

Print this page


   1 /*
   2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR


  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 
  34 #include "Platform.h"
  35 
  36 #ifdef LINUX
  37 
  38 #include "JavaVirtualMachine.h"
  39 #include "LinuxPlatform.h"
  40 #include "PlatformString.h"
  41 
  42 
  43 #include <stdlib.h>
  44 #include <pwd.h>








  45 
  46 
  47 TString GetEnv(const TString &name) {
  48     TString result;
  49 
  50     char *value = ::getenv((TCHAR*)name.c_str());
  51 
  52     if (value != NULL) {
  53        result = value;
  54     }
  55 
  56     return result;
  57 }
  58 
  59 LinuxPlatform::LinuxPlatform(void) : Platform(), GenericPlatform(), PosixPlatform() {
  60     FMainThread = pthread_self();
  61 }
  62 
  63 LinuxPlatform::~LinuxPlatform(void) {
  64 }


 178     TString jreHome = GetSystemJRE();
 179 
 180     if (jreHome.empty() == false && FilePath::DirectoryExists(jreHome) == true) {
 181         result = FilePath::IncludeTrailingSeparater(jreHome) +
 182             _T("/lib/"JAVAARCH"/client/libjvm.so");
 183 
 184         if (FilePath::FileExists(result) == false) {
 185             result = FilePath::IncludeTrailingSeparater(jreHome) +
 186                 _T("/lib/"JAVAARCH"/server/libjvm.so");
 187         }
 188     }
 189 
 190     return result;
 191 }
 192 
 193 bool LinuxPlatform::IsMainThread() {
 194     bool result = (FMainThread == pthread_self());
 195     return result;
 196 }
 197 















 198 TPlatformNumber LinuxPlatform::GetMemorySize() {
 199     long pages = sysconf(_SC_PHYS_PAGES);
 200     long page_size = sysconf(_SC_PAGE_SIZE);
 201     TPlatformNumber result = pages * page_size;
 202     result = result / 1048576; // Convert from bytes to megabytes.
 203     return result;
 204 }
 205 
 206 #ifdef DEBUG
 207 bool LinuxPlatform::IsNativeDebuggerPresent() {
 208     // gdb opens file descriptors stdin=3, stdout=4, stderr=5 whereas
 209     // a typical prog uses only stdin=0, stdout=1, stderr=2.
 210     bool result = false;
 211     FILE *fd = fopen("/tmp", "r");
 212 
 213     if (fileno(fd) > 5) {
 214         result = true;
 215     }
 216 
 217     fclose(fd);


1086                 }
1087             }
1088         }
1089     }
1090 
1091     return result;
1092 }
1093 
1094 bool LinuxJavaUserPreferences::Load(TString Appid) {
1095     bool result = false;
1096     TString filename = GetUserPrefFileName(Appid);
1097 
1098     if (FilePath::FileExists(filename) == true) {
1099         FMap = GetJvmUserArgs(filename);
1100         result = true;
1101     }
1102 
1103     return result;
1104 }
1105 






























































1106 //--------------------------------------------------------------------------------------------------
1107 
1108 #endif // LINUX
   1 /*
   2  * Copyright (c) 2014, 2017, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR


  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 
  34 #include "Platform.h"
  35 
  36 #ifdef LINUX
  37 
  38 #include "JavaVirtualMachine.h"
  39 #include "LinuxPlatform.h"
  40 #include "PlatformString.h"
  41 
  42 
  43 #include <stdlib.h>
  44 #include <pwd.h>
  45 #include <sys/file.h>
  46 #include <sys/stat.h>
  47 #include <errno.h>
  48 #include <unistd.h>
  49 #include <sys/types.h>
  50 #include <limits.h>
  51 
  52 #define LINUX_PACKAGER_TMP_DIR "/.java/packager/tmp"
  53 
  54 
  55 TString GetEnv(const TString &name) {
  56     TString result;
  57 
  58     char *value = ::getenv((TCHAR*)name.c_str());
  59 
  60     if (value != NULL) {
  61        result = value;
  62     }
  63 
  64     return result;
  65 }
  66 
  67 LinuxPlatform::LinuxPlatform(void) : Platform(), GenericPlatform(), PosixPlatform() {
  68     FMainThread = pthread_self();
  69 }
  70 
  71 LinuxPlatform::~LinuxPlatform(void) {
  72 }


 186     TString jreHome = GetSystemJRE();
 187 
 188     if (jreHome.empty() == false && FilePath::DirectoryExists(jreHome) == true) {
 189         result = FilePath::IncludeTrailingSeparater(jreHome) +
 190             _T("/lib/"JAVAARCH"/client/libjvm.so");
 191 
 192         if (FilePath::FileExists(result) == false) {
 193             result = FilePath::IncludeTrailingSeparater(jreHome) +
 194                 _T("/lib/"JAVAARCH"/server/libjvm.so");
 195         }
 196     }
 197 
 198     return result;
 199 }
 200 
 201 bool LinuxPlatform::IsMainThread() {
 202     bool result = (FMainThread == pthread_self());
 203     return result;
 204 }
 205 
 206 const char* LinuxPlatform::getTmpDirString() {
 207     return LINUX_PACKAGER_TMP_DIR;
 208 }
 209 
 210 void LinuxPlatform::reactivateAnotherInstance() {
 211     if (singleInstanceProcessId == 0) {
 212         printf("Unable to reactivate another instance, PID is undefined");
 213         return;
 214     } 
 215     Display* dsp = XOpenDisplay(NULL);
 216     ProcessReactivator processReactivator(dsp, singleInstanceProcessId);
 217     processReactivator.reactivateProcess();
 218     XCloseDisplay(dsp);
 219 }
 220 
 221 TPlatformNumber LinuxPlatform::GetMemorySize() {
 222     long pages = sysconf(_SC_PHYS_PAGES);
 223     long page_size = sysconf(_SC_PAGE_SIZE);
 224     TPlatformNumber result = pages * page_size;
 225     result = result / 1048576; // Convert from bytes to megabytes.
 226     return result;
 227 }
 228 
 229 #ifdef DEBUG
 230 bool LinuxPlatform::IsNativeDebuggerPresent() {
 231     // gdb opens file descriptors stdin=3, stdout=4, stderr=5 whereas
 232     // a typical prog uses only stdin=0, stdout=1, stderr=2.
 233     bool result = false;
 234     FILE *fd = fopen("/tmp", "r");
 235 
 236     if (fileno(fd) > 5) {
 237         result = true;
 238     }
 239 
 240     fclose(fd);


1109                 }
1110             }
1111         }
1112     }
1113 
1114     return result;
1115 }
1116 
1117 bool LinuxJavaUserPreferences::Load(TString Appid) {
1118     bool result = false;
1119     TString filename = GetUserPrefFileName(Appid);
1120 
1121     if (FilePath::FileExists(filename) == true) {
1122         FMap = GetJvmUserArgs(filename);
1123         result = true;
1124     }
1125 
1126     return result;
1127 }
1128 
1129 ProcessReactivator::ProcessReactivator(Display* display, pid_t pid):
1130    _pid(pid), _display(display) {
1131 
1132    _atomPid = XInternAtom(display, "_NET_WM_PID", True);
1133 
1134    if (_atomPid == None) {
1135        return;
1136    }
1137    searchWindowHelper(XDefaultRootWindow(display));
1138 }
1139 
1140 void ProcessReactivator::searchWindowHelper(Window w) {
1141     Atom type;
1142     int format;
1143     unsigned long  num, bytesAfter;
1144     unsigned char* propPid = 0;
1145     if (Success == XGetWindowProperty(_display, w, _atomPid, 0, 1, False, XA_CARDINAL,
1146                                      &type, &format, &num, &bytesAfter, &propPid)) {
1147         if (propPid != 0) {
1148             if (_pid == *((pid_t *)propPid)) {
1149                 _result.push_back(w);
1150             }
1151             XFree(propPid);
1152         }
1153     }
1154 
1155     Window root, parent;
1156     Window* child;
1157     unsigned int numChildren;
1158     if (0 != XQueryTree(_display, w, &root, &parent, &child, &numChildren)) {
1159         for (unsigned int i = 0; i < numChildren; i++) {
1160             searchWindowHelper(child[i]);
1161         }
1162     }
1163 }
1164 
1165 void ProcessReactivator::reactivateProcess() {
1166     for (std::list<Window>::const_iterator it = _result.begin(); it != _result.end(); it++) {
1167         // try sending an event to activate window,
1168         // after that we can try to raise it.
1169         XEvent xev;
1170         Atom atom = XInternAtom (_display, "_NET_ACTIVE_WINDOW", False);
1171         xev.xclient.type = ClientMessage;
1172         xev.xclient.serial = 0;
1173         xev.xclient.send_event = True;
1174         xev.xclient.display = _display;
1175         xev.xclient.window = *it;
1176         xev.xclient.message_type = atom;
1177         xev.xclient.format = 32;
1178         xev.xclient.data.l[0] = 2;
1179         xev.xclient.data.l[1] = 0;
1180         xev.xclient.data.l[2] = 0;
1181         xev.xclient.data.l[3] = 0;
1182         xev.xclient.data.l[4] = 0;
1183         XWindowAttributes attr;
1184         XGetWindowAttributes(_display, *it, &attr);
1185         XSendEvent(_display, attr.root, False,
1186             SubstructureRedirectMask | SubstructureNotifyMask, &xev);
1187         XRaiseWindow(_display, *it);
1188     }
1189 }
1190 
1191 //--------------------------------------------------------------------------------------------------
1192 
1193 #endif // LINUX