Print this page
3946 ::gcore
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>


   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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  23  *




  24  * Program to examine or set process privileges.
  25  */
  26 
  27 #include <stdio.h>
  28 #include <stdio_ext.h>
  29 #include <stdlib.h>
  30 #include <unistd.h>
  31 #include <fcntl.h>
  32 #include <string.h>
  33 #include <limits.h>
  34 #include <sys/types.h>
  35 #include <libproc.h>
  36 #include <priv.h>
  37 #include <errno.h>
  38 #include <ctype.h>
  39 
  40 #include <locale.h>
  41 #include <langinfo.h>
  42 
  43 static int      look(char *);


 141         }
 142 
 143         if (exec) {
 144                 privupdate_self();
 145                 rc = execvp(argv[0], &argv[0]);
 146                 (void) fprintf(stderr, "%s: %s: %s\n", command, argv[0],
 147                     strerror(errno));
 148         } else if (list) {
 149                 rc = dumppriv(argv);
 150         } else {
 151                 while (argc-- > 0)
 152                         rc += look(*argv++);
 153         }
 154 
 155         return (rc);
 156 }
 157 
 158 static int
 159 look(char *arg)
 160 {
 161         static size_t pprivsz = sizeof (prpriv_t);
 162         static prpriv_t *ppriv;
 163 
 164         struct ps_prochandle *Pr;
 165         int gcode;
 166         size_t sz;
 167         void *pdata;
 168         char *x;
 169         int i;
 170         boolean_t nodata;

 171 
 172         procname = arg;         /* for perr() */
 173 
 174         if ((Pr = proc_arg_grab(arg, set ? PR_ARG_PIDS : PR_ARG_ANY,
 175             PGRAB_RETAIN | PGRAB_FORCE | (set ? 0 : PGRAB_RDONLY) |
 176             PGRAB_NOSTOP, &gcode)) == NULL) {
 177                 (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
 178                     command, arg, Pgrab_error(gcode));
 179                 return (1);
 180         }
 181 
 182         if (ppriv == NULL)
 183                 ppriv = malloc(pprivsz);
 184 
 185         if (Ppriv(Pr, ppriv, pprivsz) == -1) {
 186                 perr(command);
 187                 Prelease(Pr, 0);
 188                 return (1);
 189         }
 190 
 191         sz = PRIV_PRPRIV_SIZE(ppriv);
 192 
 193         /*
 194          * The ppriv fields are unsigned and may overflow, so check them
 195          * separately.  Size must be word aligned, so check that too.
 196          * Make sure size is "smallish" too.
 197          */
 198         if ((sz & 3) || ppriv->pr_nsets == 0 ||
 199             sz / ppriv->pr_nsets < ppriv->pr_setsize ||
 200             ppriv->pr_infosize > sz || sz > 1024 * 1024) {
 201                 (void) fprintf(stderr,
 202                     "%s: %s: bad PRNOTES section, size = %lx\n",
 203                     command, arg, (long)sz);
 204                 Prelease(Pr, 0);

 205                 return (1);
 206         }
 207 
 208         if (sz > pprivsz) {
 209                 ppriv = realloc(ppriv, sz);
 210 
 211                 if (ppriv == NULL || Ppriv(Pr, ppriv, sz) != sz) {
 212                         perr(command);
 213                         Prelease(Pr, 0);
 214                         return (1);
 215                 }
 216                 pprivsz = sz;
 217         }
 218 
 219         if (set) {
 220                 privupdate(ppriv, arg);
 221                 if (Psetpriv(Pr, ppriv) != 0) {
 222                         perr(command);
 223                         Prelease(Pr, 0);

 224                         return (1);
 225                 }
 226                 Prelease(Pr, 0);

 227                 return (0);
 228         }
 229 
 230         if (Pstate(Pr) == PS_DEAD) {
 231                 (void) printf("core '%s' of %d:\t%.70s\n",
 232                     arg, (int)Ppsinfo(Pr)->pr_pid, Ppsinfo(Pr)->pr_psargs);
 233                 pdata = Pprivinfo(Pr);
 234                 nodata = Pstate(Pr) == PS_DEAD && pdata == NULL;
 235         } else {
 236                 (void) printf("%d:\t%.70s\n",
 237                     (int)Ppsinfo(Pr)->pr_pid, Ppsinfo(Pr)->pr_psargs);
 238                 pdata = NULL;
 239                 nodata = B_FALSE;
 240         }
 241 
 242         x = (char *)ppriv + sz - ppriv->pr_infosize;
 243         while (x < (char *)ppriv + sz) {
 244                 /* LINTED: alignment */
 245                 priv_info_t *pi = (priv_info_t *)x;
 246                 priv_info_uint_t *pii;


 281                         extern char *__priv_set_to_str(void *,
 282                             const priv_set_t *, char, int);
 283                         priv_set_t *pset = (priv_set_t *)pc;
 284 
 285                         char *s;
 286 
 287                         if (pdata)
 288                                 s = __priv_set_to_str(pdata, pset, ',', mode);
 289                         else
 290                                 s = priv_set_to_str(pset, ',', mode);
 291                         (void) puts(s);
 292                         free(s);
 293                 } else {
 294                         int j;
 295                         for (j = 0; j < ppriv->pr_setsize; j++)
 296                                 (void) printf("%08x", pc[j]);
 297                         (void) putchar('\n');
 298                 }
 299         }
 300         Prelease(Pr, 0);

 301         return (0);
 302 }
 303 
 304 static void
 305 fatal(const char *s)
 306 {
 307         (void) fprintf(stderr, "%s: %s: %s\n", command, s, strerror(errno));
 308         exit(3);
 309 }
 310 
 311 static void
 312 perr(char *s)
 313 {
 314         int err = errno;
 315 
 316         if (s != NULL)
 317                 (void) fprintf(stderr, "%s: ", procname);
 318         else
 319                 s = procname;
 320 




   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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  23  */
  24 /*
  25  * Copyright (c) 2013 by Delphix. All rights reserved.
  26  */
  27 /*
  28  * Program to examine or set process privileges.
  29  */
  30 
  31 #include <stdio.h>
  32 #include <stdio_ext.h>
  33 #include <stdlib.h>
  34 #include <unistd.h>
  35 #include <fcntl.h>
  36 #include <string.h>
  37 #include <limits.h>
  38 #include <sys/types.h>
  39 #include <libproc.h>
  40 #include <priv.h>
  41 #include <errno.h>
  42 #include <ctype.h>
  43 
  44 #include <locale.h>
  45 #include <langinfo.h>
  46 
  47 static int      look(char *);


 145         }
 146 
 147         if (exec) {
 148                 privupdate_self();
 149                 rc = execvp(argv[0], &argv[0]);
 150                 (void) fprintf(stderr, "%s: %s: %s\n", command, argv[0],
 151                     strerror(errno));
 152         } else if (list) {
 153                 rc = dumppriv(argv);
 154         } else {
 155                 while (argc-- > 0)
 156                         rc += look(*argv++);
 157         }
 158 
 159         return (rc);
 160 }
 161 
 162 static int
 163 look(char *arg)
 164 {



 165         struct ps_prochandle *Pr;
 166         int gcode;
 167         size_t sz;
 168         void *pdata;
 169         char *x;
 170         int i;
 171         boolean_t nodata;
 172         prpriv_t *ppriv;
 173 
 174         procname = arg;         /* for perr() */
 175 
 176         if ((Pr = proc_arg_grab(arg, set ? PR_ARG_PIDS : PR_ARG_ANY,
 177             PGRAB_RETAIN | PGRAB_FORCE | (set ? 0 : PGRAB_RDONLY) |
 178             PGRAB_NOSTOP, &gcode)) == NULL) {
 179                 (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
 180                     command, arg, Pgrab_error(gcode));
 181                 return (1);
 182         }
 183 
 184         if (Ppriv(Pr, &ppriv) == -1) {



 185                 perr(command);
 186                 Prelease(Pr, 0);
 187                 return (1);
 188         }

 189         sz = PRIV_PRPRIV_SIZE(ppriv);
 190 
 191         /*
 192          * The ppriv fields are unsigned and may overflow, so check them
 193          * separately.  Size must be word aligned, so check that too.
 194          * Make sure size is "smallish" too.
 195          */
 196         if ((sz & 3) || ppriv->pr_nsets == 0 ||
 197             sz / ppriv->pr_nsets < ppriv->pr_setsize ||
 198             ppriv->pr_infosize > sz || sz > 1024 * 1024) {
 199                 (void) fprintf(stderr,
 200                     "%s: %s: bad PRNOTES section, size = %lx\n",
 201                     command, arg, (long)sz);
 202                 Prelease(Pr, 0);
 203                 free(ppriv);
 204                 return (1);
 205         }
 206 











 207         if (set) {
 208                 privupdate(ppriv, arg);
 209                 if (Psetpriv(Pr, ppriv) != 0) {
 210                         perr(command);
 211                         Prelease(Pr, 0);
 212                         free(ppriv);
 213                         return (1);
 214                 }
 215                 Prelease(Pr, 0);
 216                 free(ppriv);
 217                 return (0);
 218         }
 219 
 220         if (Pstate(Pr) == PS_DEAD) {
 221                 (void) printf("core '%s' of %d:\t%.70s\n",
 222                     arg, (int)Ppsinfo(Pr)->pr_pid, Ppsinfo(Pr)->pr_psargs);
 223                 pdata = Pprivinfo(Pr);
 224                 nodata = Pstate(Pr) == PS_DEAD && pdata == NULL;
 225         } else {
 226                 (void) printf("%d:\t%.70s\n",
 227                     (int)Ppsinfo(Pr)->pr_pid, Ppsinfo(Pr)->pr_psargs);
 228                 pdata = NULL;
 229                 nodata = B_FALSE;
 230         }
 231 
 232         x = (char *)ppriv + sz - ppriv->pr_infosize;
 233         while (x < (char *)ppriv + sz) {
 234                 /* LINTED: alignment */
 235                 priv_info_t *pi = (priv_info_t *)x;
 236                 priv_info_uint_t *pii;


 271                         extern char *__priv_set_to_str(void *,
 272                             const priv_set_t *, char, int);
 273                         priv_set_t *pset = (priv_set_t *)pc;
 274 
 275                         char *s;
 276 
 277                         if (pdata)
 278                                 s = __priv_set_to_str(pdata, pset, ',', mode);
 279                         else
 280                                 s = priv_set_to_str(pset, ',', mode);
 281                         (void) puts(s);
 282                         free(s);
 283                 } else {
 284                         int j;
 285                         for (j = 0; j < ppriv->pr_setsize; j++)
 286                                 (void) printf("%08x", pc[j]);
 287                         (void) putchar('\n');
 288                 }
 289         }
 290         Prelease(Pr, 0);
 291         free(ppriv);
 292         return (0);
 293 }
 294 
 295 static void
 296 fatal(const char *s)
 297 {
 298         (void) fprintf(stderr, "%s: %s: %s\n", command, s, strerror(errno));
 299         exit(3);
 300 }
 301 
 302 static void
 303 perr(char *s)
 304 {
 305         int err = errno;
 306 
 307         if (s != NULL)
 308                 (void) fprintf(stderr, "%s: ", procname);
 309         else
 310                 s = procname;
 311