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:
Ilya Etingof 2018-06-15 17:39:57 +02:00
parent b3a8a6f532
commit a944852400
1 changed files with 6 additions and 3 deletions

View File

@ -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: