Josh Soref abce91d14b
Spelling (#12954)
* link: Collect WSL logs (recommended method)

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* link: Advanced Authoring Tests in C++

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* link: CMake Documentation and Community

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* link: Collect WSL logs for networking issues

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* link: Collect WSL logs (recommended method)

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: ; otherwise,

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: a

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: access

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: accessible

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: across

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: actively

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: adapters

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: address

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: addresses

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: and

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: appropriate

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: argument

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: associated

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: attach

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: available

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: beginning

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: between

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: binaries

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: bound

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: buffer

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: buffers

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: cannot

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: canonical

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: capabilities

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: case-insensitive

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: case-sensitive

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: certified

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: command

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: committer

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: communication

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: complains

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: configuration

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: consumed

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: continue

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: converted

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: currently

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: customers

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: daemon

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: deferred

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: definitions

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: delimiter

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: delivered

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: dellink

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: derived

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: descriptor

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: destined

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: destruct

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: destructible

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: destructor

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: detach

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: differentiate

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: directories

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: disassociate

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: disposition

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: distribution

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: distro

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: duping

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: emitted

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: empty

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: environment

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: every time

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: exclusive

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: expected

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: expire

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: explicitly

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: fall back

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: false

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: fastfail

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: filesystem

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: first

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: followed

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: for

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: functionality

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: functionally

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: github

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: greater

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: guarantee

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: guaranteed

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: handles

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: hangup

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: hierarchy

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: hogwarts

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: hydrated

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: icrnl

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: implementation

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: implementing

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: initialize

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: instance

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: instantiate

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: instantiations

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: intentionally

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: interpret

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: interpreter

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: irreversibly

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: iteration

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: iterator

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: its

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: kernel

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: kmsg

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: knowledge

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: maximum

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: mirrored

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: msftconnecttest

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: multi

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: multiple

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: mutable

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: namespace

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: nonexistent

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: notifications

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: occurred

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: occurring

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: otherwise,

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: outstanding

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: overridden

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: partition

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: pass through

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: passthrough

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: performs

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: periodically

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: positional

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: precedence

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: preexisting

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: preferring

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: prepopulate

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: previous

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: privileges

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: process

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: processes

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: programmatically

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: protection

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: provided

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: reasonable

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: receive

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: received

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: red hat

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: reentrant

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: registered

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: regularly

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: relay

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: release

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: representing

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: requests

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: response

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: resurrect

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: retention

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: returned

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: security

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: semaphore

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: separate

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: separator

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: service

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: set up

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: setup

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: severely

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: should

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: signal

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: similarly

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: simple

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: simplified

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: single

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: specified

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: splitting

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: standard

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: stress

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: succeed

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: success

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: successfully

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: supplementary

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: synced

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: system

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: take

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: than

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: that opening

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: the

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: threadpool

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: to

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: true

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: truncate

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: tunneling

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: unexpected

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: uninitialize

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: unique

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: unprivileged

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: unregistered

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: untrusted

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: upgrade

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: utility

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: validating

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: variant

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: variation

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: variations

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: verify

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: visible

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: whether

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: winget

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: worker

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: written

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* spelling: wslservice

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

* format source

---------

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: Ben Hillis <benhillis@gmail.com>
Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com>
2025-07-23 15:19:00 -07:00

537 lines
12 KiB
C

/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
flock.c
Abstract:
This file is a flock test.
--*/
#include "lxtcommon.h"
#include "unittests.h"
#include <sys/file.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define LXT_NAME "Flock"
int FnctlLockingVariation0(PLXT_ARGS Args);
int FlockVariation0(PLXT_ARGS Args);
//
// Global constants
//
static const LXT_VARIATION g_LxtVariations[] = {{"Flock", FlockVariation0}, {"Fcntl Locking", FnctlLockingVariation0}};
int FlockTestEntry(int Argc, char* Argv[])
/*++
--*/
{
LXT_ARGS Args;
int Result;
LxtCheckResult(LxtInitialize(Argc, Argv, &Args, LXT_NAME));
LxtCheckResult(LxtRunVariations(&Args, g_LxtVariations, LXT_COUNT_OF(g_LxtVariations)));
ErrorExit:
LxtUninitialize();
return !LXT_SUCCESS(Result);
}
int FnctlLockingVariation0(PLXT_ARGS Args)
/*++
Routine Description:
This routine tests file locking through the FCNTL system call.
Arguments:
Args - Supplies a pointer to the test arguments.
Return Value:
0 on success, -1 on failure.
--*/
{
char Buffer[10];
char ByteAlignedBuffer[sizeof(struct flock) + sizeof(struct flock64)];
int ChildPid;
int FileDescriptor;
struct flock* LockDescriptor;
struct flock64* LockDescriptor64;
int Result;
FileDescriptor = -1;
ChildPid = -1;
//
// Initialize the file for this test.
//
LxtCheckErrno(FileDescriptor = open("/data/test/fcntl_lock_test.bin", O_RDWR | O_CREAT, S_IRWXU));
LxtCheckErrno(write(FileDescriptor, Buffer, 10));
//
// Set the lock descriptor.
//
memset(ByteAlignedBuffer, 0, sizeof(ByteAlignedBuffer));
LockDescriptor = (struct flock*)&ByteAlignedBuffer[1];
LockDescriptor->l_type = F_WRLCK;
LockDescriptor->l_whence = SEEK_SET;
LockDescriptor->l_start = 0;
LockDescriptor->l_len = 10;
//
// Get the current lock state.
//
LxtLogInfo("Fnctl locking - Checking that lock can be set.");
LxtCheckErrno(fcntl(FileDescriptor, F_GETLK, LockDescriptor));
LxtCheckEqual(LockDescriptor->l_type, F_UNLCK, "%x");
//
// Set the lock.
//
LxtLogInfo("Fcntl locking - Setting the read lock by the parent process");
memset(ByteAlignedBuffer, 0, sizeof(ByteAlignedBuffer));
LockDescriptor->l_type = F_RDLCK;
LxtCheckErrno(fcntl(FileDescriptor, F_SETLK, LockDescriptor));
//
// Now change the lock to be a write lock.
//
memset(ByteAlignedBuffer, 0, sizeof(ByteAlignedBuffer));
LockDescriptor64 = (struct flock64*)&ByteAlignedBuffer[1];
LockDescriptor64->l_type = F_WRLCK;
LockDescriptor64->l_whence = SEEK_SET;
LockDescriptor64->l_start = 0;
LockDescriptor64->l_len = 10;
//
// Set the lock.
//
LxtLogInfo("Fcntl locking - Setting the write lock with 64 bit set lock");
LxtCheckErrno(fcntl(FileDescriptor, F_SETLK64, LockDescriptor64));
//
// Fork the process.
//
LxtLogInfo("Creating child process to test the lock.");
LxtCheckErrno(ChildPid = fork());
if (ChildPid == 0)
{
//
// Ensure that the lock was correctly set before. The lock descriptor is
// correctly set from before. The test expects to see the lock type be
// changed to exclusive, WRLCK, though.
//
LxtLogInfo("Fcntl child locking - Reading lock type");
LxtCheckErrno(fcntl(FileDescriptor, F_GETLK, LockDescriptor));
LxtCheckEqual(LockDescriptor->l_type, F_WRLCK, "%X");
Result = LXT_RESULT_SUCCESS;
goto ErrorExit;
}
Result = LxtWaitPidPoll(ChildPid, LXT_RESULT_SUCCESS);
ErrorExit:
if (FileDescriptor != 0)
{
close(FileDescriptor);
}
if (ChildPid == 0)
{
_exit(Result);
}
return Result;
}
int FlockVariation0(PLXT_ARGS Args)
/*++
--*/
{
int Result;
int FileDescriptor1;
int FileDescriptor2;
int FileDescriptor3;
int DupedDescriptor;
int ChildPid;
int Index;
//
// Initialize locals.
//
FileDescriptor1 = -1;
FileDescriptor2 = -1;
FileDescriptor3 = -1;
DupedDescriptor = -1;
ChildPid = -1;
//
// Open a file that will be locked.
//
FileDescriptor1 = open("/data/test/flock_test.bin", O_RDWR | O_CREAT, S_IRWXU);
if (FileDescriptor1 == -1)
{
Result = errno;
LxtLogError("Could not create test file! %d", Result);
goto cleanup;
}
FileDescriptor2 = open("/data/test/flock_test.bin", O_RDWR | O_CREAT, S_IRWXU);
if (FileDescriptor2 == -1)
{
Result = errno;
LxtLogError("Could not create test file! %d", Result);
goto cleanup;
}
Result = flock(FileDescriptor1, LOCK_EX);
if (Result < 0)
{
Result = errno;
LxtLogError("Lock failed! %d", Result);
goto cleanup;
}
FileDescriptor3 = open("/data/test/flock_test.bin", O_RDWR | O_CREAT, S_IRWXU);
if (FileDescriptor3 == -1)
{
Result = errno;
LxtLogError("Could not create test file! %d", Result);
goto cleanup;
}
//
// Lock the file from another descriptor (non-blocking) and it should fail
// accordingly.
//
Result = flock(FileDescriptor2, LOCK_EX | LOCK_NB);
if (Result == 0)
{
Result = -1;
LxtLogError("Lock succeeded but should have failed!");
goto cleanup;
}
Result = errno;
if (Result != EWOULDBLOCK)
{
LxtLogError("Lock failed but with wrong error! %d", Result);
goto cleanup;
}
//
// Dupe the owner descriptor and lock the file shared. This should convert
// the file to shared.
//
DupedDescriptor = dup(FileDescriptor1);
if (DupedDescriptor < 0)
{
Result = errno;
LxtLogError("Could not dup the descriptor! %d", Result);
goto cleanup;
}
for (Index = 0; Index < 2; Index += 1)
{
Result = flock(DupedDescriptor, LOCK_EX);
if (Result < 0)
{
Result = errno;
LxtLogError("Lock exclusive conversion failed! %d", Result);
goto cleanup;
}
Result = flock(DupedDescriptor, LOCK_SH);
if (Result < 0)
{
Result = errno;
LxtLogError("Lock shared conversion failed! %d", Result);
goto cleanup;
}
}
//
// The lock is now owned shared by descriptor1 (via duped descriptor) so now
// descriptor 2 should be able to acquire it shared.
//
Result = flock(FileDescriptor2, LOCK_SH | LOCK_NB);
if (Result < 0)
{
Result = errno;
LxtLogError("Lock shared failed for descriptor2! %d", Result);
goto cleanup;
}
//
// Unlock via descriptor1. That leaves just descriptor2 shared.
//
Result = flock(FileDescriptor1, LOCK_UN);
if (Result < 0)
{
Result = errno;
LxtLogError("Unlock failed for descriptor1! %d", Result);
goto cleanup;
}
//
// Fork to create another thread to wait for lock.
//
for (Index = 0; Index < 2; Index += 1)
{
ChildPid = fork();
if (ChildPid == -1)
{
Result = errno;
LxtLogError("Fork failed! %d", Result);
goto cleanup;
}
if (ChildPid == 0)
{
int FileDescriptor;
if (Index == 0)
{
FileDescriptor = FileDescriptor1;
}
else
{
FileDescriptor = FileDescriptor3;
}
//
// Wait for exclusive lock.
//
close(FileDescriptor2);
FileDescriptor2 = -1;
LxtLogInfo("C%u: Waiting for lock on FileDescriptor...", Index);
Result = flock(FileDescriptor, LOCK_EX);
if (Result < 0)
{
Result = errno;
LxtLogError("Lock acquire failed for descriptor1! %d", Result);
goto cleanup;
}
LxtLogInfo("C%u: Lock acquired on FileDescriptor...", Index);
Result = flock(FileDescriptor, LOCK_UN);
if (Result < 0)
{
Result = errno;
LxtLogError("Unlock failed for descriptor1! %d", Result);
goto cleanup;
}
LxtLogInfo("C%u: Sleeping 3 secs...", Index);
usleep(3 * 1000 * 1000);
LxtLogInfo("C%u: Waiting for lock shared on FileDescriptor...", Index);
Result = flock(FileDescriptor, LOCK_SH);
if (Result < 0)
{
Result = errno;
LxtLogError("Lock acquire failed for descriptor1! %d", Result);
goto cleanup;
}
LxtLogInfo("C%u: Lock acquired on FileDescriptor...", Index);
Result = flock(FileDescriptor, LOCK_UN);
if (Result < 0)
{
Result = errno;
LxtLogError("Unlock failed for descriptor1! %d", Result);
goto cleanup;
}
if (Index == 0)
{
Result = LXT_RESULT_SUCCESS;
goto cleanup;
}
LxtLogInfo("C%u: Sleeping 3 secs...", Index);
usleep(3 * 1000 * 1000);
LxtLogInfo("C%u: Waiting for lock exclusive to be terminated...", Index);
Result = flock(FileDescriptor, LOCK_EX);
if (Result == 0)
{
Result = -1;
LxtLogError("Lock acquisition succeeded but EINTR expected!");
goto cleanup;
}
Result = errno;
if (Result != EINTR)
{
LxtLogError("Lock acquisition failed but not with EINTR! %d", Result);
goto cleanup;
}
Result = LXT_RESULT_SUCCESS;
goto cleanup;
}
}
LxtLogInfo("P: Waiting 3 seconds before releasing lock shared...");
usleep(3 * 1000 * 1000);
Result = flock(FileDescriptor2, LOCK_UN);
if (Result < 0)
{
Result = errno;
LxtLogError("Unlock failed for descriptor2! %d", Result);
goto cleanup;
}
usleep(1 * 1000 * 1000);
LxtLogInfo("P: Waiting to acquire lock exclusive...");
Result = flock(FileDescriptor2, LOCK_EX);
if (Result < 0)
{
Result = errno;
LxtLogError("Lock exclusive failed for descriptor2! %d", Result);
goto cleanup;
}
LxtLogInfo("P: Acquired lock exclusive.");
LxtLogInfo("P: Sleeping 5 secs...");
usleep(5 * 1000 * 1000);
LxtLogInfo("P: Releasing lock exclusive...");
Result = flock(FileDescriptor2, LOCK_UN);
if (Result < 0)
{
Result = errno;
LxtLogError("Unlock failed for descriptor2! %d", Result);
goto cleanup;
}
LxtLogInfo("P: Waiting to acquire lock shared...");
Result = flock(FileDescriptor2, LOCK_SH);
if (Result < 0)
{
Result = errno;
LxtLogError("Lock shared failed for descriptor2! %d", Result);
goto cleanup;
}
LxtLogInfo("P: Sleeping 5 secs...");
usleep(5 * 1000 * 1000);
//
// Force the child's file lock wait to be interrupted by a signal.
//
kill(ChildPid, SIGKILL);
Result = LXT_RESULT_SUCCESS;
cleanup:
if (FileDescriptor1 != -1)
{
close(FileDescriptor1);
}
if (FileDescriptor2 != -1)
{
close(FileDescriptor2);
}
if (DupedDescriptor != -1)
{
close(DupedDescriptor);
}
if (ChildPid == 0)
{
_exit(0);
}
return Result;
}