Information of Processes running on SunSolaris similar to HP pstat_getproc
Hi,
I need some help please. I am trying to port some HP C++ code to Sun Solaris 8 with Forte Compiler 7.
I am trying to get the list of all processes running on my Sun server and all the process information.
I do this on HP using pstat_getproc function which stores all information in a structure pst_status.
On Sun Solaris I did managed to find a structure in pstatus, psinfo and lwpsinfo from procfs.h file. BUT I could not find any function which will return the all processes information in these structures at a given time.
My code which I use on HP is enclosed. I need to do simialr sort of thing so please help/sugget/guide me.
Thanks
Ravi
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
123 // Load the internal list from the system process table.
124 void PsList_c::LoadList( const char *UserName )
125 {
126 long Uid( 0 );
127 struct passwd PwEntry;
128 struct passwd *pPwEntry;
129
130 // Get the UID we wish to use as a filter. Default to running process.
131 if ( UserName == cpNULL ) {
132 Uid = getuid();
133 }
134 else if ( strcmp( UserName, PROC_ALL_USERS ) == 0 ) {
135 Uid = 0;
136 }
137 else {
138 pPwEntry = getpwnam( UserName );
139 if ( pPwEntry == (struct passwd *) 0 ) {
140 Uid = getuid();
141 }
142 else {
143 PwEntry = *pPwEntry;
144 Uid = PwEntry.pw_uid;
145 }
146 }
147
148 long PageSize = sysconf( SCPAGE_SIZE );
149
150 char Buf[PST_CLEN + 1];
151 PstStat_t Pst;
152 int idx( 0 );
153 int count;
154 // Identify us:
155 pid_t MyPid = getpid();
157 // Reset the number of items in our list:
158 NumPsItems = 0;
159
160 String ProgName;
161 String SocketName;
162 static char SepList[] = " "; // separator list for strtok
163
164 // The argument string to look for:
165 String SocketSwitch( "-" );
166 SocketSwitch << SWITCH_SOCKET;
167
168 while ( ( count = pstat_getproc( &Pst, sizeof( Pst ), (size_t) 1, idx ) )
169 > 0 ) {
170 // Set up for the next item in case we continue:
171 idx = (int) Pst.pst_idx + 1;
172 // Only check entries with our user ID and NOT our pid
173 if ( ( Pst.pst_uid == Uid || Uid == 0 ) && Pst.pst_pid != MyPid) {
174 PsItem_t Psi; // PstItem_t is of struct pst_status.
175 char *pWord;
176 strcpy( Buf, Pst.pst_cmd );
177 // We only want to check full words in the command line:
178 pWord = strtok( Buf, SepList ); // command
179 if ( pWord == cpNULL ) {
180 continue;
181 }
182 // As programs are sometimes started with a full path, and
183 // we only want the actual, program name, we must extract it:
184 const char *base = basename( pWord );
185 // If the program name starts with a '-' (a login shell) or
186 // is equal to "ps" or a shell or nice (which is sometimes
187 // used to start the program) then skip it:
188 if ( *base == '-' ||
189 strcmp( base, "ps" ) == 0 ||
190 strcmp( base, "sh" ) == 0 ||
191 strcmp( base, "ksh" ) == 0 ||
192 strcmp( base, "csh" ) == 0 ||
193 strcmp( base, "tcsh" ) == 0 ||
194 strcmp( base, "nice" ) == 0 ) {
195 continue;
196 }
197
198 // Set the program name to the actual program until we
199 // search for a socket argument:
200 ProgName = base;
201 SocketName = base;
202 // search for socket argument:
203 while ( ( pWord = strtok( cpNULL, SepList ) ) != cpNULL ) {
204 if ( strncmp( SocketSwitch.Get(), pWord,
205 SocketSwitch.Length() ) == 0 ) {
206 // If the socket name is part of the word, then use it:
207 if ( strlen( pWord ) > SocketSwitch.Length() ) {
208 SocketName = pWord + SocketSwitch.Length();
209 break;
210 }
211 // Otherwise, it must be the next word so get it:
212 pWord = strtok( cpNULL, SepList );
213 if ( pWord != cpNULL ) {
214 SocketName = pWord;
215 break;
216 }
217 }
218 }
219 // Store the details of each program item in our list:
220 strcpy( Psi.Cmd, Pst.pst_cmd );
221 strcpy( Psi.Name, ProgName.Get() );
222 strcpy( Psi.Socket, SocketName.Get() );
223 Psi.Pid = Pst.pst_pid;
225 Psi.PPid = Pst.pst_ppid;
226 Psi.Start = Pst.pst_start;
227 Psi.Cpu = Pst.pst_utime + Pst.pst_stime;
228 // Do size in actual memory usage in KB:
229 Psi.Size = ( ( Pst.pst_dsize + Pst.pst_tsize + Pst.pst_ssize)
230 * PageSize ) / 1024;
231 Psi.Nice = Pst.pst_nice;
232 if ( NumPsItems >= ArraySize ) {
233 ExtendArray();
234 }
235 pPsItem[NumPsItems] = Psi;
236 NumPsItems++;
237 }
238 }
239 return;
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++