/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2015, Joyent, Inc.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <limits.h>
#include <errno.h>
#include <libproc.h>
#include <priv.h>
#define FALSE 0
static int interrupt;
static char *command;
static int Fflag;
static void intr(int);
static int parse_limits(int, char *);
static void show_limits(struct ps_prochandle *);
static int set_limits(struct ps_prochandle *);
static void
usage()
{
"usage:\n"
" For each process, report all resource limits:\n"
"\t%s [-km] pid ...\n"
"\t-k\treport file sizes in kilobytes\n"
" For each process, set specified resource limits:\n"
"\t%s -{cdfnstv} soft,hard ... pid ...\n"
"\t-c soft,hard\tset core file size limits\n"
"\t-d soft,hard\tset data segment (heap) size limits\n"
"\t-f soft,hard\tset file size limits\n"
"\t-n soft,hard\tset file descriptor limits\n"
"\t-s soft,hard\tset stack segment size limits\n"
"\t-t soft,hard\tset CPU time limits\n"
"\t-v soft,hard\tset virtual memory size limits\n"
"\t(default units are as shown by the output of '%s pid')\n",
exit(2);
}
int
{
int retc = 0;
int opt;
int errflg = 0;
command++;
else
switch (opt) {
case 'F': /* force grabbing (no O_EXCL) */
Fflag = PGRAB_FORCE;
break;
case 'k':
break;
case 'm':
break;
case 'c': /* core file size */
break;
case 'd': /* data segment size */
break;
case 'f': /* file size */
break;
case 'n': /* file descriptors */
break;
case 's': /* stack segment size */
break;
case 't': /* CPU time */
break;
case 'v': /* virtual memory size */
break;
default:
errflg = 1;
break;
}
}
usage();
/* catch signals from terminal */
char *arg;
int gret;
/* get the specified pid and the psinfo struct */
retc = 1;
if (Pcreate_agent(Pr) == 0) {
if (set) {
if (set_limits(Pr) != 0)
retc = 1;
} else {
(void) printf("%d:\t%.70s\n",
}
} else {
"%s: cannot control process %d\n",
retc = 1;
}
} else {
(void) printf(" [system process]\n");
else
} else {
"%s: %s: %d\n",
retc = 1;
}
}
}
if (interrupt)
retc = 1;
return (retc);
}
static void
{
}
/* ------ begin specific code ------ */
/*
* Compute a limit, given a string:
* unlimited unlimited
* nnn k nnn kilobytes
* nnn m nnn megabytes (minutes for CPU time)
* nnn h nnn hours (for CPU time only)
* mm : ss minutes and seconds (for CPU time only)
*/
static int
{
char *lastc;
*limit = RLIM64_INFINITY;
return (0);
}
return (1);
return (1);
sec = 0;
else {
return (1);
}
if (value > RLIM64_INFINITY)
return (0);
}
case 'k':
unit = 1024;
*lastc = '\0';
break;
case 'm':
if (which == RLIMIT_CPU)
unit = 60;
else
*lastc = '\0';
break;
case 'h':
if (which == RLIMIT_CPU)
else
return (1);
*lastc = '\0';
break;
default:
switch (which) {
}
break;
}
return (1);
if (value > RLIM64_INFINITY)
return (0);
}
static int
{
char *lastc;
return (1);
} else {
return (1);
}
} else {
return (1);
}
return (1);
return (0);
}
static void
{
}
static char *
{
char *s1;
char *s2;
s1 = "unlimited";
else {
}
s2 = "unlimited";
else {
}
return (buffer);
}
static void
{
int resource;
char *s;
(void) printf(" resource\t\t current\t maximum\n");
continue;
switch (resource) {
case RLIMIT_CPU:
s = " time(seconds)\t\t";
break;
case RLIMIT_FSIZE:
if (kbytes) {
s = " file(kbytes)\t\t";
} else if (mbytes) {
s = " file(mbytes)\t\t";
} else {
s = " file(blocks)\t\t";
}
break;
case RLIMIT_DATA:
if (mbytes) {
s = " data(mbytes)\t\t";
} else {
s = " data(kbytes)\t\t";
}
break;
case RLIMIT_STACK:
if (mbytes) {
s = " stack(mbytes)\t\t";
} else {
s = " stack(kbytes)\t\t";
}
break;
case RLIMIT_CORE:
if (kbytes) {
s = " coredump(kbytes)\t";
} else if (mbytes) {
s = " coredump(mbytes)\t";
} else {
s = " coredump(blocks)\t";
}
break;
case RLIMIT_NOFILE:
s = " nofiles(descriptors)\t";
break;
case RLIMIT_VMEM:
if (mbytes) {
s = " vmemory(mbytes)\t";
} else {
s = " vmemory(kbytes)\t";
}
break;
default:
s = buf;
break;
}
}
}
static int
{
int be_su = 0;
int ret = 0;
"%s: unable to get process limit for pid %d: %s\n",
return (1);
}
if (!set_current[which])
if (!set_maximum[which])
be_su = 1;
if (be_su) {
if (new_prpriv == NULL) {
"%s: unable to get process privileges for pid"
return (1);
}
/*
* We only have to change the process privileges if it doesn't
* already have PRIV_SYS_RESOURCE. In addition, we want to make
* sure that we don't leave a process with elevated privileges,
* so we make sure the process dies if we exit unexpectedly.
*/
eset = (priv_set_t *)
pset = (priv_set_t *)
/* Keep track of original privileges */
if (old_prpriv == NULL) {
"%s: unable to get process privileges "
"for pid %d: %s\n", command,
return (1);
}
"%s: unable to set process privileges for"
" pid %d: %s\n", command,
return (1);
}
}
}
"%s: cannot set resource limit for pid %d: %s\n",
ret = 1;
}
if (old_prpriv != NULL) {
/*
* If this fails, we can't leave a process hanging
* around with elevated privileges, so we'll have to
* release the process from libproc, knowing that it
* will be killed (since we set PR_KLC).
*/
"%s: cannot relinquish privileges for pid %d."
" The process was killed.",
ret = 1;
}
"%s: cannot relinquish privileges for pid %d."
" The process was killed.",
ret = 1;
}
}
if (new_prpriv != NULL)
return (ret);
}
static int
{
int which;
int retc = 0;
retc = 1;
}
}
return (retc);
}