Thanks Axel for the reply - that's a clever solution. Just to close the loop on this, the solution that I finally opted for was to use the umask system call twice during my program's init() and save the value. Given the nature of the program, the race condition should not be a problem - no files are created in any package's init() that I'm aware of.
On Tuesday, November 6, 2018 at 4:44:07 PM UTC+1, Axel Wagner wrote: > > (and, of course, O_CREATE and everything. sorry for sending multiple > messages ^^) > > On Tue, Nov 6, 2018 at 4:43 PM Axel Wagner <axel.wa...@googlemail.com > <javascript:>> wrote: > >> Oh, come to think of it, there's a way: Use os.OpenFile with mode=0777 >> and then stat the file? Should work, no? >> >> On Tue, Nov 6, 2018 at 4:41 PM Axel Wagner <axel.wa...@googlemail.com >> <javascript:>> wrote: >> >>> That seems to be fundamentally impossible on posix using only syscalls, >>> but you may be able to use /proc: >>> https://manpages.debian.org/stretch/manpages-dev/umask.2.en.html#NOTES >>> >>> >>> On Tue, Nov 6, 2018 at 4:35 PM Tom Payne <twp...@gmail.com <javascript:>> >>> wrote: >>> >>>> Hi, >>>> >>>> The user's umask can be set with the umask(2) system call, this sets >>>> the new umask and returns the old one. As far as I can tell, the canonical >>>> way to get the user's current umask is to call umask twice: once to get >>>> the >>>> old value and set a temporary one, then a second call to restore the old >>>> value: >>>> >>>> func getUmask() int { >>>> umask := syscall.Umask(0) >>>> syscall.Umask(umask) >>>> return umask >>>> } >>>> >>>> This has an obvious race condition: if some file operations (e.g. mkdir >>>> or open) occur between the two calls to syscall.Umask then they will use >>>> the unwanted temporary umask value. >>>> >>>> How can I avoid this race condition? >>>> >>>> One heavyweight possibility is to read the umask during the main >>>> package's init, including a call to runtime.LockOSThread(). However, I'd >>>> like to be able to call my getUmask() function at any time. >>>> >>>> Many thanks for any pointers, >>>> Tom >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "golang-nuts" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to golang-nuts...@googlegroups.com <javascript:>. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.