File: PsTitle.cc

package info (click to toggle)
postman 2.1-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,248 kB
  • ctags: 3,249
  • sloc: cpp: 33,376; ansic: 2,014; sh: 768; makefile: 300
file content (538 lines) | stat: -rw-r--r-- 15,686 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
PsTitle.cc
*/

#include "PsTitle.h"
#include <sys/ioctl.h>
#include <sys/param.h>
#include <limits.h>

char *output;
char *DoprEnd;
int  SnprfOverflow;
int  SyslogErrno;

char * xalloc (register int sz)
  {
  register char *p;

  /* some systems can't handle size zero mallocs */
  if (sz <= 0) sz = 1;

  p = (char *)malloc((unsigned) sz);
  if (p == NULL)
    {
    fprintf (stderr, "!Out of memory!!");
    /* exit(EX_UNAVAILABLE); */
    }
  return (p);
  }

void dopr_outch (int c)
  {
#if 0
  if( iscntrl(c) && c != '\n' && c != '\t' ){
          c = '@' + (c & 0x1F);
          if( DoprEnd == 0 || output < DoprEnd )
                  *output++ = '^';
  }
#endif
  if( DoprEnd == 0 || output < DoprEnd )
          *output++ = c;
  else
           SnprfOverflow++;
  }

void fmtstr (char *value, int ljust, int len, int maxwidth)
  {
  int padlen, strlen;     /* amount to pad */

  if( value == 0 ){
          value = "<NULL>";
  }
  for( strlen = 0; value[strlen]; ++ strlen ); /* strlen */
  if (strlen > maxwidth && maxwidth)
    strlen = maxwidth;
  padlen = len - strlen;
  if( padlen < 0 ) padlen = 0;
  if( ljust ) padlen = -padlen;
  while( padlen > 0 ) {
          dopr_outch( ' ' );
          --padlen;
  }
  dostr( value, maxwidth );
  while( padlen < 0 ) {
          dopr_outch( ' ' );
          ++padlen;
    }
  }

void fmtnum (long value, int base, int dosign, int ljust, int len, int zpad)
  {
  int signvalue = 0;
  unsigned long uvalue;
  char convert[20];
  int place = 0;
  int padlen = 0; /* amount to pad */
  int caps = 0;

  /* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
          value, base, dosign, ljust, len, zpad )); */
  uvalue = value;
  if( dosign ){
          if( value < 0 ) {
                  signvalue = '-';
                  uvalue = -value;
          }
  }
  if( base < 0 ){
          caps = 1;
          base = -base;
  }
  do{
          convert[place++] =
                  (caps? "0123456789ABCDEF":"0123456789abcdef")
                   [uvalue % (unsigned)base  ];
          uvalue = (uvalue / (unsigned)base );
  }while(uvalue);
  convert[place] = 0;
  padlen = len - place;
  if( padlen < 0 ) padlen = 0;
  if( ljust ) padlen = -padlen;
  /* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
          convert,place,signvalue,padlen)); */
  if( zpad && padlen > 0 ){
          if( signvalue ){
                  dopr_outch( signvalue );
                  --padlen;
                  signvalue = 0;
          }
          while( padlen > 0 ){
                  dopr_outch( zpad );
                  --padlen;
          }
  }
  while( padlen > 0 ) {
          dopr_outch( ' ' );
          --padlen;
  }
  if( signvalue ) dopr_outch( signvalue );
  while( place > 0 ) dopr_outch( convert[--place] );
  while( padlen < 0 ){
          dopr_outch( ' ' );
          ++padlen;
  }
  }

void dostr (char *str, int cut)
  {
    if (cut) {
      while(*str && cut-- > 0) dopr_outch(*str++);
    } else {
      while(*str) dopr_outch(*str++);
    }
  }

