/************************************\
 *                                  *
 *   scritto da zero per CloseBSD   *
 *                                  *
 *   ultima modifica: 25/10/2026    *
 *                                  *
\************************************/

// risolvi "device busy" per remount rdonly '/' 
// >> rm fstab.h + mount + mount_ffs
// clean /tmp
// sistema poweroff flags
// wait

#include <stdio.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <sys/swap.h>
#include <sys/types.h>
#include <unistd.h>

#define ROOT_DEV	"/dev/sd0a"
#define	SWAP		"/dev/sd0b"
#define USR_LOCAL	"/usr/local"
#define ROOT_DIR	"/"
#define DEFAULT_ROOTUID	-2

int main(int argc, char *argv[])
{
	struct ufs_args dev;
	int flags, come;

	dev.fspec = ROOT_DEV;
	flags = MNT_RDONLY & MNT_UPDATE;
	dev.export_info.ex_root = DEFAULT_ROOTUID;
	dev.export_info.ex_flags = MNT_EXRDONLY;

	/*
	 *   disattiva swap
	 */
	if (swapctl(SWAP_OFF, SWAP, 0) > 0) {
		perror(SWAP);
		return 1;
	}
	printf("\tswap disattivata: %s\n", SWAP);

	/*
	 *   smonta /usr/local
	 */
	if (unmount(USR_LOCAL, 0) == -1) {
		perror(USR_LOCAL);
		return 1;
	}
	printf("\tsmontato: %s\n", USR_LOCAL);

	/*
	 *   rimonta root read-only
	 */
/* 	"device busy"
	if (mount(MOUNT_FFS, ROOT_DIR, flags, &dev) == -1) {
		perror(argv[1]);
		return 1;
	}
	printf("\tmontato read-only: %s su %s\n", ROOT_DEV, ROOT_DIR);
*/

	come = RB_AUTOBOOT;
	if (strcmp("riavvia", argv[0]) == 0) {
		if (reboot(come) == -1) {
			perror(argv[0]);
			return 1;
		}
	}

	if (strcmp("spegni", argv[0]) == 0) {
		// come &= RB_HALT & RB_POWERDOWN; non va!!
		if (reboot(0x01008) == -1) {
			perror(argv[0]);
			return 1;
		}
	}

	return 0;
}
