View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache Licens e, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.os;
33  
34  import com.sun.jna.Pointer;
35  import com.sun.jna.platform.win32.Kernel32;
36  import com.sun.jna.platform.win32.WinNT;
37  import java.lang.management.ManagementFactory;
38  import java.lang.management.RuntimeMXBean;
39  import java.lang.reflect.Field;
40  import javax.annotation.Signed;
41  import org.spf4j.base.UncheckedExecutionException;
42  
43  /**
44   * PID is exposed in jdk 9... some of this will go away.
45   * @author Zoltan Farkas
46   */
47  public final class ProcessUtil {
48  
49    private static final int PID = parsePid();
50  
51    private ProcessUtil() { }
52  
53    private static int parsePid()  {
54      RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
55      String mxBeanName = runtimeMxBean.getName();
56      int atIdx = mxBeanName.indexOf('@');
57      if (atIdx < 0) {
58        return -1;
59      } else {
60        return Integer.parseInt(mxBeanName.substring(0, atIdx));
61      }
62    }
63  
64    /**
65     * @return the jvm process ID, -1 if it cannot be figured out.
66     */
67    @Signed
68    public static int getPid() {
69      return PID;
70    }
71  
72  
73    public static int getPid(final Process p) {
74      if (OperatingSystem.isWindows()) {
75        return getWindowsPid(p);
76      }
77      return getUnixPid(p);
78    }
79  
80    private static int getUnixPid(final Process p) {
81      try {
82        Field pidF = p.getClass().getDeclaredField("pid");
83        pidF.setAccessible(true);
84        return pidF.getInt(p);
85      } catch (IllegalAccessException | NoSuchFieldException | SecurityException ex) {
86        throw new UncheckedExecutionException("Cannot get PID for " + p, ex);
87      }
88    }
89  
90    private static int getWindowsPid(final Process p) {
91      Field f;
92      long lHandle;
93      try {
94        f = p.getClass().getDeclaredField("handle");
95        f.setAccessible(true);
96        lHandle = f.getLong(p);
97      } catch (NoSuchFieldException | SecurityException | IllegalAccessException ex) {
98        throw new UncheckedExecutionException("Cannot get PID for " + p, ex);
99      }
100 
101     Kernel32 kernel = Kernel32.INSTANCE;
102     WinNT.HANDLE handle = new WinNT.HANDLE();
103     handle.setPointer(Pointer.createConstant(lHandle));
104     return kernel.GetProcessId(handle);
105   }
106 
107 }