[ovs-dev] [PATCH V8] windows: Added lockf function and lock PID file

Paul Boca pboca at cloudbasesolutions.com
Mon Jul 25 11:53:47 UTC 2016


Hi!

Disregard this patch, I will send another one that addresses all the required changes.

Thanks,
Paul

> -----Original Message-----
> From: dev [mailto:dev-bounces at openvswitch.org] On Behalf Of Paul Boca
> Sent: Friday, July 15, 2016 9:45 AM
> To: dev at openvswitch.org
> Subject: [ovs-dev] [PATCH V8] windows: Added lockf function and lock PID
> file
> 
> If the PID file isn't locked then appctl.py detects it as stale and
> bails out without doing anything. Because of this lots of Python tests fail.
> Also this protects the PID file from being overwritten.
> 
> I used only shared lock, in order to be compatible with Python tests,
> which try to acquire the lock exclusively. On Windows if the exclusive lock
> is used, than the read access is denied too for other instances of this file.
> 
> Signed-off-by: Paul-Daniel Boca <pboca at cloudbasesolutions.com>
> Acked-by: Alin Gabriel Serdean <aserdean at cloudbasesolutions.com>
> ---
> V2: No changes
> V3: No changes
> V4: No changes
> V5: Removed this patch from Python series.
>     Removed unused constants and defined 0xFFFF0000 as constant.
>     Updated commit text.
> V6: flock returns now the error of GetLastError.
>     Also it uses fd parameter instead of the global filep_pidfile.
>     Removed unused local variable and unnecessary error message.
> V7: Keep a shared lock on PID file after the fopen.
>     This ensures us that at most one process can write the same PID file.
>     fopen with 'w' on Windows creates a file with FILE_SHARED_WRITE
>     flag, so anyone with write access on file can write it.
> V8: Added LOCKFILE_FAIL_IMMEDIATELY flag when trying to get exclusive
> lock
>     on PID file in order to avoid hangs of other daemons that try to acquire
>     exclusive lock over the same PID file
> ---
>  lib/daemon-windows.c | 44
> ++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 44 insertions(+)
> 
> diff --git a/lib/daemon-windows.c b/lib/daemon-windows.c
> index 8cf0fea..350e8fc 100644
> --- a/lib/daemon-windows.c
> +++ b/lib/daemon-windows.c
> @@ -18,6 +18,7 @@
>  #include "daemon.h"
>  #include "daemon-private.h"
>  #include <stdio.h>
> +#include <io.h>
>  #include <stdlib.h>
>  #include "dirs.h"
>  #include "ovs-thread.h"
> @@ -26,6 +27,14 @@
> 
>  VLOG_DEFINE_THIS_MODULE(daemon_windows);
> 
> +/* Constants for flock function */
> +#define	LOCK_SH	0x0                         /* Shared lock. */
> +#define	LOCK_EX	LOCKFILE_EXCLUSIVE_LOCK     /* Exclusive
> lock. */
> +#define	LOCK_UN	0x80000000                  /* Unlock. Custom
> value. */
> +
> +/* High-order 32 bits of byte range to lock */
> +#define LOCK_MAX_SIZE 0xFFFF0000
> +
>  static bool service_create;          /* Was --service specified? */
>  static bool service_started;         /* Have we dispatched service to start? */
> 
> @@ -404,9 +413,38 @@ detach_process(int argc, char *argv[])
>  }
> 
>  static void
> +flock(FILE* fd, int operation)
> +{
> +    HANDLE hFile;
> +    OVERLAPPED ov = {0};
> +
> +    hFile = (HANDLE)_get_osfhandle(fileno(fd));
> +    if (hFile == INVALID_HANDLE_VALUE) {
> +        VLOG_FATAL("Invalid handle value");
> +    }
> +
> +    if (operation & LOCK_UN) {
> +        if (UnlockFileEx(hFile, 0, 0, LOCK_MAX_SIZE, &ov) == 0) {
> +            VLOG_FATAL("Failed to unlock PID file (%s).",
> +                       ovs_lasterror_to_string());
> +        }
> +    } else {
> +       /* Use LOCKFILE_FAIL_IMMEDIATELY flag to avoid hang of another
> daemon that tries to
> +           acquire exclusive lock over the same PID file */
> +        if (LockFileEx(hFile, operation | LOCKFILE_FAIL_IMMEDIATELY, 0, 0,
> +                       LOCK_MAX_SIZE, &ov) == FALSE) {
> +            VLOG_FATAL("Failed to lock PID file (%s).",
> +                       ovs_lasterror_to_string());
> +        }
> +    }
> +}
> +
> +static void
>  unlink_pidfile(void)
>  {
>      if (filep_pidfile) {
> +        /* Remove the shared lock on file */
> +        flock(filep_pidfile, LOCK_UN);
>          fclose(filep_pidfile);
>      }
>      if (pidfile) {
> @@ -437,6 +475,8 @@ make_pidfile(void)
>          VLOG_FATAL("failed to open %s (%s)", pidfile, ovs_strerror(errno));
>      }
> 
> +    flock(filep_pidfile, LOCK_EX);
> +
>      fatal_signal_add_hook(unlink_pidfile, NULL, NULL, true);
> 
>      fprintf(filep_pidfile, "%d\n", _getpid());
> @@ -444,6 +484,10 @@ make_pidfile(void)
>          VLOG_FATAL("Failed to write into the pidfile %s", pidfile);
>      }
> 
> +    flock(filep_pidfile, LOCK_SH);
> +    /* This will remove the exclusive lock. The shared lock will remain */
> +    flock(filep_pidfile, LOCK_UN);
> +
>      /* Don't close the pidfile till the process exits. */
>  }
> 
> --
> 2.7.2.windows.1
> _______________________________________________
> dev mailing list
> dev at openvswitch.org
> http://openvswitch.org/mailman/listinfo/dev


More information about the dev mailing list