void sm_dopr (char *buffer, const char *format, va_list args)
  {
       int ch;
       long value;
       int longflag  = 0;
       int pointflag = 0;
       int maxwidth  = 0;
       char *strvalue;
       int ljust;
       int len;
       int zpad;
//# if !HASSTRERROR && !defined(ERRLIST_PREDEFINED)
#if defined (HP) || defined (SUN)
        extern char *sys_errlist[];
        extern int sys_nerr;
#endif        
//# endif                

       output = buffer;
       while( (ch = *format++) != '\0' ){
               switch( ch ){
               case '%':
                       ljust = len = zpad = maxwidth = 0;
                       longflag = pointflag = 0;
               nextch:
                       ch = *format++;
                       switch( ch ){
                       case 0:
                               dostr( "**end of format**" , 0);
                               return;
                       case '-': ljust = 1; goto nextch;
                       case '0': /* set zero padding if len not set */
                               if(len==0 && !pointflag) zpad = '0';
                       case '1': case '2': case '3':
                       case '4': case '5': case '6':
                       case '7': case '8': case '9':
                               if (pointflag)
                                 maxwidth = maxwidth*10 + ch - '0';
                               else
                                 len = len*10 + ch - '0';
                               goto nextch;
                       case '*': 
                               if (pointflag)
                                 maxwidth = va_arg( args, int );
                               else
                                 len = va_arg( args, int );
                               goto nextch;
                       case '.': pointflag = 1; goto nextch;
                       case 'l': longflag = 1; goto nextch;
                       case 'u': case 'U':
                               /*fmtnum(value,base,dosign,ljust,len,zpad) */
                               if( longflag ){
                                       value = va_arg( args, long );
                               } else {
                                       value = va_arg( args, int );
                               }
                               fmtnum( value, 10,0, ljust, len, zpad ); break;
                       case 'o': case 'O':
                               /*fmtnum(value,base,dosign,ljust,len,zpad) */
                               if( longflag ){
                                       value = va_arg( args, long );
                               } else {
                                       value = va_arg( args, int );
                               }
                               fmtnum( value, 8,0, ljust, len, zpad ); break;
                       case 'd': case 'D':
                               if( longflag ){
                                       value = va_arg( args, long );
                               } else {
                                       value = va_arg( args, int );
                               }
                               fmtnum( value, 10,1, ljust, len, zpad ); break;
                       case 'x':
                               if( longflag ){
                                       value = va_arg( args, long );
                               } else {
                                       value = va_arg( args, int );
                               }
                               fmtnum( value, 16,0, ljust, len, zpad ); break;
                       case 'X':
                               if( longflag ){
                                       value = va_arg( args, long );
                               } else {
                                       value = va_arg( args, int );
                               }
                               fmtnum( value,-16,0, ljust, len, zpad ); break;
                       case 's':
                               strvalue = va_arg( args, char *);
                               if (maxwidth > 0 || !pointflag) {
                                 if (pointflag && len > maxwidth)
                                   len = maxwidth; /* Adjust padding */
                                 fmtstr( strvalue,ljust,len,maxwidth);
                               }
                               break;
                       case 'c':
                               ch = va_arg( args, int );
                               dopr_outch( ch ); break;
                       case 'm':
#if HASSTRERROR
                               dostr(strerror(SyslogErrno), 0);
#else
                               if (SyslogErrno < 0 || SyslogErrno >= sys_nerr) 
                               {
                                   dostr("Error ", 0);
                                   fmtnum(SyslogErrno, 10, 0, 0, 0, 0);
                               }
                               else
                                 { 
                                 dostr((char *)sys_errlist[SyslogErrno], 0);
                                 }
#endif
                               break;

                       case '%': dopr_outch( ch ); continue;
                       default:
                               dostr(  "???????" , 0);
                       }
                       break;
               default:
                       dopr_outch( ch );
                       break;
               }
       }
       *output = 0;
  }

    
int avsnprintf (char *str, size_t count, const char *fmt, va_list args)
  {
  str[0] = 0;
  DoprEnd = str + count - 1;
  SnprfOverflow = 0;
  sm_dopr( str, fmt, args );
  if (count > 0) DoprEnd[0] = 0;
  //if (SnprfOverflow && tTd(57, 2))
  //        printf("\navsnprintf overflow, len = %ld, str = %s",
  //                (long) count, shortenstring(str, MAXSHORTSTR));
  return strlen(str);
  }
  
