#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    C function to find a process ID and kill it


    1) What is the C function to obtain a process id of say smbd and nmbd ?

    2) What is the C function to stop a process ID?

    I do not want to use system("killall nmbd") since this will execute a bash command from within a C program. I wanted to use all C function.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    >> 1) What is the C function to obtain a process id of say smbd and nmbd ?
    man 3 sysctl
    In particular, look in the man page of the above function for the sub-sections under CTL_KERN and KERN_PROC_ALL

    >> 2) What is the C function to stop a process ID?
    man 2 kill

    I suggest that u look at the source code for killall, and pull out the relevant sections of code out, for your own app.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    obtaining a process id by using a C function.


    sysctl is a system call that gives you the ability to fine tune kernel parameters.

    I have not figured out how to use the sysctl C function to obtain a process id of say nmbd, smbd or any process by name.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    No, but you can use sysctl to get a list of all the current running processes and their pids. Then, all you have to do is run through the list and compare the names, till you find the process you want. That's how killall does it.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    use sysctl to get a list of all the current running processes and their pids


    I got stuck.

    Please show me the code example of how to use sysctl to get a list of all the current running processes and their pids
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Borrowing heavily from the FreeBSD killall source, I came up with this:
    Code:
    #include <sys/param.h>
    #include <sys/user.h>
    #include <sys/sysctl.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
      struct kinfo_proc *procs = NULL, *newprocs;
      char          thiscmd[MAXCOMLEN + 1];
      pid_t           thispid;
      int           mib[4];
      size_t                miblen;
      int           i, st, nprocs;
      size_t                size;
      
      
      size = 0;
      mib[0] = CTL_KERN;
      mib[1] = KERN_PROC;
      mib[2] = KERN_PROC_ALL;
      mib[3] = 0;
      miblen = 3;
      
      st = sysctl(mib, miblen, NULL, &size, NULL, 0);
      do {
        size += size / 10;
        newprocs = realloc(procs, size);
        if (newprocs == 0) {
          if (procs)
            free(procs);
          errx(1, "could not reallocate memory");
        }
        procs = newprocs;
        st = sysctl(mib, miblen, procs, &size, NULL, 0);
      } while (st == -1 && errno == ENOMEM);
      
      nprocs = size / sizeof(struct kinfo_proc);
    
      /* Now print out the data */
      for (i = 0; i < nprocs; i++) {
        thispid = procs[i].kp_proc.p_pid;
        strncpy(thiscmd, procs[i].kp_proc.p_comm, MAXCOMLEN);
        thiscmd[MAXCOMLEN] = '\0';
        printf("%d\t%s\n", thispid, thiscmd);
      }
      
      /* Clean up */
      free(procs);
      return(0);
    }
    Hope this helps :)
    Last edited by Scorpions4ever; June 20th, 2003 at 11:15 AM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Look for a process running


    The program that Scorpionforever gave did not ran, but gave me
    a compiled error.

    1) What is the number range that MAXCOMLEN must have?
    2) What value do other variable such as KERN_PROC, KERN_PROC_ALL, CTL_KERN must be set to?
    3) When I run this program do I have to include external parameter like ./look_for_process_running2 nmbd.
    I asked because it have the argc and argv in main.
    int main(int argc, char **argv)


    =======================================
    root@galaxian:~# gcc -o look_for_process_running2 look_for_process_running2.c
    look_for_process_running2.c: In function `main':
    look_for_process_running2.c:10: `MAXCOMLEN' undeclared (first use in this function)
    look_for_process_running2.c:10: (Each undeclared identifier is reported only once
    look_for_process_running2.c:10: for each function it appears in.)
    look_for_process_running2.c:10: size of array `thiscmd' has non-integer type
    look_for_process_running2.c:20: `KERN_PROC' undeclared (first use in this function)
    look_for_process_running2.c:21: `KERN_PROC_ALL' undeclared (first use in this function)
    look_for_process_running2.c:39: `errno' undeclared (first use in this function)
    look_for_process_running2.c:39: `ENOMEM' undeclared (first use in this function)
    look_for_process_running2.c:41: sizeof applied to an incomplete type
    look_for_process_running2.c:46: arithmetic on pointer to an incomplete type
    look_for_process_running2.c:46: dereferencing pointer to incomplete type
    look_for_process_running2.c:47: arithmetic on pointer to an incomplete type
    look_for_process_running2.c:47: dereferencing pointer to incomplete type
    ======================================

    1 #include <sys/param.h>
    2 #include <sys/user.h>
    3 #include <sys/sysctl.h>
    4 #include <stdio.h>
    5 #include <stdlib.h>
    6
    7 int main(int argc, char **argv)
    8{
    9 struct kinfo_proc *procs = NULL, *newprocs;
    10 char thiscmd[MAXCOMLEN + 1];
    11 pid_t thispid;
    12 int mib[4];
    13 size_t miblen;
    14 int i, st, nprocs;
    15 size_t size;
    16
    17
    18 size = 0;
    19 mib[0] = CTL_KERN;
    20 mib[1] = KERN_PROC;
    21 mib[2] = KERN_PROC_ALL;
    22 mib[3] = 0;
    23 miblen = 3;
    24
    25 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
    26 do
    27 {
    28 size += size / 10;
    29 newprocs = realloc(procs, size);
    30 if (newprocs == 0)
    31 {
    32 if (procs)
    33 free(procs);
    34 errx(1, "could not reallocate memory");
    35 }
    36 procs = newprocs;
    37 st = sysctl(mib, miblen, procs, &size, NULL, 0);
    38 }
    39 while (st == -1 && errno == ENOMEM);
    40
    41 nprocs = size / sizeof(struct kinfo_proc);
    42
    43 /* Now print out the data */
    44 for (i = 0; i < nprocs; i++)
    45 {
    46 thispid = procs[i].kp_proc.p_pid;
    47 strncpy(thiscmd, procs[i].kp_proc.p_comm, MAXCOMLEN);
    48 thiscmd[MAXCOMLEN] = '\0';
    49 printf("%d\t%s\n", thispid, thiscmd);
    50 }
    51
    52 /* Clean up */
    53 free(procs);
    54 return(0);
    55}

    ==================================
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Oops, I gave you code that works on *BSD (I tested on Free and Open BSD). For Linux, you can try using opendir() and readdir() to read the contents of the /proc directory. Just look for the directories that have all digits and then look in those directories for the exe link. Hope this helps :)

IMN logo majestic logo threadwatch logo seochat tools logo