I'm a newbie. I need to print the details of all process on the system. I have redirected the output of "ps -aux"
to a text file and opened it in-order to display. Though I get the required details displayed correctly, I get stack smashing error and then segmentation fault. I could understand the segmentation fault is from one of the fgets
/sscanf
functions. May I know where might have I went wrong?
if ( NULL != ( FileDesc = fopen( FileName , "r" ) ) )
{
if( ! fgets(buf, sizeof( buf ), FileDesc) )
{
Status = -1;
}
while( NULL != fgets( buf, sizeof( buf ), FileDesc ) )
{
sscanf( buf, "%*s %d %*s %s %*d %*d %*s %s %*s %s %[^\n] ",
&(ProcVar[CurrProcessNum].Pid),
&(ProcVar[CurrProcessNum].Size),
(ProcVar[CurrProcessNum].State),
(ProcVar[CurrProcessNum].CpuTime),
(ProcVar[CurrProcessNum].Cmd));
printf (" PID: %d size: %s State: %s CpuTime: %s Cmd %s",
(ProcVar[CurrProcessNum].Pid),
(ProcVar[CurrProcessNum].Size),
(ProcVar[CurrProcessNum].State),
(ProcVar[CurrProcessNum].CpuTime),
(ProcVar[CurrProcessNum].Cmd));
CurrProcessNum ++;
}
}
Sample output is :
PID: 21342 size: 0.0 State: S CpuTime: 0:00 Cmd [kjournald]
PID: 23384 size: 2.6 State: Sl CpuTime: 39:59 Cmd /opt/Adobe/Reader9/Reader/intellinux/bin/acroread /root/Documents/Comcast_RDK2.0-B13.4_Broadcom_release_notes_20140123.pdf
PID: 23495 size: 0.9 State: Ssl CpuTime: 9:01 Cmd gnome-terminal
PID: 23498 size: 0.0 State: S CpuTime: 0:00 Cmd gnome-pty-helper
PID: 23499 size: 0.0 State: Ss CpuTime: 0:00 Cmd bash
PID: 26733 size: 0.1 State: Ss CpuTime: 0:18 Cmd sshd: root@pts/3
PID: 26843 size: 0.2 State: Ss CpuTime: 0:01 Cmd -bash
PID: 26943 size: 0.1 State: Ss CpuTime: 0:06 Cmd sshd: root@notty
PID: 27052 size: 0.0 State: Ss CpuTime: 0:00 Cmd /usr/lib/openssh/sftp-server
PID: 29510 size: 0.0 State: S CpuTime: 0:00 Cmd su root
PID: 29517 size: 0.1 State: S+ CpuTime: 0:04 Cmd bash
PID: 29951 size: 0.1 State: S+ CpuTime: 1:06 Cmd minicom
PID: 30056 size: 0.0 State: Ss+ CpuTime: 0:00 Cmd bash
PID: 30293 size: 0.0 State: Ss CpuTime: 0:00 Cmd bash
PID: 30329 size: 0.0 State: S+ CpuTime: 0:01 Cmd ssh root@192.168.70.54
PID: 30597 size: 0.0 State: Ss CpuTime: 0:00 Cmd bash
PID: 30632 size: 0.0 State: S+ CpuTime: 0:00 Cmd ssh root@192.168.70.54
PID: 31508 size: 0.0 State: Ss+ CpuTime: 0:00 Cmd bash
PID: 31522 size: 0.1 State: Ss+ CpuTime: 0:00 Cmd bash
*** stack smashing detected ***: bin/TR69_DM terminated
Segmentation fault