Print this page
2989 Eliminate use of LOGNAME_MAX in ON
1166 useradd have warning with name more 8 chars


   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*


  22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  27 /*        All Rights Reserved   */
  28 
  29 /*
  30  * University Copyright- Copyright (c) 1982, 1986, 1988
  31  * The Regents of the University of California
  32  * All Rights Reserved
  33  *
  34  * University Acknowledgment- Portions of this document are derived from
  35  * software developed by the University of California, Berkeley, and its
  36  * contributors.
  37  */
  38 
  39 /*
  40  * This is the new w command which takes advantage of
  41  * the /proc interface to gain access to the information


  52 #include <stdio.h>
  53 #include <string.h>
  54 #include <stdarg.h>
  55 #include <stdlib.h>
  56 #include <ctype.h>
  57 #include <fcntl.h>
  58 #include <time.h>
  59 #include <errno.h>
  60 #include <sys/types.h>
  61 #include <utmpx.h>
  62 #include <sys/stat.h>
  63 #include <dirent.h>
  64 #include <procfs.h>               /* /proc header file */
  65 #include <locale.h>
  66 #include <unistd.h>
  67 #include <sys/loadavg.h>
  68 #include <limits.h>
  69 #include <priv_utils.h>
  70 
  71 /*
  72  * utmpx defines wider fields for user and line.  For compatibility of output,
  73  * we are limiting these to the old maximums in utmp. Define UTMPX_NAMELEN
  74  * to use the full lengths.
  75  */
  76 #ifndef UTMPX_NAMELEN
  77 /* XXX - utmp - fix name length */
  78 #define NMAX            (_POSIX_LOGIN_NAME_MAX - 1)
  79 #define LMAX            12
  80 #else   /* UTMPX_NAMELEN */
  81 static struct utmpx dummy;
  82 #define NMAX            (sizeof (dummy.ut_user))
  83 #define LMAX            (sizeof (dummy.ut_line))
  84 #endif /* UTMPX_NAMELEN */
  85 




  86 #define DIV60(t)        ((t+30)/60)     /* x/60 rounded */
  87 
  88 #ifdef ERR
  89 #undef ERR
  90 #endif
  91 #define ERR             (-1)
  92 
  93 #define HSIZE           256             /* size of process hash table   */
  94 #define PROCDIR         "/proc"
  95 #define INITPROCESS     (pid_t)1        /* init process pid */
  96 #define NONE            'n'             /* no state */
  97 #define RUNNING         'r'             /* runnable process */
  98 #define ZOMBIE          'z'             /* zombie process */
  99 #define VISITED         'v'             /* marked node as visited */
 100 #define PRINTF(a)       if (printf a < 0) { \
 101                 perror((gettext("%s: printf failed"), prog)); \
 102                 exit(1); }
 103 
 104 struct uproc {
 105         pid_t   p_upid;                 /* process id */


 466                 }
 467         }
 468 
 469         /* revert to non-privileged user after opening */
 470         (void) __priv_relinquish();
 471 
 472         (void) closedir(dirp);
 473         (void) time(&now);  /* get current time */
 474 
 475         /*
 476          * loop through utmpx file, printing process info
 477          * about each logged in user
 478          */
 479         for (ut = utmpbegin; ut < utmpend; ut++) {
 480                 if (ut->ut_type != USER_PROCESS)
 481                         continue;
 482                 if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
 483                         continue;       /* we're looking for somebody else */
 484 
 485                 /* print login name of the user */
 486                 PRINTF(("%-*.*s ", NMAX, NMAX, ut->ut_name));
 487 
 488                 /* print tty user is on */
 489                 if (lflag) {
 490                         PRINTF(("%-*.*s", LMAX, LMAX, ut->ut_line));
 491                 } else {
 492                         if (ut->ut_line[0] == 'p' && ut->ut_line[1] == 't' &&
 493                             ut->ut_line[2] == 's' && ut->ut_line[3] == '/') {
 494                                 PRINTF(("%-*.3s", LMAX, &ut->ut_line[4]));
 495                         } else {
 496                                 PRINTF(("%-*.*s", LMAX, LMAX, ut->ut_line));

 497                         }
 498                 }
 499 
 500                 /* print when the user logged in */
 501                 if (lflag) {
 502                         time_t tim = ut->ut_xtime;
 503                         prtat(&tim);
 504                 }
 505 
 506                 /* print idle time */
 507                 idle = findidle(ut->ut_line);
 508                 if (idle >= 36 * 60) {
 509                         PRINTF((dcgettext(NULL, "%2ddays ", LC_TIME),
 510                             (idle + 12 * 60) / (24 * 60)));
 511                 } else
 512                         prttime(idle, " ");
 513                 showtotals(findhash(ut->ut_pid));
 514         }
 515         if (fclose(stdout) == EOF) {
 516                 perror((gettext("%s: fclose failed"), prog));




   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2013 Gary Mills
  23  *
  24  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  25  * Use is subject to license terms.
  26  */
  27 
  28 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  29 /*        All Rights Reserved   */
  30 
  31 /*
  32  * University Copyright- Copyright (c) 1982, 1986, 1988
  33  * The Regents of the University of California
  34  * All Rights Reserved
  35  *
  36  * University Acknowledgment- Portions of this document are derived from
  37  * software developed by the University of California, Berkeley, and its
  38  * contributors.
  39  */
  40 
  41 /*
  42  * This is the new w command which takes advantage of
  43  * the /proc interface to gain access to the information


  54 #include <stdio.h>
  55 #include <string.h>
  56 #include <stdarg.h>
  57 #include <stdlib.h>
  58 #include <ctype.h>
  59 #include <fcntl.h>
  60 #include <time.h>
  61 #include <errno.h>
  62 #include <sys/types.h>
  63 #include <utmpx.h>
  64 #include <sys/stat.h>
  65 #include <dirent.h>
  66 #include <procfs.h>               /* /proc header file */
  67 #include <locale.h>
  68 #include <unistd.h>
  69 #include <sys/loadavg.h>
  70 #include <limits.h>
  71 #include <priv_utils.h>
  72 
  73 /*
  74  * Use the full lengths from utmpx for user and line.


  75  */





  76 static struct utmpx dummy;
  77 #define NMAX            (sizeof (dummy.ut_user))
  78 #define LMAX            (sizeof (dummy.ut_line))

  79 
  80 /* Print minimum field widths. */
  81 #define LOGIN_WIDTH     8
  82 #define LINE_WIDTH      12
  83 
  84 #define DIV60(t)        ((t+30)/60)     /* x/60 rounded */
  85 
  86 #ifdef ERR
  87 #undef ERR
  88 #endif
  89 #define ERR             (-1)
  90 
  91 #define HSIZE           256             /* size of process hash table   */
  92 #define PROCDIR         "/proc"
  93 #define INITPROCESS     (pid_t)1        /* init process pid */
  94 #define NONE            'n'             /* no state */
  95 #define RUNNING         'r'             /* runnable process */
  96 #define ZOMBIE          'z'             /* zombie process */
  97 #define VISITED         'v'             /* marked node as visited */
  98 #define PRINTF(a)       if (printf a < 0) { \
  99                 perror((gettext("%s: printf failed"), prog)); \
 100                 exit(1); }
 101 
 102 struct uproc {
 103         pid_t   p_upid;                 /* process id */


 464                 }
 465         }
 466 
 467         /* revert to non-privileged user after opening */
 468         (void) __priv_relinquish();
 469 
 470         (void) closedir(dirp);
 471         (void) time(&now);  /* get current time */
 472 
 473         /*
 474          * loop through utmpx file, printing process info
 475          * about each logged in user
 476          */
 477         for (ut = utmpbegin; ut < utmpend; ut++) {
 478                 if (ut->ut_type != USER_PROCESS)
 479                         continue;
 480                 if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
 481                         continue;       /* we're looking for somebody else */
 482 
 483                 /* print login name of the user */
 484                 PRINTF(("%-*.*s ", LOGIN_WIDTH, NMAX, ut->ut_name));
 485 
 486                 /* print tty user is on */
 487                 if (lflag) {
 488                         PRINTF(("%-*.*s", LINE_WIDTH, LMAX, ut->ut_line));
 489                 } else {
 490                         if (ut->ut_line[0] == 'p' && ut->ut_line[1] == 't' &&
 491                             ut->ut_line[2] == 's' && ut->ut_line[3] == '/') {
 492                                 PRINTF(("%-*.3s", LMAX, &ut->ut_line[4]));
 493                         } else {
 494                                 PRINTF(("%-*.*s", LINE_WIDTH, LMAX,
 495                                     ut->ut_line));
 496                         }
 497                 }
 498 
 499                 /* print when the user logged in */
 500                 if (lflag) {
 501                         time_t tim = ut->ut_xtime;
 502                         prtat(&tim);
 503                 }
 504 
 505                 /* print idle time */
 506                 idle = findidle(ut->ut_line);
 507                 if (idle >= 36 * 60) {
 508                         PRINTF((dcgettext(NULL, "%2ddays ", LC_TIME),
 509                             (idle + 12 * 60) / (24 * 60)));
 510                 } else
 511                         prttime(idle, " ");
 512                 showtotals(findhash(ut->ut_pid));
 513         }
 514         if (fclose(stdout) == EOF) {
 515                 perror((gettext("%s: fclose failed"), prog));