From owner-freebsd-questions@freebsd.org Wed Jun 13 13:47:01 2018 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id CC34D100CC20 for ; Wed, 13 Jun 2018 13:47:00 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.126.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "mout.kundenserver.de", Issuer "TeleSec ServerPass DE-2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 419EA83FD2 for ; Wed, 13 Jun 2018 13:46:59 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r56.edvax.de ([92.195.97.38]) by mrelayeu.kundenserver.de (mreue004 [212.227.15.167]) with ESMTPA (Nemesis) id 0LgtFa-1fzfmm3Vx8-00oGNy; Wed, 13 Jun 2018 15:46:50 +0200 Date: Wed, 13 Jun 2018 15:46:49 +0200 From: Polytropon To: Manish Jain Cc: Shane Ambler , freebsd-questions@freebsd.org Subject: Re: How to detect single user mode in FreeBSD ? Message-Id: <20180613154649.6e49eeda.freebsd@edvax.de> In-Reply-To: References: Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:7dh9NRWW56nHglz/LVtBZ6Ewvyd+jwxkhxHcjaxp0I2IHJU7Xwj l0d6jd0rVc91DIPzs2cl0d4EzaMDH4Xto4As0SJ6x4HGqSB8aS15NGr2B+FksvquYAU384z /UNTjSiuzv3RLjEYvqoOxgSAaPTS6FfoxaJwbzKsxjissJs4TI5UI9pJ1OB0Dum55g2cN+v 7sUnsftZsu0Rf2o8+ehkw== X-UI-Out-Filterresults: notjunk:1;V01:K0:XyRQUGnj6vg=:y/0QhHOn5KVGuQpj1xRwzX rh+eYb3Bo95AxKgCuqCPqdTYvH+pSRU5s+IBikjqoxsju+YtZnh4wYALqN4Xr8tVq8HLthATj kL4xFEnWKAnI7zDtTyIlyZWTwO0LiJeqb2VApJ1yt5zedk/umgNjaNdcZnmayk5pZ6WXRVaOl Ui4KtHKYF4pw2gdm1A+T8P3zpeGv3hwNnUTFCa4bOggco8OlJ6o871HNdqDA1oBXdh+lSZIzI fo4WKhJvFb7FyOB1nBojtppIs6kzMFQS1Rle1LjmYJd8t4FhBYLsOx1b7rBUWgfgSuSdl20rP DxzOdGOYIIby7vFDs7ua8GGDe9GK2bZE3qe+G9ClYVPSXzyI5x19Q2QEMG87DpCedTAmrX24N dD+xXSmUah1yT/VXOTvU9N6b6TGdMtHf12gBMoHgBQbhF4styY2YkA9Ho0cWsxO43+p272hlo vtgh1tfGzZ08L230VSKJSn4YVJK/ZaPVgXlEzPxixzjHw3SYucFbJxoQvAo6kz6ElQyRX3S4C W6fLio6QYRvzPjNQlbIuc+Hh3KLrXeiljGJ9+VNv8tterrf8EJOYn4UMvy3Dc2KaBxZeUYX2X FRNPZy8FOsslVVOW0d5roVyRT/PM55yvz5eUGGil7Q+Fb1Ai37LIHs43XbtiiXMGcReoa+GBE fe3HnuamdkpaqBt00pAgzTfgPg0WLD7h9JHc5lJabJJrnVd3d5bAFHBmOST8SLWzmpcWEEA4D yDJtrcSst/ZSupKY X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jun 2018 13:47:01 -0000 On Wed, 13 Jun 2018 18:04:08 +0530, Manish Jain wrote: > On 06/13/18 15:55, Shane Ambler wrote: > > When in single user mode PID 1 should be "/sbin/init -s" which becomes > > "/sbin/init --" in multi user mode. > > > > The next closet to knowing would be looking at mount, in single user > > mode you will only have / mounted read only and /dev. It is rare for any > > system to be past single user mode with only that mount setup but the > > user can mount the filesystems before starting your script and still be > > in single user mode. > > Hi Shane, > > Tx for replying. But don't you think there should ideally be a sysctl to > be detect the runlevel, particularly single user mode ? Currently, there is no such sysctl availablr (or another utility that prints the current runlevel - see runlevel- related comment later on). > It makes things > easily documentible, just as when we can use sysctl to find out if the > OS is virtualized (I think kern.vm_guest). You can probably work with the information from "man 8 init", such as If the system security level (see security(7)) is initially nonzero, then init leaves it unchanged. Otherwise, init raises the level to 1 before going multi-user for the first time. Since the level cannot be reduced, it will be at least 1 for subsequent operation, even on return to single- user. If a level higher than 1 is desired while running multi-user, it can be set before going multi-user, e.g., by the startup script rc(8), using sysctl(8) to set the kern.securelevel variable to the required security level. [...] If run as a user process as shown in the second synopsis line, init will emulate AT&T System V UNIX behavior, i.e., super-user can specify the desired run-level on a command line, and init will signal the original (PID 1) init as follows: Run-level Signal Action 0 SIGUSR2 Halt and turn the power off 1 SIGTERM Go to single-user mode 6 SIGINT Reboot the machine c SIGTSTP Block further logins q SIGHUP Rescan the ttys(5) file See "man 8 init" for further details. For details about the run-levels (which aren't that common to the FreeBSD boot & startup mechanism, which is BSD-style init, whereas runlevels appear in System V sysvinit-like mechanisms), check: https://docs.freebsd.org/doc/7.4-RELEASE/usr/share/doc/en_US.ISO8859-1/articles/linux-users/startup.html https://www.freebsd.org/doc/en_US.ISO8859-1/articles/linux-users/startup.html You could summarize it as "FreeBSD doesn't have runlevels". :-) However, you can still check for a process that is typucally run in multi-user mode. If it's _not_ running - well, you are in single-user mode. Such a test could look like this: if ! ps -auxww | grep -v "grep" | grep "adjkerntz" > /dev/null; then echo "Single user"; else echo "Multi-user"; fi In this example, a running instance of adjkerntz is being process-grepped for. Suggestion found here: https://superuser.com/questions/457111/how-to-get-run-level-in-freebsd -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...