/*
**  PROC_LIST_SET -- set pid task in process list
**
**      Parameters:
**              pid -- pid to set
**              task -- task of pid
**
**      Returns:
**              none.
*/
void proc_list_set (pid_t pid, char *task)
  {
  int i;
  static int   ProcListSize    = 0;
  static struct procs  *ProcListVec    = NULL;
  for (i = 0; i < ProcListSize; i++)
    {
    if (ProcListVec[i].proc_pid == pid)
      {
      if (ProcListVec[i].proc_task != NULL) free(ProcListVec[i].proc_task);
      ProcListVec[i].proc_task = newstr(task);
      break;
      }
    }
  }


/*
**  SETPROCTITLE -- set process title for ps
**
**      Parameters:
**              fmt -- a printf style format string.
**              a, b, c -- possible parameters to fmt.
**
**      Returns:
**              none.
**
**      Side Effects:
**              Clobbers argv of our main procedure so ps(1) will
**              display the title.
*/

#define SPT_NONE        0       /* don't use it at all */
#define SPT_REUSEARGV   1       /* cover argv with title information */
#define SPT_BUILTIN     2       /* use libc builtin */
#define SPT_PSTAT       3       /* use pstat(PSTAT_SETCMD, ...) */
#define SPT_PSSTRINGS   4       /* use PS_STRINGS->... */
#define SPT_SYSMIPS     5       /* use sysmips() supported by NEWS-OS 6 */
#define SPT_SCO         6       /* write kernel u. area */
#define SPT_CHANGEARGV  7       /* write our own strings into argv[] */

//PARA hpux
#if defined (HP)
#define SPT_TYPE       SPT_PSTAT
#endif

#ifndef SPT_TYPE
# define SPT_TYPE       SPT_REUSEARGV
#endif

#if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN

# if SPT_TYPE == SPT_PSTAT
#  include <sys/pstat.h>
# endif
# if SPT_TYPE == SPT_PSSTRINGS
#  include <machine/vmparam.h>
#  include <sys/exec.h>
#  ifndef PS_STRINGS    /* hmmmm....  apparently not available after all */
#   undef SPT_TYPE
#   define SPT_TYPE     SPT_REUSEARGV
#  else
#   ifndef NKPDE                        /* FreeBSD 2.0 */
#    define NKPDE 63
typedef unsigned int    *pt_entry_t;
#   endif
#  endif
# endif
# if SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV
#  define SETPROC_STATIC        static
# else
#  define SETPROC_STATIC
# endif

# if SPT_TYPE == SPT_SYSMIPS
#  include <sys/sysmips.h>
#  include <sys/sysnews.h>
# endif

# if SPT_TYPE == SPT_SCO
#  include <sys/immu.h>
#  include <sys/dir.h>
#  include <sys/user.h>
#  include <sys/fs/s5param.h>
#  if PSARGSZ > MAXLINE
#   define SPT_BUFSIZE  PSARGSZ
#  endif
# endif

# ifndef SPT_PADCHAR
#  define SPT_PADCHAR   ' '
# endif

#endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */

# ifndef SPT_BUFSIZE
#  define SPT_BUFSIZE   MAXLINE
# endif

/*
**  Pointers for setproctitle.
**      This allows "ps" listings to give more useful information.
*/

char            **Argv = NULL;          /* pointer to argument vector */
char            *LastArgv = NULL;       /* end of argv */

void initsetproctitle (int argc, char **argv, char **envp)
  {
  register int i, envpsize = 0;
  extern char **environ;

  /*Move the environment so setproctitle can use the space at the top of memory.*/

  for (i = 0; envp[i] != NULL; i++) envpsize += strlen(envp[i]) + 1;
  environ = (char **) xalloc(sizeof (char *) * (i + 1));
  for (i = 0; envp[i] != NULL; i++) environ[i] = newstr(envp[i]);
  environ[i] = NULL;

  Argv = argv;

  for (i = 0; i < argc; i++)
    {
    if (i==0 || LastArgv + 1 == argv[i]) LastArgv = argv[i] + strlen(argv[i]);
    else continue;
    }     
  for (i=0; envp[i] != NULL; i++)
    {
    if (LastArgv + 1 == envp[i]) LastArgv = envp[i] + strlen(envp[i]);
    else continue;
    }
  }

