Improve PID file creation
This is a followup patch to [1] utilizing built-in tempfile module for creating PID file securely. 1. https://review.openstack.org/#/c/488874 Change-Id: I419e68302995232dd12639d33821444ced9b1498
This commit is contained in:
parent
b3a8a6f532
commit
a944852400
|
@ -15,6 +15,7 @@ from __future__ import print_function
|
|||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
import virtualbmc
|
||||
from virtualbmc import config as vbmc_config
|
||||
|
@ -63,10 +64,12 @@ def main(argv=sys.argv[1:]):
|
|||
if not os.path.exists(dir_name):
|
||||
os.makedirs(dir_name, mode=0o700)
|
||||
|
||||
with open(pid_file, 'w') as f:
|
||||
f.write(str(pid))
|
||||
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(mode='w+t', dir=dir_name,
|
||||
delete=False) as f:
|
||||
f.write(str(pid))
|
||||
os.rename(f.name, pid_file)
|
||||
|
||||
func()
|
||||
|
||||
except Exception as e:
|
||||
|
|
Loading…
Reference in New Issue