Git Product home page Git Product logo

Comments (2)

Syllo avatar Syllo commented on September 27, 2024

I guess that it's hard for a process to know if it's running in a VM/container.
Maybe add a column showing the hostname of the machine which is probably the cleanest way to show that information?

from nvtop.

sammcj avatar sammcj commented on September 27, 2024

Hmm I wonder if the cgroup path could be used?

I don’t really know C but a certain large language model suggested something like this:

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

#define BUFFER_SIZE 1024

// Function to trim newline character from strings
void trim_newline(char* string) {
    if (string == NULL) return;
    char* newline = strchr(string, '\n');
    if (newline) *newline = 0;
}

// Function to get the container name from cgroup file
void get_container_name_from_pid(int pid, char* container_name, int max_len) {
    char path[BUFFER_SIZE];
    char buffer[BUFFER_SIZE];
    FILE* file;
    snprintf(path, sizeof(path), "/proc/%d/cgroup", pid);

    if ((file = fopen(path, "r")) == NULL) {
        perror("Error opening cgroup file");
        strcpy(container_name, "");
        return;
    }

    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        // Docker containers are usually identified by "docker" in the cgroup path
        if (strstr(buffer, "/docker/") != NULL) {
            char* start = strrchr(buffer, '/');
            if (start != NULL) {
                start++; // Move past the '/'
                trim_newline(start);
                strncpy(container_name, start, max_len);
                fclose(file);
                return;
            }
        }
    }

    // If we reach here, no container name was found
    strcpy(container_name, "");
    fclose(file);
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        printf("Usage: %s <pid>\n", argv[0]);
        return 1;
    }

    int pid = atoi(argv[1]);
    char container_name[BUFFER_SIZE];
    get_container_name_from_pid(pid, container_name, sizeof(container_name));

    if (strlen(container_name) > 0) {
        printf("Container name: %s\n", container_name);
    } else {
        printf("The PID %d is not running in a Docker container.\n", pid);
    }

    return 0;
}

from nvtop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.