#if SPT_TYPE != SPT_BUILTIN
void setproctitle (const char *PREFIX, const char *fmt, ...)
{
# if SPT_TYPE != SPT_NONE
        register char *p;
        register int i;
        SETPROC_STATIC char buf[SPT_BUFSIZE];
        VA_LOCAL_DECL
#  if SPT_TYPE == SPT_PSTAT
        union pstun pst;
#  endif
#  if SPT_TYPE == SPT_SCO
        off_t seek_off;
        static int kmem = -1;
        static int kmempid = -1;
        struct user u;
#  endif

        /*AGUSTIN*/for (int h = 0; h < SPT_BUFSIZE; ++h) buf[h] = '\0';
         
        p = buf;

        /* print sendmail: heading for grep */
        (void) strncpy(p, PREFIX, SPT_BUFSIZE); 
        xstrncat (p, SPT_BUFSIZE, " ");
        p += strlen(p);

        /* print the argument string */
        VA_START(fmt);
        (void) avsnprintf(p, SPACELEFT(buf, p), fmt, ap);
        VA_END;

        i = strlen(buf);

#  if SPT_TYPE == SPT_PSTAT
        pst.pst_command = buf;
        pstat(PSTAT_SETCMD, pst, i, 0, 0);
#  endif
#  if SPT_TYPE == SPT_PSSTRINGS
        PS_STRINGS->ps_nargvstr = 1;
        PS_STRINGS->ps_argvstr = buf;
#  endif
#  if SPT_TYPE == SPT_SYSMIPS
        sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf);
#  endif
#  if SPT_TYPE == SPT_SCO
        if (kmem < 0 || kmempid != getpid())
        {
                if (kmem >= 0)
                        close(kmem);
                kmem = open(_PATH_KMEM, O_RDWR, 0);
                if (kmem < 0)
                        return;
                (void) fcntl(kmem, F_SETFD, 1);
                kmempid = getpid();
        }
        buf[PSARGSZ - 1] = '\0';
        seek_off = UVUBLK + (off_t) u.u_psargs - (off_t) &u;
        if (lseek(kmem, (off_t) seek_off, SEEK_SET) == seek_off)
                (void) write(kmem, buf, PSARGSZ);
#  endif

#  if SPT_TYPE == SPT_REUSEARGV


        if (i > LastArgv - Argv[0] - 2)
        {
                i = LastArgv - Argv[0] - 2;
                buf[i] = '\0';
        }
        (void) strncpy(Argv[0], buf, strlen(buf)+1);
        p = &Argv[0][strlen(PREFIX) + strlen(buf)];
        
        while (p < LastArgv) *p++ = SPT_PADCHAR;
        
        Argv[1] = NULL;
#  endif



#  if SPT_TYPE == SPT_CHANGEARGV
        Argv[0] = buf;
        Argv[1] = 0;
#  endif
# endif /* SPT_TYPE != SPT_NONE */
}

#endif /* SPT_TYPE != SPT_BUILTIN */

/*
**  SM_SETPROCTITLE -- set process task and set process title for ps
**
**      Possibly set process status and call setproctitle() to
**      change the ps display.
**
**      Parameters:
**              status -- whether or not to store as process status
**              fmt -- a printf style format string.
**              a, b, c -- possible parameters to fmt.
**
**      Returns:
**              none.
*/
void sm_setproctitle (const char *PREFIX, bool status, const char *fmt, ...)
  {
  char buf[SPT_BUFSIZE];
  VA_LOCAL_DECL
  VA_START(fmt);
    
    
  memset (buf, '\0', SPT_BUFSIZE);  
    
  (void) avsnprintf(buf, SPT_BUFSIZE, fmt, ap);
  VA_END;
  if (status == true) proc_list_set(getpid(), buf);
  
  //DEBUG ("--%d--%s--%s--", SPT_BUFSIZE, fmt, buf);
  setproctitle(PREFIX, "%s", buf);
  }