5.2 Opening a File

peios_file_open requests an explicit access mask and creation policy. The returned descriptor represents the access granted by the kernel. The kernel native-open guide describes the kernel's validation and file creation behavior.

struct peios_open_params {
    uint32_t    desired_access; /* KACS_FILE_* | standard | generic | MAXIMUM_ALLOWED */
    uint32_t    disposition;    /* KACS_DISPOSITION_* */
    uint32_t    options;        /* KACS_CREATE_OPT_* */
    uint32_t    flags;          /* AT_SYMLINK_NOFOLLOW | KACS_BACKUP_INTENT | KACS_RESTORE_INTENT |
                                   KACS_OPEN_SHARE_ACCESS | KACS_OPEN_LEASE_KEY |
                                   KACS_OPEN_PIN_ANCESTORS */
    const void *sd;             /* creator SD on create, else NULL */
    size_t      sd_len;
    uint32_t    share_access;   /* KACS_FILE_SHARE_*, with KACS_OPEN_SHARE_ACCESS */
    int         lease_fd;       /* lease manager, with KACS_OPEN_LEASE_KEY */
    uint8_t     lease_key[16];  /* with KACS_OPEN_LEASE_KEY */
};

int peios_file_open(int dirfd, const char *path,
                    const struct peios_open_params *p, uint32_t *status_out);

peios_file_open opens path relative to dirfd (the usual *at convention — an absolute path ignores dirfd, and AT_FDCWD means the current directory). It returns a file fd, or -1 with errno.

Zero-initialize the struct and set the fields you need. share_access and the lease fields are read only when flags carries their flag, so a zeroed struct opens the way a plain open(2) does: sharing read, write, and delete.

The parameters:

FieldMeaning
desired_accessThe access mask you are requesting — KACS_FILE_* object rights, standard rights, or generic bits the file class maps. Every right named has to be granted, or the open fails with EACCES. MAXIMUM_ALLOWED asks for everything you can have, and can be named alone.
dispositionWhat to do about existence: KACS_DISPOSITION_* — open-existing, create-new, open-or-create, supersede, overwrite, and so on. This is the create/open decision open() splits across O_CREAT/O_EXCL/O_TRUNC.
optionsKACS_CREATE_OPT_DIRECTORY and KACS_CREATE_OPT_DELETE_ON_CLOSE; other bits fail with EINVAL. No-follow belongs in flags, not options.
flagsAT_SYMLINK_NOFOLLOW; the privilege-intent flags KACS_BACKUP_INTENT / KACS_RESTORE_INTENT; and the flags that switch on the fields below.
sd / sd_lenThe creator security descriptor — the SD to stamp on a newly created file. Pass NULL when opening an existing file (or to let the parent's inheritance decide the new file's SD).
share_accessWith KACS_OPEN_SHARE_ACCESS: which kinds of access other opens of the file can have while this one is open — KACS_FILE_SHARE_READ, _WRITE, and _DELETE.
lease_fd / lease_keyWith KACS_OPEN_LEASE_KEY: a lease manager and the key this opener holds leases under. Opening does not break leases held under the same manager and key (Locks and leases).

KACS_OPEN_PIN_ANCESTORS keeps the directories above the file from being renamed or removed while the open lasts, as Windows does for a file it has open. It needs SeLockParentagePrivilege enabled, and fails with EPERM without it.

5.2.1 What the descriptor can do #

The descriptor's access is fixed when it opens. Read it back with peios_file_query_open, which is how you learn what a MAXIMUM_ALLOWED open received.

The descriptor is readable if it was granted FILE_READ_DATA and writable if it was granted a write right. An open granted neither is a control handle: it cannot read or write the file's contents, but it can read attributes, read and change the security descriptor, set a delete disposition, and rename. You do not need to ask for a data right to get a descriptor.

