Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unexpected errno: 13 when writing to file #22442

Closed
ItsMeSamey opened this issue Jan 7, 2025 · 2 comments
Closed

unexpected errno: 13 when writing to file #22442

ItsMeSamey opened this issue Jan 7, 2025 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. os-linux standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@ItsMeSamey
Copy link

ItsMeSamey commented Jan 7, 2025

Zig Version

0.14.0-dev.2615+a7a5f3506

Steps to Reproduce and Observed Behavior

run the following code on linux (as normal/unprivelaged user)

const std = @import("std");

pub fn makeOomUnkillable() !void {
  var file = try std.fs.cwd().openFileZ("/proc/self/oom_score_adj", .{ .mode = .write_only });
  defer file.close();
  try file.writeAll("-1000\n"); // Fails with errno = 13
}

pub fn main() !void {
  try makeOomUnkillable();
}
~/Projects/zig/auto_preempt ❯❯ zig run test.zig
unexpected errno: 13
/home/a/.local/lib/zig/std/debug.zig:198:31: 0x105338f in dumpCurrentStackTrace (test)
        writeCurrentStackTrace(stderr, debug_info, io.tty.detectConfig(io.getStdErr()), start_addr) catch |err| {
                              ^
/home/a/.local/lib/zig/std/posix.zig:7432:40: 0x105391e in unexpectedErrno (test)
        std.debug.dumpCurrentStackTrace(null);
                                       ^
/home/a/.local/lib/zig/std/posix.zig:1277:49: 0x105467e in write (test)
            else => |err| return unexpectedErrno(err),
                                                ^
/home/a/.local/lib/zig/std/fs/File.zig:1327:23: 0x103ae1a in write (test)
    return posix.write(self.handle, bytes);
                      ^
/home/a/.local/lib/zig/std/fs/File.zig:1333:32: 0x10387b9 in writeAll (test)
        index += try self.write(bytes[index..]);
                               ^
/home/a/Projects/zig/auto_preempt/test.zig:6:20: 0x103825e in makeOomUnkillable (test)
  try file.writeAll("-1000\n"); // Fails with Errno = 13
                   ^
/home/a/Projects/zig/auto_preempt/test.zig:10:24: 0x1038860 in main (test)
  try makeOomUnkillable();
                       ^
/home/a/.local/lib/zig/std/start.zig:656:37: 0x1038122 in posixCallMainAndExit (test)
            const result = root.main() catch |err| {
                                    ^
/home/a/.local/lib/zig/std/start.zig:271:5: 0x1037d0d in _start (test)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
error: Unexpected
/home/a/.local/lib/zig/std/posix.zig:7434:5: 0x1053927 in unexpectedErrno (test)
    return error.Unexpected;
    ^
/home/a/.local/lib/zig/std/posix.zig:1277:27: 0x105468b in write (test)
            else => |err| return unexpectedErrno(err),
                          ^
/home/a/.local/lib/zig/std/fs/File.zig:1327:5: 0x103ae47 in write (test)
    return posix.write(self.handle, bytes);
    ^
/home/a/.local/lib/zig/std/fs/File.zig:1333:18: 0x10387e2 in writeAll (test)
        index += try self.write(bytes[index..]);
                 ^
/home/a/Projects/zig/auto_preempt/test.zig:6:3: 0x103827a in makeOomUnkillable (test)
  try file.writeAll("-1000\n"); // Fails with Errno = 13
  ^
/home/a/Projects/zig/auto_preempt/test.zig:10:3: 0x1038873 in main (test)
  try makeOomUnkillable();
  ^

Operating System: Arch Linux
Kernel: Linux 6.12.8-zen1-1-zen
Architecture: x86-64

Expected Behavior

program should exit with some sort of PermissionError or AccessDenied

@ItsMeSamey ItsMeSamey added the bug Observed behavior contradicts documented or intended behavior label Jan 7, 2025
@ItsMeSamey ItsMeSamey changed the title unexpected errno: 13 when writing to /proc/self/oom_score_adj unexpected errno: 13 when writing to file Jan 7, 2025
@alexrp
Copy link
Member

alexrp commented Jan 7, 2025

Looks like this just needs to be made into an expected error in std.posix.write().

@alexrp alexrp added standard library This issue involves writing Zig code for the standard library. os-linux contributor friendly This issue is limited in scope and/or knowledge of Zig internals. labels Jan 7, 2025
@alexrp alexrp added this to the 0.15.0 milestone Jan 7, 2025
skk64 added a commit to skk64/zig that referenced this issue Jan 7, 2025
This is to handle the issue described in ziglang#22442.

There is an AccessDenied already there, but posix describes errno 13
specifically as "Permission Denied".
@andrewrk andrewrk modified the milestones: 0.15.0, 0.14.0 Jan 21, 2025
@andrewrk
Copy link
Member

Fixed in 744771d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. os-linux standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

3 participants