[PATCH] Only allow one anaconda instance (#1146735)

Brian C. Lane bcl at redhat.com
Tue Oct 7 22:59:57 UTC 2014


On Tue, Oct 07, 2014 at 05:22:46PM -0400, David Shea wrote:
> If the PID file is stale, remove it and retry.  Display a graphical
> error dialog if we're in the sort of environment that can display
> graphical dialogs early on, otherwise just print a message and bomb out.
> ---
>  anaconda | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
>  1 file changed, 85 insertions(+), 4 deletions(-)
> 
> diff --git a/anaconda b/anaconda
> index beeaabd..d49dea5 100755
> --- a/anaconda
> +++ b/anaconda
> @@ -44,7 +44,7 @@ if ("debug=1" in proc_cmdline) or ("debug" in proc_cmdline):
>      cov.start()
>  
>  
> -import atexit, sys, os, time, signal
> +import atexit, sys, os, time, signal, stat, errno
>  
>  def exitHandler(rebootData, storage):
>      # Clear the list of watched PIDs.
> @@ -81,6 +81,10 @@ def exitHandler(rebootData, storage):
>          uninhibit_screensaver(anaconda.dbus_session_connection, anaconda.dbus_inhibit_id)
>          anaconda.dbus_inhibit_id = None
>  
> +    # Clean up the PID file
> +    if pidfile_created:
> +        os.unlink(pidfile_path)
> +
>      if not flags.imageInstall and not flags.livecdInstall \
>         and not flags.dirInstall:
>          from pykickstart.constants import KS_SHUTDOWN, KS_WAIT
> @@ -98,6 +102,18 @@ def exitHandler(rebootData, storage):
>          else:  # reboot action is KS_REBOOT or None
>              execWithRedirect("systemctl", ["--no-wall", "reboot"])
>  
> +def createPIDFile():
> +    # If the PID file already exists, this method will raise OSError with
> +    # errno == EEXIST
> +    global pidfile_created
> +
> +    # Set all of the read/write bits and let umask make it make sense
> +    pidfile = os.open(pidfile_path, os.O_WRONLY|os.O_CREAT|os.O_EXCL,
> +            stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IWGRP|stat.S_IROTH|stat.S_IWOTH)
> +    pidfile_created = True
> +    os.write(pidfile, "%s\n" % os.getpid())
> +    os.close(pidfile)
> +
>  def startSpiceVDAgent():
>      status = iutil.execWithRedirect("spice-vdagent", [])
>  
> @@ -841,9 +857,74 @@ if __name__ == "__main__":
>      # make sure we have /var/log soon, some programs fail to start without it
>      iutil.mkdirChain("/var/log")
>  
> -    pidfile = open("/var/run/anaconda.pid", "w")
> -    pidfile.write("%s\n" % (os.getpid(),))
> -    del pidfile
> +    # Share these with the exit handler, installed later
> +    pidfile_path = '/var/run/anaconda.pid'
> +    pidfile_created = False
> +
> +    # Attempt to create the pidfile
> +    try:
> +        createPIDFile()
> +    except OSError as e:
> +        # If the failure was anything other than EEXIST during the open call,
> +        # just re-raise the exception
> +        if e.errno != errno.EEXIST:
> +            raise
> +
> +        log.error("%s already exists", pidfile_path)
> +        retry_pid = False
> +

This could be a little cleaner if you dropped the first attempt to
create the PID. Try reading first, check for stale, then do the
remove/creation.

-- 
Brian C. Lane | Anaconda Team | IRC: bcl #anaconda | Port Orchard, WA (PST8PDT)


More information about the anaconda-patches mailing list