[comp.unix.wizards] multi/single-user mode flag?

okunewck@gondor.cs.psu.edu (Phil OKunewick) (05/13/88)

	Is there any nice way for a program to tell whether a generic
unix system is in single-user or multi-user mode?  Two ideas here are
to read init's memory (not standard under different unixes) or to check
what processes are running (kludge).

	We're running several different versions of unix here, and need
a standard dump routine for everything.  Even if dump is modified to do
sane backups on a live filesystem, multi-user mode dumps are impractical.
(20 hours for a Level 0 dump on one of our machines).
	Therefore, I want something that will tell my operator "Do not do
it this way you fool" if he tries to dump in multi-user mode.

						---Phil OKunewick
						okunewck@psuvax1.cs.psu.edu

tony@oha.UUCP (Tony Olekshy) (05/15/88)

In <3595@psuvax1.psu.edu> okunewck@gondor.cs.psu.edu (Phil OKunewick) writes:
>
> Therefore, I want something that will tell my operator "Do not do it this
> way you fool" if he tries to dump in multi-user mode.

We are running Xenix V here, and Xenix V uses /.profile before $HOME/.profile
only when starting the top-level single-user-mode sh.  I have placed:

			SU_MODE=1; export SU_MODE

in /.profile only, so I can always determine if I am in single user mode just
by examining "$SU_MODE".  If your system lets you determine if you are in
single user mode during the shell startup script then you can use this too.

root@conexch.UUCP (Larry Dighera) (05/16/88)

In article <243@oha.UUCP> tony@oha.UUCP (Tony Olekshy) writes:
>In <3595@psuvax1.psu.edu> okunewck@gondor.cs.psu.edu (Phil OKunewick) writes:
>>
>> Therefore, I want something that will tell my operator "Do not do it this
>> way you fool" if he tries to dump in multi-user mode.
>
>We are running Xenix V here, and Xenix V uses /.profile before $HOME/.profile

I think Tony means /etc/profile not /.profile.  /.profile is traditionally
root's $HOME/.profile.

>only when starting the top-level single-user-mode sh.  I have placed:
>
>			SU_MODE=1; export SU_MODE
>
>in /.profile only, so I can always determine if I am in single user mode just
>by examining "$SU_MODE".  If your system lets you determine if you are in
>single user mode during the shell startup script then you can use this too.

What happens when the 'init s' or 'init 1' is issued?  I don't think Tony's
fix is general enough for Phil's  problem.

The normal way for a Sys V user to determine which run level state the system 
is in, is to do a 'who -r'.  Surely this could be exploited for Phil's 
purpose, although his original article didn't indicate what version of Unix
is being run at his site.  Who's -r option isn't supported yet in SCO's Xenix, but should 
be soon.

Hope this helps.
 
Larry Dighera

-- 
USPS: The Consultants' Exchange, PO Box 12100, Santa Ana, CA  92712
TELE: (714) 842-6348: BBS (N81); (714) 842-5851: Xenix guest account (E71)
UUCP: conexch Any ACU 2400 17148425851 ogin:-""-ogin:-""-ogin: nuucp
UUCP: ...!ucbvax!ucivax!icnvax!conexch!root || ...!trwrb!ucla-an!conexch!root

rogers@ofc.Columbia.NCR.COM (H. L. Rogers) (05/17/88)

In article <340@conexch.UUCP> root@conexch.UUCP (Larry Dighera) writes:
>
>The normal way for a Sys V user to determine which run level state the system 
>is in, is to do a 'who -r'.

True, but since there is no 'standard' which specifies the system state for
a particular run level number, a user program (application?) risks
portability problems from vendor to vendor.  Does anyone know of any
standards work to abstract the numerical run level for Un*x systems?
-- 

------------
HL Rogers    (hl.rogers@ncrcae.Columbia.NCR.COM)

mjy@sdti.UUCP (Michael J. Young) (05/18/88)

In article <166@ofc.Columbia.NCR.COM> rogers@ofc.UUCP (H. L. Rogers) writes:
>In article <340@conexch.UUCP> root@conexch.UUCP (Larry Dighera) writes:
>>The normal way for a Sys V user to determine which run level state the system 
>>is in, is to do a 'who -r'.
>True, but since there is no 'standard' which specifies the system state for
>a particular run level number, a user program (application?) risks
>portability problems from vendor to vendor.  Does anyone know of any
>standards work to abstract the numerical run level for Un*x systems?

