What is wrong with my sysctl()?

This is a discussion on What is wrong with my sysctl()? within the Linux Programming forums, part of the Platform Specific Boards category; When I compile the following: Code: #include <sys/types.h> #include <sys/sysctl.h> #include <sys/stat.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <kvm.h> ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    What is wrong with my sysctl()?

    When I compile the following:

    Code:
    #include <sys/types.h>
    #include <sys/sysctl.h>
    #include <sys/stat.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <kvm.h>
    
    int main(void)
    {
    /*ttypf*/
    
        struct stat sb;
        int mib[4];
        size_t len;
        struct kinfo_proc  *p; 
        static char buffer[1024];
        int count;
        struct kinfo_proc *kp;
    
    
        if (stat("/dev/ttypf", &sb) != 0) {
            perror("stat");
            return 1;
        }
            
    
        mib[0] = CTL_KERN;
        mib[1] = KERN_PROC;
        mib[2] = KERN_PROC_TTY;
        mib[3] = sb.st_rdev;
    
        len = 0;
        if (sysctl(mib, sizeof(mib)/sizeof(int), NULL , &len, NULL, 0) == -1) 
    {
            perror("sysctl test");
            return 1;
        }
    
       p =(struct kinfo_proc *)malloc(len); 
       if (sysctl(mib, sizeof(mib)/sizeof(int), p, &len, NULL, 0) == -1) 
        {
            perror("sysctl real");
            return 1;
        }
    
        /*count = len / sizeof(kp);*/
        kp = p; 
    
        printf("The pid is: %u\n", kp.ki_pid);
    
        return 0;
    }
    I get...

    root@robotics.eecs.berkeley.edu# gcc -g RobotSex.c -o RobotSex -lkvm
    RobotSex.c: In function `main':
    RobotSex.c:52: error: request for member `ki_pid' in something not a structure or union

    What did I do wrong?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    Try "kp->ki_pid".

    gg

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    I'm not sure why you are malloc()'ing the kinfo_proc. Why not just declare:

    Code:
    struct kinfo_proc p;
    And then do:

    Code:
    if (sysctl(mib, sizeof(mib)/sizeof(int), &p, &len, NULL, 0) == -1)
    And why do you alias the p pointer with kp before you access it?

  4. #4
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    I made the changes...

    Code:
    #include <sys/types.h>
    #include <sys/sysctl.h>
    #include <sys/stat.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <utmp.h>
    #include <kvm.h>
    
    int main(void)
    {
    /*ttypf*/
    
        struct stat sb;
        int mib[4];
        size_t len;
        int count;
        struct kinfo_proc p;
    
        if (stat("/dev/ttypq", &sb) != 0) {
            perror("stat");
            return 1;
        }
            
    
        mib[0] = CTL_KERN;
        mib[1] = KERN_PROC;
        mib[2] = KERN_PROC_TTY;
        mib[3] = sb.st_rdev;
    
        len = 0;
        if (sysctl(mib, sizeof(mib)/sizeof(int), NULL , &len, NULL, 0) == -1) 
    {
            perror("sysctl test");
            return 1;
        }
    
       /*p =(struct kinfo_proc *)malloc(len); */
       if (sysctl(mib, sizeof(mib)/sizeof(int), &p, &len, NULL, 0) == -1) 
        {
            perror("sysctl real");
            return 1;
        }
    
        /*count = len / sizeof(kp);*/
       /* kp = p; */
    
        printf("The pid is: %u\n", p.ki_pid);
    
        return 0;
    }
    And now I get

    RobotSex.c: In function `main':
    RobotSex.c:20: error: storage size of 'p' isn't known

    Apparently the structure struct kinfo_proc is in some other header file. I have no idea which one.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    What OS are you compiling/running this on?

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21