[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]


previous: >>108049558

#define __NR_dup                32
#define __NR_dup2 33
#define __NR_dup3 292

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

god, backwards compatibility makes the linux API pull some weird shit. having three separate syscalls that do effectively the same thing is really unfortunate. especially when it would be so easy to combine them all into a single syscall. but i guess that's just the price you pay.
still, i rather like dup2. it's a nice, handy way to modify your stdin/stdout/stderr files without having to worry about race conditions. i'm sure there are plenty of other uses, too, especially with regard to magic-numbered file descriptors, but the std* ones are the only ones i've had occasion to use

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/
>>
>>108058648
It's neat that they have the errors listed.
I can't see many uses for it desu. Only useful thing is that it lets you pipe stdout to a file instead
>>
>>108058763
>pipe stdout to a file instead
what do you think stdout is by default?
>>
>>108058648
And to duplicate into a new fd (not replace an old one) while setting CLOEXEC you have to use neither dup nor dup2 nor dup3 but rather fcntl: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
>>
>>108058648
you fucking degenerate troon, inserting your mental illness and degeneracy into our pure board, kys by strogen overdose
>>
>>108058961
well, technically i believe you can still use dup3. you just need to know an available fd value, or be willing to blow away whatever was there before. so you could theoretically open /dev/null to acquire the fd and then replace that, but i agree that's pretty convoluted
>>108058969
lmfao
i picked the image because dup sounds like "dupe", so a *trick* or treat image felt appropriate
>>
File: bears.png (311 KB, 557x446)
311 KB
311 KB PNG
>>108058981
>you just need to know an available fd value, or be willing to blow away whatever was there before
At that point you're vulnerable to race conditions and you might as well use dup() and set the flag non-atomically. Or you'd have to use a convoluted allocation scheme
>dup sounds like "dupe", so a *trick* or treat image felt appropriate
I don't mind the degenerate troon image but I did not guess that, it seemed unrelated
Here's my contribution to the thread theme
>>
>>108059006
yeah, the fcntl method definitely seems the most reasonable way to set it
honestly, the whole concept of fds not being CLOEXEC by default is a bit strange. why are they inherited while your memory is all blown away? i rather dislike that the consistency isn't there. i guess it would probably be pretty difficult to maintain std* io otherwise, though
cute bears!
>>
>>108058648
My ex-girlfriend had a doggirl fetish who wore a tail and ears during sex and wanted me to leash her. I ended up dumping her because I couldn't take the BPD and the doggirl thing didn't do anything for me.
>>
>>108059024
I'm guessing it's because CLOEXEC didn't use to exist and making it the default would have broken a lot of existing programs.
It's stupid because random inherited file descriptors were a problem from the very beginning before Unix even had fork(). When a command exited it exec'd the shell (sorta) and then the shell had to manually clean up all the files that the command had opened. And then it still took them until V7 to add FIOCLEX/CLOEXEC. (Though I guess it wouldn't have helped with that particular issue.)
On the topic of old Unix, this code is funny:
https://www.tuhs.org/cgi-bin/utree.pl?file=PWB1/sys/source/s1/fd2.c
            close(2);
if (dup(fd) != 2) {
write(open("/dev/tty",1),"bad file descriptor?!?\n",23);
exit(1);
}
close(fd);

No dup2() yet so you close() and dup() and then do an extra check to see if that did what you hoped.
>>
>>108059130
this is so cursed
>>
>>108058785
a stream of data?
>>
>>108058648
Disgusting image
>>
>>108059530
Yeah, op is a faggot
>>
I've used this little guy quite a bit doing graphics driver work. When you're working with reference-counted resources that are bound to an FD like memory buffers or fences it becomes essential.
>>
>>108059519
Unix calls those files too.
You don't have to listen to Unix though.
>>
>>108059519
The usual default is a serial device, typically a (virtual) terminal.
>>
>>108058648
I hope people appreciate writing C with just pure pointers and syscalls. When I started doing it, I fell in love with it.
>>
>>108059519
what do you think backs streams? :)
>>
>>108064005
I'm not sure it makes sense to have as broad a definition of "file" as Unix does
>>
>>108059519
>>108062153
>>108064005
We have to call real files "cis files" now, according to Unix trannies.
>>
>>108066665
lmao
>>
>>108058763
I use dup when using pipewire for screen capture. Pipewire has a callback function and if you do hardware accelerated screen capture it calls the callback function with a file descriptor that it closes after the callback function ends. This callback function is called in a separate thread from everything else but I need to handle it in the main thread so I use dup to increase the reference count of the file descriptor so its not removed even when close is called after the callback function.
>>
>>108066885
souka



[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.