An open that creates the file gets the access it asked for, even where the new file's descriptor would not grant it, as in Windows. You can create a file with a restrictive descriptor and still write it, in one step. Integrity labels, restricted tokens and confinement still apply, and later opens are checked against the descriptor as normal.

DELETE and FILE_READ_ATTRIBUTES can come from the parent directory: from FILE_DELETE_CHILD and FILE_LIST_DIRECTORY on it, as NTFS grants them.

5.2.2 Sharing #

Every open of a regular file or directory takes part in share checking, including plain open(2). An open that conflicts with another fails with EBUSY. Without KACS_OPEN_SHARE_ACCESS your open shares everything; with it, other opens are refused the kinds of access you leave out — but only those you could exercise yourself, so a reader cannot lock others out of a file it could not change. See Sharing and Deletion for the rules.

5.2.3 Status and errors #

status_out, if non-NULL, receives a KACS_STATUS_* code telling you what happened — whether the file was opened, created, superseded, overwritten. For OPEN_IF with no creator SD (sd = NULL, sd_len = 0), this distinguishes "created a new file" from "opened the existing one" without a separate stat race. Supplying a creator SD changes the existing-file behavior described by the disposition rules.

When the open fails for a reason errno alone cannot tell apart, status_out names it:

StatuserrnoCondition
KACS_STATUS_SHARING_VIOLATIONEBUSYAnother open's sharing conflicts with this one.
KACS_STATUS_DELETE_PENDINGEBUSYThe file is marked to be deleted when its last open closes.
KACS_STATUS_CANNOT_DELETEEPERMDelete-on-close on a read-only file.

Other errors include EACCES (a requested right denied), EEXIST (create-new and the file exists), ENOENT (open-existing and it doesn't), ENOTDIR (directory option, non-directory target), ELOOP (native open with no-follow and a final symlink), EPERM (pinning without the privilege), EINVAL (unsupported create-option bits, a field set without its flag, malformed creator SD, NULL path/p, sd == NULL with sd_len != 0), and EBADF (bad dirfd, or a lease_fd that is not a lease manager).

Supplying a creator SD with OPEN fails with EOPNOTSUPP, and with OVERWRITE fails with EINVAL. With OPEN_IF or OVERWRITE_IF, it fails with EINVAL when the path resolves to an existing object. The SDK forwards the disposition and SD; it does not silently drop the SD on that branch. See the kernel native-open guide for disposition rules. Use create-only when an explicit creator SD is required:

struct peios_open_params p = {
    .desired_access = KACS_FILE_READ_DATA | KACS_FILE_WRITE_DATA,
    .disposition    = KACS_DISPOSITION_CREATE,    /* fail if the path exists */
    .sd             = creator_sd, .sd_len = creator_sd_len,
};
uint32_t status = 0;
int fd = peios_file_open(AT_FDCWD, "data.bin", &p, &status);
if (fd < 0) { /* errno */ }
/* On success, status == KACS_STATUS_CREATED. */

To open an existing file for reading while nobody else writes or deletes it:

struct peios_open_params p = {
    .desired_access = KACS_FILE_READ_DATA,
    .disposition    = KACS_DISPOSITION_OPEN,
    .flags          = KACS_OPEN_SHARE_ACCESS,
    .share_access   = KACS_FILE_SHARE_READ,
};
uint32_t status = 0;
int fd = peios_file_open(AT_FDCWD, "report.txt", &p, &status);
if (fd < 0 && status == KACS_STATUS_SHARING_VIOLATION) { /* someone else has it */ }

To open an existing object, use KACS_DISPOSITION_OPEN with sd = NULL and sd_len = 0. If create-only returns EEXIST, decide whether opening the existing object is appropriate before issuing that separate call. A create/open retry is not an atomic transaction; neither is a stat-then-open check.

The wrapper forwards options, flags, the share access, the lease fields and the creator descriptor to the kernel, and zeroes reserved fields. Supplying an option through the library does not make an unsupported kernel option valid.

Edit this page