• four@lemmy.zip
    link
    fedilink
    English
    arrow-up
    1
    ·
    13 minutes ago

    I’m surprised it doesn’t mention that /proc/self automagically points to the directory of the current process. So if you’re writing a program, you can just look there for information about itself

  • jaybone@lemmy.zip
    link
    fedilink
    English
    arrow-up
    4
    ·
    1 hour ago

    I’m aware of /proc but usually use lsof to find open fd’s for a process.

    Is one better than the other?

  • jaybone@lemmy.zip
    link
    fedilink
    English
    arrow-up
    1
    ·
    59 minutes ago

    I seem to recall in an Operating Systems class we wrote a kernel module and communicated with it through the proc interface?

    • white_nrdy@programming.dev
      link
      fedilink
      arrow-up
      1
      ·
      49 minutes ago

      I may have been through “SysFS” as well? In /sys/class you can setup char dev IO with a kernel module (and more). Very helpful for userspace <-> kernel communication (like DMA drivers for userspace applications)

      • jaybone@lemmy.zip
        link
        fedilink
        English
        arrow-up
        1
        ·
        7 minutes ago

        Hmm yeah maybe it was /sys, it’s been so long I can’t remember.

        I seem to recall echoing into a file to set some value which we then checked the value of in the kernel module code. It was just a hello world type of thing for a college class.

  • provectus@lemmy.ml
    link
    fedilink
    English
    arrow-up
    4
    ·
    edit-2
    2 hours ago

    Did you know openBSD does not have this folder? I think the BSD’s do not use /proc. I do not know why though.

  • otacon239@lemmy.world
    link
    fedilink
    arrow-up
    30
    ·
    4 hours ago

    Okay, this is legitimately cool as heck. This finally helps me wrap my head around the “everything is a file” concept in Linux. Of course it can just copy the executable to memory for later reference. That’s how the system would not completely have a meltdown when you do live updates.

    Excellent share!

    • atzanteol@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      18
      ·
      4 hours ago

      It’s a bit more interesting than that…

      Linux, unlike Windows, will let you delete a file that is actively in use. It removes the reference on the file system but the contents of the file won’t be deleted until all file pointers to it close. In fact it will still be seen as taking up disk space until it’s garbage collected (deleting a large file that’s in use can be frustrating).

      So the file is actually still available to that running process. If you replace it with a new executable and run that then you get the new version.

  • naught101@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    1 hour ago

    Damn, I really could have made a lot of use of this over the last 20+ years… Thanks for sharing!

    • Shadow@lemmy.ca
      link
      fedilink
      arrow-up
      16
      ·
      5 hours ago

      If you delete a file that’s still open by the app, the link in proc will still exist and you can copy the file back out of proc.

      • RustySharp@programming.dev
        link
        fedilink
        arrow-up
        6
        ·
        5 hours ago

        Does that not make them a hard link (pointing to a filesystem node) instead of a symlink (pointing to another filename)?

        • Shadow@lemmy.ca
          link
          fedilink
          arrow-up
          12
          ·
          5 hours ago

          No, you can’t have a hard link cross filesystems and proc is its own fs type.

                • Shadow@lemmy.ca
                  link
                  fedilink
                  arrow-up
                  1
                  ·
                  40 minutes ago

                  It still is! If you try and boot a kernel with nothing in /dev, you won’t get most of the kernel boot messages since /dev/console doesn’t exist. You need a basic set of device nodes in /dev before udev is started.

                  If you’re going to rsync a system to new hardware and exclude /dev /proc /sys, it’s better to setup a new mount of your / to a different directory and sync off that, so you get the underlying stuff without the cruft.

  • lyralycan@sh.itjust.works
    link
    fedilink
    arrow-up
    13
    ·
    edit-2
    6 hours ago

    Great tips! Although if i may 🤓 just a little for the top right, it works because what you deleted isn’t the binary, it’s the pointer that points to the binary’s location. The data is still exactly as it was before “deletion”; the symlink is simply a copy of the original pointer’s info; and I’m speculating that the existence of any pointer prevents the system from recycling those addressed bits.

    • bruh@nord.pub
      link
      fedilink
      English
      arrow-up
      3
      ·
      5 hours ago

      it merely deletes the inode from the directory instead of overwriting the actual data on the disk

  • pelya@lemmy.world
    link
    fedilink
    arrow-up
    7
    ·
    5 hours ago

    Do you know how to build portable executables?

    configure --prefix=/proc/self/pwd
    

    It even works in .so files and libtool.

      • pelya@lemmy.world
        link
        fedilink
        arrow-up
        3
        ·
        3 hours ago

        The executable will search for .so files not inside predefined paths like /usr/lib but inside it’s current directory. You may theoretically put . as an argument to configure, but it converts that into an absolute path, snd libtool and linker fail when encountering relative paths inside shared library dependencies.

      • pelya@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        4 hours ago

        --prefix=$(pwd) is resolved at compile time. If you move your compiled .so files to a different directory, they stop working.

        • eleijeep@piefed.social
          link
          fedilink
          English
          arrow-up
          2
          ·
          4 hours ago

          I see what you mean, very clever solution if a program is using the configure prefix by compiling it into the binary.

          That can’t be very common though is it? Usually the configure prefix is just used by make install to install the binaries into the prefix. If the compiled program needs to know where it is installed it can read argv[0].

          Have you seen this used somewhere?

          • pelya@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            3 hours ago

            I am using this myself in Android app, where you can write files only inside /data/data/your.app.id/

            So naturally, if you want to publish a different app, your.app.id will be different, so you need to recompile all your Linux tools that you bundle inside your app.

            If you are not running your Linux tools on Android, you’d probably be better off using Docker or a chroot.