]> begriffs open source - cmsis-freertos/blob - Test/litani/lib/pid_file.py
Updated pack to FreeRTOS 10.4.6
[cmsis-freertos] / Test / litani / lib / pid_file.py
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License").
4 # You may not use this file except in compliance with the License.
5 # A copy of the License is located at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # or in the "license" file accompanying this file. This file is distributed
10 # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 # express or implied. See the License for the specific language governing
12 # permissions and limitations under the License.
13
14 """Write the current process ID to a file, or read a PID from that file.
15
16 This is used so that one Litani process can write its PID, and another process
17 can then read the PID to send POSIX signals to the first process (see also
18 signal(3) on macOS or signal(7) on Linux).
19 """
20
21 import os
22
23 import lib.litani
24
25
26 _NAME = "run-pid"
27
28
29 def read():
30     cache_dir = lib.litani.get_cache_dir()
31     with open(cache_dir / _NAME) as handle:
32         return int(handle.read().strip())
33
34
35 def write():
36     pid = os.getpid()
37     cache_dir = lib.litani.get_cache_dir()
38     with lib.litani.atomic_write(cache_dir / _NAME) as handle:
39         print(str(pid), file=handle)