I don't know of a standard way, but this should work for System V at least.
No flames for style or accuracy.  Not a shar.
--------------------- cut here ---------------- cut here -------------------
# include <stdio.h>
# include <sys/types.h>
# include <utmp.h>

extern struct utmp *getutid ();
extern void utmpname ();

int main (){
	struct utmp *buf;	/* will hold utmp entry */
	struct utmp buf2;	/* dummy entry to hold search type */

	/*
	 * look for an entry in the utmp file that is of type RUN_LVL.
	 * The ut_line member of this entry will contain the string 
	 * "run-level %c", where %c is [sS1-6].
         */
	buf2.ut_type = RUN_LVL;
	utmpname ("/etc/utmp");	/* not really necessary */
	if ((buf = getutid (&buf2)) == NULL){
		perror ("rlvl: could not find RUN_LVL entry in /etc/utmp");
		exit (1);
	}
	else {
		printf ("%c\n", buf->ut_line[10]);
	}
	exit (0);
}
-- 
Mike Young - Software Development Technologies, Inc., Sudbury MA 01776
UUCP     : {decvax,harvard,linus,mit-eddie}!necntc!necis!mrst!sdti!mjy
Internet : mjy%sdti.uucp@harvard.harvard.edu      Tel: +1 617 443 5779
"Bill & Opus in '88" -- Consider the alternatives!

allbery@ncoast.UUCP (Brandon S. Allbery) (05/21/88)

As quoted from <3595@psuvax1.psu.edu> by okunewck@gondor.cs.psu.edu (Phil OKunewick):
+---------------
| 	Is there any nice way for a program to tell whether a generic
| unix system is in single-user or multi-user mode?  Two ideas here are
| to read init's memory (not standard under different unixes) or to check
| what processes are running (kludge).
+---------------

who > /tmp/who$$
if test -s /tmp/who$$; then
	echo multiuser
else
	echo singleuser
fi
rm /tmp/who$$

Who doesn't produce output in singleuser mode, since it's getty that writes
the login records and init spawns a shell directly in singleuser mode.  This
is true under all OSes I've checked (System III/V, Xenix 2/3/5, V7).  It is
most likely true of BSD (2.x and 4.x) as well.

The gotcha:  utmp formats are anything but compatible between V7/BSD and
System V/Xenix 5.  The easiest portable way is to have a shell script to do
the dump, and have it do the "who" stuff above.
-- 
	      Brandon S. Allbery, moderator of comp.sources.misc
	{well!hoptoad,uunet!marque,cbosgd,sun!mandrill}!ncoast!allbery
Delphi: ALLBERY						     MCI Mail: BALLBERY

haugj@pigs.UUCP (John F. Haugh II) (05/25/88)

In article <7798@ncoast.UUCP>, allbery@ncoast.UUCP (Brandon S. Allbery) writes:
] As quoted from <3595@psuvax1.psu.edu> by okunewck@gondor.cs.psu.edu (Phil OKunewick):
] +---------------
] | 	Is there any nice way for a program to tell whether a generic
] | unix system is in single-user or multi-user mode?  Two ideas here are
] | to read init's memory (not standard under different unixes) or to check
] | what processes are running (kludge).
] +---------------
] 
] who > /tmp/who$$
] if test -s /tmp/who$$; then
] 	echo multiuser
] else
] 	echo singleuser
] fi
] rm /tmp/who$$
] 
] Who doesn't produce output in singleuser mode, since it's getty that writes
] the login records and init spawns a shell directly in singleuser mode.  This
] is true under all OSes I've checked (System III/V, Xenix 2/3/5, V7).  It is
] most likely true of BSD (2.x and 4.x) as well.

who _does_ produce output in single user mode in one particular circumstance.
go rip the cord out of the wall, and when you get back in single user mode
do a who and see what you get.

under sys5 you will get a list of the people who were logged in when the
machine crashed.  i suspect that init doesn't zero out the utmp file in
xenix also.

the only fixes i can think of involve removing the utmp file or changing
its name every time you reboot.  how to insure the commands are executed
in a portable fashion is a whole different manner ...

- john.
-- 
 The Beach Bum                                 Big "D" Home for Wayward Hackers
 UUCP: ...!killer!rpp386!jfh                          jfh@rpp386.uucp :SMAILERS

 "You are in a twisty little maze of UUCP connections, all alike" -- fortune