getrusage patch for Solaris

From: Arjan de Vet <Arjan.deVet@dont-contact.us>
Date: Fri, 21 Feb 1997 08:18:09 +0100 (MET)

Squid on Solaris cannot get its CPU usage with the getrusage call if
uid != euid which is the case if Squid is started as root and running
as nobody. The following patch fixes this:

--- stat.c.orig Sun Jan 19 09:32:13 1997
+++ stat.c Fri Feb 21 07:56:24 1997
@@ -766,7 +766,15 @@
 
 #if HAVE_GETRUSAGE && defined(RUSAGE_SELF)
     storeAppendPrintf(sentry, "{Resource usage for %s:}\n", appname);
+#ifdef _SQUID_SOLARIS_
+ if (getuid() == 0 && geteuid() != 0) {
+ enter_suid();
+ }
+#endif
     getrusage(RUSAGE_SELF, &rusage);
+#ifdef _SQUID_SOLARIS_
+ leave_suid();
+#endif
     storeAppendPrintf(sentry, "{\tCPU Time: %d seconds (%d user %d sys)}\n",
         (int) rusage.ru_utime.tv_sec + (int) rusage.ru_stime.tv_sec,
         (int) rusage.ru_utime.tv_sec,
--- tools.c.orig Mon Jan 13 23:58:04 1997
+++ tools.c Fri Feb 21 07:55:45 1997
@@ -209,7 +209,15 @@
 {
 #if HAVE_GETRUSAGE && defined(RUSAGE_SELF)
     struct rusage rusage;
+#ifdef _SQUID_SOLARIS_
+ if (getuid() == 0 && geteuid() != 0) {
+ enter_suid();
+ }
+#endif
     getrusage(RUSAGE_SELF, &rusage);
+#ifdef _SQUID_SOLARIS_
+ leave_suid();
+#endif
     fprintf(lf, "CPU Usage: user %d sys %d\n",
         (int) rusage.ru_utime.tv_sec, (int) rusage.ru_stime.tv_sec);
 #if defined(_SQUID_SGI_) || defined(_SQUID_OSF_) || defined(BSD4_4)

Here is the answer I got from Sun regarding this 'bug'.

-----------------------------------------------------------------------------
>From: Casper.Dik@Holland.Sun.COM (Casper H.S. Dik - Network Security Engineer)
Newsgroups: comp.unix.solaris
Subject: Re: Bug with getrusage and seteuid but not setuid?
Supersedes: <cancel.casper.856255708@uk-usenet.uk.sun.com>
Date: 18 Feb 1997 08:49:58 GMT
Organization: Sun Microsystems, Netherlands
Lines: 31
Distribution: inet
Message-ID: <casper.856255708@uk-usenet.uk.sun.com>
References: <5earad$rfg@ns1.nl.cis.philips.com>
NNTP-Posting-Host: room101.holland.sun.com

Arjan.deVet@nl.cis.philips.com (Arjan de Vet) writes:

>Consider the program below. When started as root on Solaris 2.5.1 it
>will report "CPU usage: user 0 sys 0". When using setuid instead of
>seteuid it reports a user CPU usage >0 (3 on a Sparc 20 in my case).

>Is this a bug?

> seteuid(60001); /* CPU = 0 */

> getrusage(RUSAGE_SELF, &rusage);

It's an artifact of the getrusage application.

it tries to open /proc/pid and load get the resource usage; unfortunately,
it can't open /proc/pid if the uids don't match sufficiently.

(Though /proc/pid should probably be openable by the process itself always)

In 2.6, process usage is available for all to to as /proc/pid/usage and
the bug is fixed.

Casper

--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
-----------------------------------------------------------------------------
Arjan
--
Ir. Arjan de Vet                       E-mail: Arjan.deVet@nl.cis.philips.com
Origin IT TIS/INS - IntraNet Services        Tel/Fax: (+31 40 27) 86335/83962
Building VN 5.07, P.O. Box 218, 5600 MD  Eindhoven, The Netherlands
Intranet-URL: http://www.nl.cis.philips.com/home/Arjan.deVet/
Received on Thu Feb 20 1997 - 23:29:12 MST

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:34:30 MST