2016-03-08 61 views
-1
的信息

今天的第二個問題,第一個問題確實有幫助。如何打印來自/ proc/pid/

所以這是我的代碼:

#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/procfs.h> 
#include <sys/fcntl.h> 
#include <pwd.h> 
char *getWaitChannel(int pid); 
char *getPath(int pid); 
char *getUserName(int uid); 
int getBytes(int pid); 
int main(int argc, char *argv[]) 
{ 
    long x; 
    if (argc < 2){ 
    //error message 
} 
    x = strtol(argv[1], NULL, 10); 
    printf("Good 1\n"); 
    get_info(x, argc, argv); 
} 
int get_info(pid_t pid) 
{ 
    char path[40], line[100], *p, stateChar[100], Name[100]; 
    FILE* statusf; 
    char buf[100]; 
    printf("This is pid %d\n", pid); 
    int uid, vM; 
    snprintf(path, 40, "/proc/%d/status", pid); 
    statusf = fopen(path, "r"); 
    if(statusf == NULL) 
     return -1; 

    while(fgets(buf,sizeof buf, statusf) != NULL){ 

    sscanf(buf, "State: %s", stateChar); 
    sscanf(buf, "Name: %s", Name); 
    sscanf(buf, "Uid:  %d", &uid); 
    sscanf(buf, "VmPeak: %d", &vM); 

} 
    char *channel = getWaitChannel(pid); 
    char *full_path = getPath(pid); 
    char *user = getUserName(uid); 
    int b = getBytes(pid); 
    printf("State: %s\n", stateChar); 
    printf("Name: %s\n", Name); 
    printf("Uid: %d\n", uid); 
    printf("Username: %s\n", user); 
    printf("Max Virtual Memory: %d\n", vM); 
    printf("Full Path: %s\n", full_path); 
    printf("Bytes written to storage layer: %d\n", b); 
    printf("Waiting channel: %s\n", channel); 

} 
char *getUserName(int uid) 
{ 
    struct passwd *pw = getpwuid(uid); 
    if (pw) 
    { 
     return pw->pw_name; 
    } 
    return ""; 
} 
int getBytes(int pid) 
{ 
    FILE* statusf2; 
    char path[40]; 
    char buf2[100]; 
    int storage_bytes; 
    snprintf(path, 40, "/proc/%d/io", pid); 
    statusf2 = fopen(path, "r"); 
    if(statusf2 == NULL) 
     return -1; 

    while(fgets(buf2,sizeof buf2, statusf2) != NULL){ 
    sscanf(buf2, "write_bytes: %d", &storage_bytes); 
    return storage_bytes; 
    } 
} 
char *getPath(int pid) 
{ 
    FILE* statusf3; 
    char path[40]; 
    char buf3[100]; 
    char *fullpath; 
    snprintf(path, 40, "/proc/%d/cmdline", pid); 
    statusf3 = fopen(path, "r"); 
    if(statusf3 == NULL) 
     return ""; 

    while(fgets(buf3,sizeof buf3, statusf3) != NULL){ 
    sscanf(buf3,"/ %s", fullpath); 
    return fullpath; 
} 
} 
char *getWaitChannel(int pid) 
{ 
    FILE* statusf4; 
    char path[40]; 
    char buf4[100]; 
    char *channel; 
    snprintf(path, 40, "/proc/%d/stack", pid); 
    statusf4 = fopen(path, "r"); 
    if(statusf4 == NULL) 
     return ""; 

    while(fgets(buf4,sizeof buf4, statusf4) != NULL){ 
    sscanf(buf4,"[<c0227f4e>] %s", channel); 
    return channel; 
} 
} 

我得到的名稱,狀態,UID以及用戶名和VmPeak信息。他們以我想要的方式工作。但其他3個是我似乎無法使它們工作的問題,我找不出原因(完整路徑,寫入存儲層和等待通道的字節)。所以我的問題是如何訪問它們並打印信息。

+0

該代碼似乎錯過了爲'char * channel'分配任何東西,因此讀到它指向的位置會調用未定義的行爲。 – alk

+0

你可以請示出解決方案嗎? –

回答

1

如alk所示,修改您的getWaitChannel()爲通道分配內存。稍後當通道數據不需要時,您需要釋放內存。

char *getWaitChannel(int pid) 
{ 
    FILE* statusf4; 
    char path[40]; 
    char buf4[100]; 
    char *channel; 
    channel = malloc(1024); 
    /* Add error handling for malloc failure here */ 

    snprintf(path, 40, "/proc/%d/stack", pid); 
    statusf4 = fopen(path, "r"); 
    if(statusf4 == NULL) 
     return ""; 

    while(fgets(buf4,sizeof buf4, statusf4) != NULL){ 
    sscanf(buf4,"[<c0227f4e>] %s", channel); 
    return channel; 
} 
} 

檢查你的代碼,看看任何其他變量(例如FULLPATH)是否需要內存分配,並通過註釋的代碼捕獲錯誤的某些部分進行分步調試。

+0

我找出問題所在,因爲權限被拒絕,無法訪問文件io和堆棧。你知道如何解決這個問題嗎? –

+0

我認爲它需要root權限。嘗試從root用戶運行程序。 –

+0

我正在從根目錄運行它 –

相關問題