1

I recently discovered the mkstemp() function (see this link) but it does not fully meet my need which would be to compile temporary .o files (with gcc -c script.c for example) and can only be accessed by the PID of the current program (and destroyed once it is stopped). Do you know if that is possible ?

One of the solutions could also be to create a directory in /tmp only accessible by the program much like the systemd-private-* directories system.

the program starts like this:

#include <sys/types.h>
#include <unistd.h>

int main(int argc, char *argv[]){

    printf("pid=%d\n", getpid());
    
    /* for create .o in tmp file, but i dont know how to but
    I don't know how to create/configure "systemd-***" directory
    or if it is possible to directly create a temporary file*/
    /* the ideal would be not to have to create a special user,
    which would give rights specifically to the directory/file */
    int ret = system("gcc -c script.c -o /tmp/systemd-private-script/script.o");
    ...

    return(0);
}

Thanks for you help

John S
  • 231
  • 3
  • 11
  • 2
    In most POSIX-ish systems, you can only stop other user IDs from accessing the files; you can't stop other processes with the same user ID from accessing the files in a directory owned by the user ID. So, without knowing what the `/tmp/systemd-private-xxxx` script directories do, I'd say "No; you can't do all that you want." Even ACLs work on user and group IDs and not on processes. Linux may have features to help you — you don't mention Linux in the question, so it isn't clear how relevant these are to you. – Jonathan Leffler Apr 13 '22 at 20:58
  • You can use the `-o` option to `gcc` to specify the output file of the compilation. This can be in the temporary directory. – Barmar Apr 13 '22 at 20:58
  • There is `mkdtemp()` function for creating directories, like `mkstemp()` creates files. I didn't find any that creates FIFOs as standard. – Jonathan Leffler Apr 13 '22 at 21:09
  • Maybe you could change your program's UID and GID to `root` and given the permission `6755` so that it could be run by other users, if it's OK for the root user to access your gcc generated files. – ABacker Apr 14 '22 at 04:27
  • 1
    If you don't insist of using GCC, did you consider TCC (the tiny C compiler) to compile into memory? AFAIK this project provides a compiling library. -- I think that an application's memory is better protected than any file. And it is released at termination of the application. – the busybee Apr 14 '22 at 06:46
  • thank you for you reply. for @thebusybee , do you know if it is possible to use the libttc library to compile in memory (or in the filesystem) static libraries? Because I would like TTC to be directly integrated into the program (so as not to make the prior installation of TTC mandatory) – John S Apr 17 '22 at 14:00
  • 1
    Well, I did not try it, but perhaps you can statically link libtcc. In any case, make sure to read and understand available documentation. – the busybee Apr 17 '22 at 17:20

1 Answers1

1

On linux, yes. using open with the O_TMPFILE and O_EXCL flags, you use the posix_spawn api instead of system(). make gcc ouput to the stdout, which is connected to the open fd. After you are done close the fd and it will vanish, it can never be accessed by anyone as it is not materialized on the filesystem.