Wednesday, April 18, 2012

Run SYSTEM command and get output in C

#include 
#include
int main( int argc, char *argv[] )
{
FILE *fp;
int status;
char path[1035];
/* Open the command for reading. */
fp = popen("/bin/ls /etc/", "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit;
}
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path)-1, fp) != NULL) {
printf("%s", path);
}
/* close */ pclose(fp);
return 0;
}




We can parse the string path by following concept for example

(Consider my input) -> hi, Me A. 2154
it suppose to return:
hi
Me
A
2154



Then following Program will help you,



#include
#include
int main(void)
{
char message[50], *current, *last;

gets(message);
last=message;

current=strtok(last, ",");
puts(current);

current=strtok(NULL, " ");
puts(current);

current=strtok(NULL, ".");
puts(current);

current=strtok(NULL, " ");
puts(current);

current=strtok(NULL, " ");
puts(current);

current=strtok(NULL, " ");
puts(current);

return;
}



STRTOK()

A sequence of calls to strtok() breaks the string pointed to by s1 into a sequence of tokens,
each of which is delimited by a byte from the string pointed to by s2.
The first call in the sequence has s1 as its first argument,
and is followed by calls with a null pointer as their first argument.
The separator string pointed to by s2 may be different from call to call.

Thursday, April 5, 2012

DTRACE GETTING STARTED


Quick-start guide


FreeBSD 9-CURRENT (or later)

  1. Compile KDTRACE_HOOKS and DDB_CTF into your kernel. On AMD64 you'll also need KDTRACE_FRAME and enable gdb(1) debug symbols.
    options KDTRACE_HOOKS        # all architectures - enable general DTrace hooks 
    options DDB_CTF              # all architectures - kernel ELF linker loads CTF data  
    options KDTRACE_FRAME        # amd64 - ensure frames are compiled in  
    makeoptions DEBUG="-g"       # amd64? - build kernel with gdb(1) debug symbols  
    makeoptions WITH_CTF=1
    Note: WITH_CTF=1 has to be defined in the kernel config file. It will not be picked up from make.conf or src.conf for the kernel build.
  2. Recompile and install your kernel; then reboot:
    make buildkernel KERNCONF=DTRACE  
    make installkernel KERNCONF=DTRACE shutdown -r NOW
  3. Load some or all DTrace kernel modules:
    kldload dtraceall
  4. Confirm that you have piles of available DTrace hooks:
    dtrace -l | head
    You'll need to su to root in order to use DTrace, which may be fixed once we add a more comprehensive fine-grained privilege policy.
  5. For userland DTrace support add the following to your make.conf: (optional)
    STRIP= CFLAGS+=-fno-omit-frame-pointer
    This allows stack traces to work and display even more information.
  6. Rebuild and install world with 'WITH_CTF=1' in either make.conf (if you also want to have it for ports) or src.conf: (optional)
    make buildworld shutdown -r NOW boot -s 
    make installworld reboot

FreeBSD 8-STABLE (or earlier)

  1. Compile KDTRACE_HOOKS and DDB_CTF into your kernel. On AMD64 you'll also need KDTRACE_FRAME and enable gdb(1) debug symbols.
    options KDTRACE_HOOKS      # all architectures - enable general DTrace hooks  
    options DDB_CTF            # all architectures - kernel ELF linker loads CTF data  
    options KDTRACE_FRAME      # amd64 - ensure frames are compiled in  
    makeoptions DEBUG="-g"     # amd64? - build kernel with gdb(1) debug symbols
    Note: WITH_CTF=1 has to be specified on the command line. It will not be picked up from either make.conf, src.conf or inside the kernel config file for the kernel build.
  2. Recompile and install your kernel; then reboot:
    make buildkernel WITH_CTF=1 KERNCONF=DTRACE 
    make installkernel KERNCONF=DTRACE shutdown -r NOW
  3. Load some or all DTrace kernel modules:
    kldload dtraceall
  4. Confirm that you have piles of available DTrace hooks:
    dtrace -l | head
    You'll need to su to root in order to use DTrace, which may be fixed once we add a more comprehensive fine-grained privilege policy.
  5. For userland DTrace support add the following to your make.conf: (optional)
    STRIP= CFLAGS+=-fno-omit-frame-pointer
    This allows stack traces to work and display even more information.
  6. Rebuild and install world: (optional)
    make WITH_CTF=1 buildworld shutdown -r NOW boot -s 
    make installworld






    Using DTrace

    Before making use of DTrace functionality, the DTrace device must exist. To load the device, issue the following command:
    # kldload dtraceall 
    DTrace support should now be available. To view all probes the administrator may now execute the following command:
    # dtrace -l | more
    
    
    
    
    
    
    
    
    Probes
     • Data is generated from instrumentation points called “probes”. • DTrace provides thousands of probes.
     • Probe examples: Probe Name                Description syscall::read:entry     A read() syscall began proc:::exec-success     A process created successfully io:::start              An I/O was issued (disk/vol/NFS) io:::done               An I/O completed
    
    
     Probe Names • Probe names are a four-tuple,
     Provider:Module:Function:Name
     syscall::exece:return
    
    
     > Provider : A library of related probes.
    > Module :The module the function belongs to,either a kernel module or user segment. > Function : The function name that contains the probe.
    > Name : The name of the probe.