aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/util/ProcessUtils.java
blob: ec687365ca897dae2bacfb708b984ff47fe4d4af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.util;

import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;

/**
 * OS Process related utilities.
 *
 * <p>Default implementation forwards all requests to
 * {@link com.google.devtools.build.lib.unix.ProcessUtils}. The default implementation
 * can be overridden by {@code #setImplementation(ProcessUtilsImpl)} method.
 */
@ThreadSafe
public final class ProcessUtils {

  /**
   * Describes implementation to which all {@code ProcessUtils} requests are
   * forwarded.
   */
  public interface ProcessUtilsImpl {
    /** @see ProcessUtils#getgid() */
    int getgid();

    /** @see ProcessUtils#getpid() */
    int getpid();

    /** @see ProcessUtils#getuid() */
    int getuid();
  }

  private volatile static ProcessUtilsImpl implementation = new ProcessUtilsImpl() {

    @Override
    public int getgid() {
      return com.google.devtools.build.lib.unix.ProcessUtils.getgid();
    }

    @Override
    public int getpid() {
      return com.google.devtools.build.lib.unix.ProcessUtils.getpid();
    }

    @Override
    public int getuid() {
      return com.google.devtools.build.lib.unix.ProcessUtils.getuid();
    }
  };

  private ProcessUtils() {
    // prevent construction.
  }

  /**
   * @return the real group ID of the current process.
   */
  public static int getgid() {
    return implementation.getgid();
  }

  /**
   * @return the process ID of this process.
   */
  public static int getpid() {
    return implementation.getpid();
  }

  /**
   * @return the real user ID of the current process.
   */
  public static int getuid() {
    return implementation.getuid();
  }
}