How do I obtain `sizeof(struct sigaction)`? Clang.jl?

I would like to figure out the value printed by this C program in Julia.

 #include <signal.h>
 #include <stdio.h>
 
 int main() {
     printf("%ld", sizeof(struct sigaction));
     return 0;
 }

On my system, this prints 152 bytes.

My /usr/include/x86_64-linux-gnu/bits/sigaction.h has this definition:
https://sourceware.org/git/?p=glibc.git;a=blob;f=bits/sigaction.h;h=cef879f40aae8e14b2e998daeded26ebe5cfad9d;hb=HEAD#l32

Other than building the C code above, is there a way to obtain the size of this struct within Julia?

Related: Dealing with external libraries that don't restore old signal handlers

https://juliainterop.github.io/Clang.jl/stable/api/#Clang.getSizeOf-Tuple{CXType}

1 Like

Here is the solution thanks to @Gnimuc !

julia> temp_header = tempname()*".h"
"/tmp/jl_Cvhf3N.h"

julia> open(temp_header, "w") do f
           write(f,
           """
           #include <stddef.h>
           #include <signal.h>
           
           const size_t sizeof_struct_sigaction = sizeof(struct sigaction);
           """
           )
       end
106

julia> trans = Clang.parse_header(Index(), temp_header, Clang.get_default_args())
TranslationUnit(Ptr{Clang.LibClang.CXTranslationUnitImpl} @0x00007f5ac0220fb0, Index(Ptr{Nothing} @0x0000000002f7cc10, 0, 1))

julia> cursor = Clang.getTranslationUnitCursor(trans)
CLCursor (CLTranslationUnit) /tmp/jl_6DBaOh.h

julia> sigaction = Clang.search(children(cursor), c->Clang.name(c) == "sigaction")[1]
CLCursor (CLStructDecl) sigaction

julia> Clang.getCursorType(sigaction) |> Clang.getSizeOf
152

As a compact function:

julia> using Clang

julia> function sizeof_sigaction()
           temp_header = tempname()*".h"
           open(temp_header, "w") do f
                  write(f, "#include <signal.h>\n")
           end
           trans = Clang.parse_header(Index(), temp_header, Clang.get_default_args())
           rm(temp_header)
           cursor = Clang.getTranslationUnitCursor(trans)
           sigaction = Clang.search(children(cursor), c->Clang.name(c) == "sigaction")[1]
           sigaction_type = Clang.getCursorType(sigaction)
           return Clang.getSizeOf(sigaction_type)
       end
sizeof_sigaction (generic function with 1 method)

julia> sizeof_sigaction()
152