#How to read virtual files on Linux using a C program?

28 messages · Page 1 of 1 (latest)

livid oracle
#

Hey, Im writing a simple program, where every process from /proc is printed out on the screen and I also want to hace the names of each process, so I tried file /proc/88/status => and it says its empty but when I try to read it by cat status it spits out the needed information, so my question is how and if I can even read it?

thank you very much

raw trellisBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

green belfry
#

depending on the mount options you might not be able to read it

#

Users may not access files and subdirectories inside
any /proc/pid directories but their own (the /proc/pid
directories themselves remain visible). Sensitive
files such as /proc/pid/cmdline and /proc/pid/status
are now protected against other users. This makes it
impossible to learn whether any user is running a
specific program (so long as the program doesn't
otherwise reveal itself by its behavior).

livid oracle
#

i tried also the comm file but no luck, is there a way to "sudo" read it?

green belfry
#

not sure if systemd allows that

livid oracle
#

so i suppose i have option 1, right?

#

so i should change it to 0 and then try?

green belfry
#

its not locked down "for fun" by default

livid oracle
#

lol, thank you, you saved me a lot of time

green belfry
#

can you cat /proc/1/status ?

livid oracle
#

yes i can

#

i can read every file on there but not with a script

green belfry
#

how do you read it

livid oracle
#

ill show u the code

#
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <ctype.h>
#include <string.h>

int is_all_digits(struct dirent *entry)
{
    char *pToName = entry->d_name;
    size_t lenOfName = strlen(pToName); // ahoj = 4 => 0-3

    if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name,"..") != 0)
    {
        for(size_t i = 0; i < lenOfName; i++)
        {
            if(isdigit(pToName[i]))
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
    }
    else
    {
        printf("\nROVNA SE TO\n");
        return 1;
    }

    return 0;
}


char nameOfPID(DIR *directory, const char *newPathToOpenDir) // /proc/PID/comm
{
    struct dirent *newEntry;
    
    if((directory = opendir(newPathToOpenDir)) == NULL)
    {
        printf("chyba pro otevirani slozky");
        return 1;
    }

    //FILE *file_pointer

    

}
#

and main:

int main()
{
    printf("---procesy z /proc a jejich nazvy---\n");

    char newPathToOpenDir[20]; // /proc/maxDelkaPID4 => 20 pro jistotu
    char newPathToFile[20]; // /proc/maxDelkaPID4/comm => pro jistotu
    unsigned char data[30];

    DIR *directory; // neco jako FILE *file_pointer, ale pro slozky
    struct dirent *entry; // ukazatel na strukturu dirent, ktera obsahuje ruzne informace current slozce/souboru => entry

    if((directory = opendir("/proc")) == 0)
    {
        printf("chyba u otevirani slozky /proc");
        return 1;
    }

    size_t i = 1;
    while((entry = readdir(directory)) != NULL)
    {
        // offset v /proc se udela sam interne
        if (is_all_digits(entry) == 0)
        {
            printf("%d: %s\n", i, entry->d_name);
            
            snprintf(newPathToOpenDir, 12, "/proc/%s", entry->d_name);
            snprintf(newPathToFile, 16, "/proc/%s/comm", entry->d_name);

            FILE *filePointer = fopen(newPathToFile, "rb");

            fseek(filePointer, 0, SEEK_SET);
            fseek(filePointer, 0, SEEK_END);
            size_t delkaSouboru = ftell(filePointer);
            fseek(filePointer, 0, SEEK_SET);

            if (fread(data, sizeof(unsigned char), delkaSouboru, filePointer) != delkaSouboru)
            {
                printf("nejak se to nerovna, error");
                fclose(filePointer);
                return 1;
            }
            data[delkaSouboru] = '\0';

            printf("\ntady to je: %s\n", data);
            fclose(filePointer);
            
            i++;
        }
        else
        {
            //printf("\nNE\n");
        }
    }
    if(closedir(directory) != 0)
    {
        return 1;
    }

    return 0;
}
green belfry
#

works on my machine gigachad

livid oracle
#

what is the assert.h for?

#

ill try it on a whole new project

green belfry
#

if fp == NULL it will kill the program

livid oracle
#

got you

#

oh yeah it works lol

#

wow, thank you for the solution, ill do it in the main project, really appreaecite it

raw trellisBOT
#

@livid oracle Has your question been resolved? If so, type !solved :)

livid oracle
#

!solved