[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: IMG_3107.jpg (206 KB, 941x1197)
206 KB
206 KB JPG
previous: >>107948632

#define __NR_access                21

https://man7.org/linux/man-pages/man2/access.2.html

i really like this syscall! despite its limitations in the notes and bugs sections, it's still really cool. pretty handy for checking whether certain files are present. just don't use it for security, lol. as long as you only ever use F_OK, you're probably going to be fine, okay

relevant resources:
man man

man syscalls

https://man7.org/linux/man-pages/
https://linux.die.net/man/
https://elixir.bootlin.com/linux/
https://elixir.bootlin.com/musl/
https://elixir.bootlin.com/glibc/
>>
bampu
>>
>>107957203
I usually see this done using stat
Seems like this is mainly intended for setuid programs? But if you feel the need to do this in a setuid program you probably shouldn't be using setuid
>>
>>107958036
access is nice because you only need the filename. no need to declare a stat struct
>>
>>107957203
most of the time you shouldn't be checking for existence anyway
and just open the file direcetcly and let open "check for existance"
>>
>>107958674
sometimes you only want to check for existence and don't need to open it, though i will admit that is rare
>>
File: file.png (18 KB, 431x184)
18 KB
18 KB PNG
>>107958329
Maybe I'm spoiled by the ergonomic wrappers in my standard library but isn't this better solved by means of an ergonomic wrapper

>>107958036
>Seems like this is mainly intended for setuid programs
It probably showed up first in PWB Unix (fork of V6 Unix that influenced V7). Here's the PWB man page: https://www.tuhs.org/cgi-bin/utree.pl?file=PWB1/usr/man/man2/access.2
It already had the setuid special behavior. But it's used in non-setuid utilities like mkdir: https://www.tuhs.org/cgi-bin/utree.pl?file=PWB1/sys/source/s2/mkdir.c
Wait scratch that mkdir used to be setuid. It had to call chown and everything. What the fuck?
>>
>>107957203
hm, interesting one.
But as other anons have mentioned I've never written a setuid programme or have been in the situation where I couldn't just try to open a file and go from there.
>>107958329
But also on another note, I try to avoid file names in my programme as much as possible, so I mostly open a file and then use fstat.
>>
>>107957203
The message in ops pic resonates with me.
>>
Hmm... Couldn't you accomplish everything this system call does by calling getuid and stat? I'm wondering why it's even a thing
>>
>>107959667
convenience I'd imagine
>>
>>107959274
>>107959667
so, to give a really contrived example: say you're a 32-bit program, and you want to know whether the machine you're on has support for 64-bit programs. you could make a call to uname, sure. but again, then you have to parse a struct. it's way easier to just check if a `lib64` path exists, and as a bonus, that call probably doesn't even need a comment explaining what you're doing. it should be pretty obvious just from the call itself
bool supports_64_bit = access("/usr/lib64", F_OK) == 0;
>>
>>107959667
test -x path requires access()
sorry chud
>>
>>107957203
I sent that pic to my mom, she didn’t like it.
>>
>>107961082
https://git.kernel.org/pub/scm/utils/dash/dash.git/tree/src/bltin/test.c#n662
Checkmate atheists
>>
>>107962079
The comment above this implementation gives some grand wizard tier historical context
>>
>>107957203
>just don't use it for security, lol
If this is about avoiding the typical racey security problems you could open the file first then check access("/proc/self/fd/2").
>>
>Obviously you would have to close stderr first and also not have any other files open for that to make sense. Despite this terrific blunder you understood my meaning anyways.
>>
>>107964497
That might work if you can then keep operating on the fd but somebody in this thread told me syscalls like openat were bad
>>
>>107964542
Bad because the filepath model is inadequate for the problem, perhaps, and it would be better to have handles and transactions... but as an amelioration, idk what the problem would be.
>>
>>107959123
No? It's extremely common?
If necessary_path does not exist create necessary_path
This is done all the time, you can do something like
open necessary_path || create necessary_path
But that's essentially just rewriting the conditional into a cleaner form
>>
>>107966305
Anonymous was talking about cases where you don't need to open it and generally if you need to create it you need to open it
>open necessary_path || create necessary_path
You can just open it with O_CREAT to do it in a single syscall
I guess in that case you don't know whether the file already existed? But it's always been good enough for my use cases
>>
>>107966305
>If necessary_path does not exist create necessary_path
this is a condition race
https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use
>>
File: dont_car.png (361 KB, 716x716)
361 KB
361 KB PNG
>>107966787
I see
>>107966817
>
>>
I haven't posted on this shithole in years, but this is an interesting concept for a thread, so bump.
>>
File: FWSNfO-UEAAd658.jpg (408 KB, 2048x1918)
408 KB
408 KB JPG
>>107957203
i love these threads and i love (You), OP.
>>
File: 1746258524982609.png (241 KB, 736x820)
241 KB
241 KB PNG
>>107969800
y-you too....



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.