#!/bin/sh # # This is a shell archive. To extract its contents, # execute this file with /bin/sh to create the file(s): # # format_ps.README format_ps.c # # This shell archive created: Tue Sep 17 10:36:47 EDT 2002 # echo "Extracting file format_ps.README" sed -e 's/^X//' <<\SHAR_EOF > format_ps.README Xformat_ps.README X X Xformat_ps.c Formats the output of ps -el, shows in a tree-like format X who started what. oninit processes are shown as owning shm X segments (IDS not XPS). X X XN.B. --- this is 'as is' only X X-- X--------------------------------------- XMadison Pruet XEnterprise Replication Product Development XIBM Informix Dynamic Server XDallas, Texas SHAR_EOF if [ `wc -c < format_ps.README` -ne 375 ] then echo "Lengths do not match -- Bad Copy of format_ps.README" fi echo "Extracting file format_ps.c" sed -e 's/^X//' <<\SHAR_EOF > format_ps.c X#include X#include X#include X#include X X#define PERROR(x) {perror((x)); exit(1);} X Xtypedef struct ps_node { X struct ps_node *next; X struct ps_node *next_sib; X struct ps_node *head_child; X struct ps_node *end_child; X int pid; X int ppid; X char *command; X } ps_t; X Xtypedef struct shm_node { X struct shm_node *next; X pid_t cpid; X char key[40]; X } shm_t; X Xshm_t *shmhead = NULL; Xshm_t *shmtail = NULL; Xps_t *pshead = NULL; Xps_t *pstail = NULL; Xps_t *first_ps = NULL; Xps_t *orphan_head = NULL; Xint spaceing = 0; Xps_t *orphan_tail = NULL; X Xps_t* Xallocate_ps_node() X{ ps_t *tps = (ps_t *)calloc(sizeof(char),sizeof(ps_t)); X X if (!tps) X { PERROR("allocate_ps_node() malloc"); X } X return tps; X} X Xshm_t* Xinit_shm_node(pid_t cpid, char *key) X{ shm_t *tps = (shm_t *) calloc(sizeof(char), sizeof(shm_t)); X if (!tps) X { PERROR("allocate_shm_node() calloc"); X } X tps->cpid = cpid; X strcpy(tps->key, key); X if (!shmhead) X shmhead = tps; X else shmtail->next = tps; X shmtail = tps; X} X Xshm_t* Xfind_shm_node(pid_t cpid) X{ shm_t *tps; X X for (tps = shmhead; tps; tps = tps->next) X { if (tps->cpid == cpid) return tps; X } X return NULL; X} X Xvoid Xinsert_ps_node(ps_t *tps) X{ ps_t *tmpps=pshead; X X if (!pshead) X { pshead = tps; X pstail = tps; X } X else X { while ((tmpps->next) && (tmpps->next->pid < tps->pid)) X { tmpps = tmpps->next; X } X if (tmpps->next) X { tps->next = tmpps->next; X tmpps->next = tps; X } X else X { tmpps->next = tps; X } X } X} X Xps_t* Xfind_ps_node (int pid) X{ ps_t *tps; X X for (tps = pshead; tps; tps=tps->next) X { if (tps->pid == pid) break; X } X return tps; X} X X Xvoid Xlink_to_parent(ps_t *tps) X{ ps_t *tpps = find_ps_node(tps->ppid); X X if (tpps) X { if (tpps->head_child) X { tpps->end_child->next_sib = tps; X } X else tpps->head_child = tps; X tpps->end_child = tps; X } X else X { if (orphan_head) orphan_tail->next_sib = tps; X else orphan_head = tps; X orphan_tail = tps; X } X} X Xint Xmax_depth(ps_t *tps) X{ int max_below_me = 0; X int tnum; X while (tps) X { if (tps->head_child) X { tnum = max_depth(tps->head_child); X if (tnum > max_below_me) max_below_me = tnum; X } X tps=tps->next_sib; X } X return (max_below_me + 1); X} X Xvoid Xprint_heading() X{ int tspace = spaceing; X while(tspace-- > 0) printf(" "); X printf("LEVEL PPID PID COMMAND SHM KEY SERVERNUM/SEG\n"); X} X X Xvoid Xprint_this_node(int level, ps_t *tps) X{ int tspace = spaceing - level; X int tlev = level; X shm_t *tpnt; X int first_line = 1; X unsigned long tkey=0; X X while (tlev--) printf("| "); X while (tspace-- > 0) printf(". "); X printf("%2d %6d %6d %-12s",level, tps->ppid, tps->pid, tps->command); X for (tpnt = shmhead; tpnt; tpnt=tpnt->next) X { if (tpnt->cpid == tps->pid) X { if (!first_line) X { tspace = spaceing - level; X tlev = level; X while (tlev--) printf("| "); X while (tspace-- > 0) printf(". "); X printf("%2s %6s %6s %12s","","","",""); X } X first_line = 0; X printf(" %s", tpnt->key); X if (!strcmp(tps->command,"oninit")) X { char *keypnt = tpnt->key; X if (*keypnt == '0') keypnt++; X if (toupper(*keypnt) == 'X') keypnt++; X sscanf(keypnt," %x ",&tkey); X if ((tkey & 0xff00ff00) == 0x52004800) X { int servernum = (((tkey >> 16) & 0xff) - 0x56); X int segmentnum = tkey & 0xff; X printf(" %d/%d", servernum, segmentnum); X } X } X printf("\n"); X } X } X if (first_line) printf("\n"); X} X X Xvoid Xprint_node(ps_t *tps) X{ static int level = -1; X X level++; X while (tps) X { print_this_node(level, tps); X print_node(tps->head_child); X tps=tps->next_sib; X } X level--; X} X X Xvoid Xmain() X{ ps_t *tps=NULL; X int tint; X char buffer[200]; X char *tpnt; X FILE *ps_fd; X FILE *ipcs_fd; X X if ((ps_fd = popen("ps -el","r")) == NULL) X { perror("can not start ps -el\n"); X exit(1); X } X X X fgets(buffer, sizeof(buffer),ps_fd); X while (!feof(ps_fd)) X { if (fgets(buffer, sizeof(buffer), ps_fd)) X { if ((int)strlen(buffer) > 30) X { tps = allocate_ps_node(); X sscanf(buffer,"%*s %*s %*s %i %i", &(tps->pid), &(tps->ppid)); X tpnt = buffer; X while (*tpnt) tpnt++; X if (*(--tpnt) == '\n') *(tpnt--) = '\0'; X while ((*tpnt <= ' ') && (tpnt-- > buffer)); X while ((*tpnt > ' ') && (tpnt-- > buffer)); X tpnt++; X if (!(tps->command = (char*)malloc(strlen(tpnt)+1))) X { PERROR("malloc of command"); X } X strcpy(tps->command,tpnt); X insert_ps_node(tps); X } X } X } X X pclose(ps_fd); X X if ((ipcs_fd = popen("ipcs -pm","r")) == NULL) X { perror("Can not issue \"ipcs -pm\""); X exit(1); X } X X fgets(buffer,sizeof(buffer),ipcs_fd); X while (!feof(ipcs_fd)) X { char fld1[40]; X char fld2[40]; X char fld3[40]; X X if (fgets(buffer,sizeof(buffer),ipcs_fd)) X { if ((int)strlen(buffer) > 30) X { sscanf(buffer,"%s %*s %s %*s %*s %*s %s",fld1, fld2, fld3); X if (!strcmp(fld1,"m")) X { init_shm_node((pid_t)atol(fld3),fld2); X } X } X } X } X pclose(ipcs_fd); X X X X X for (tps = pshead; tps; tps=tps->next) X { if (tps->pid) link_to_parent(tps); X else first_ps = tps; X } X X spaceing= max_depth(first_ps); X X printf("List of processes\n"); X print_heading(); X X print_node(first_ps); X X if (orphan_head) X { spaceing = max_depth(orphan_head); X printf("Orphans\n"); X print_heading(); X print_node(orphan_head); X } X X} X SHAR_EOF if [ `wc -c < format_ps.c` -ne 5482 ] then echo "Lengths do not match -- Bad Copy of format_ps.c" fi echo "Done." exit 0