1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 package it.jnrpe.installer;
17
18 import java.io.InputStream;
19 import java.nio.charset.Charset;
20
21 public class InstallerUtil {
22
23 public final static boolean ROOT = _init();
24
25 private static boolean _init() {
26
27 try {
28 Process p = Runtime.getRuntime().exec("id -u");
29 p.waitFor();
30
31 byte[] buff = new byte[50];
32 InputStream in = p.getInputStream();
33
34 StringBuffer res = new StringBuffer();
35 int iCount;
36
37 while ((iCount = in.read(buff)) > 0) {
38 // The default charset must be used...
39 res.append(new String(buff, 0, iCount, Charset.defaultCharset()));
40 }
41
42 return res.toString().trim().equals("0");
43 } catch (Exception e) {
44 e.printStackTrace();
45 }
46
47 return false;
48 }
49 }