From 2eb9db473adf885dc0361b1967edd1781ff13b1e Mon Sep 17 00:00:00 2001
From: wanghuizhao <wanghuizhao1@huawei.com>
Date: Sun, 24 Dec 2023 23:23:55 +0800
Subject: [PATCH] restorecond: remove dependency of glib2

In order to remove the dependency of glib2, and in some scenarios, it is
not necessary to use the user mode, remove the user related option.

Signed-off-by: wanghuizhao <wanghuizhao1@huawei.com>
---
 .../restorecond/Makefile                      | 21 +++++++++++++++++++
 .../restorecond/restorecond.c                 | 14 +++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/restorecond/Makefile b/restorecond/Makefile
index 8e9a5ef..bebd39e 100644
--- a/restorecond/Makefile
+++ b/restorecond/Makefile
@@ -13,21 +13,37 @@ SYSTEMDUSERUNITDIR ?= $(shell $(PKG_CONFIG) --variable=systemduserunitdir system
 autostart_DATA = sealertauto.desktop
 INITDIR ?= /etc/rc.d/init.d
 SELINUXDIR = /etc/selinux
+CONFIG_NO_GLIB2 ?= n
 
+ifeq ($(CONFIG_NO_GLIB2),n)
 GIO_CFLAGS = -DHAVE_DBUS $(shell $(PKG_CONFIG) --cflags gio-2.0)
 GIO_LIBS = $(shell $(PKG_CONFIG) --libs gio-2.0)
+endif
 
 CFLAGS ?= -g -Werror -Wall -W
+
+ifeq ($(CONFIG_NO_GLIB2),y)
+	override CFLAGS += -DCONFIG_NO_GLIB2
+	override LDLIBS += -lselinux
+else
 override CFLAGS += $(GIO_CFLAGS)
 
 override LDLIBS += -lselinux $(GIO_LIBS)
+endif
 
 all: restorecond
 
+ifeq ($(CONFIG_NO_GLIB2),y)
+restorecond.o utmpwatcher.o stringslist.o watch.o: restorecond.h
+
+restorecond:  restore.o restorecond.o utmpwatcher.o stringslist.o watch.o
+	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+else
 restorecond.o utmpwatcher.o stringslist.o user.o watch.o: restorecond.h
 
 restorecond:  restore.o restorecond.o utmpwatcher.o stringslist.o user.o watch.o
 	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+endif
 
 install: all
 	[ -d $(DESTDIR)$(MANDIR)/man8 ] || mkdir -p $(DESTDIR)$(MANDIR)/man8
@@ -44,15 +60,20 @@ install: all
 	install -m 755 restorecond.init $(DESTDIR)$(INITDIR)/restorecond
 	-mkdir -p $(DESTDIR)$(SELINUXDIR)
 	install -m 644 restorecond.conf $(DESTDIR)$(SELINUXDIR)/restorecond.conf
+ifeq ($(CONFIG_NO_GLIB2),n)
 	install -m 644 restorecond_user.conf $(DESTDIR)$(SELINUXDIR)/restorecond_user.conf
 	-mkdir -p $(DESTDIR)$(AUTOSTARTDIR)
 	install -m 644 restorecond.desktop $(DESTDIR)$(AUTOSTARTDIR)/restorecond.desktop
 	-mkdir -p $(DESTDIR)$(DBUSSERVICEDIR)
 	install -m 644 org.selinux.Restorecond.service  $(DESTDIR)$(DBUSSERVICEDIR)/org.selinux.Restorecond.service
+endif
 	-mkdir -p $(DESTDIR)$(SYSTEMDSYSTEMUNITDIR)
 	install -m 644 restorecond.service $(DESTDIR)$(SYSTEMDSYSTEMUNITDIR)
+ifeq ($(CONFIG_NO_GLIB2),n)
 	-mkdir -p $(DESTDIR)$(SYSTEMDUSERUNITDIR)
 	install -m 644 restorecond_user.service $(DESTDIR)$(SYSTEMDUSERUNITDIR)
+endif
+
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/restorecond 
 
diff --git a/restorecond/restorecond.c b/restorecond/restorecond.c
index d5f70fc..fc0594b 100644
--- a/restorecond/restorecond.c
+++ b/restorecond/restorecond.c
@@ -124,7 +124,11 @@ static void term_handler(int s __attribute__ ((unused)))
 
 static void usage(char *program)
 {
+#ifndef CONFIG_NO_GLIB2
 	printf("%s [-d] [-f restorecond_file ] [-u] [-v] \n", program);
+#else
+    printf("%s [-d] [-f restorecond_file ] [-v] \n", program);
+#endif
 }
 
 void exitApp(const char *msg)
@@ -165,7 +169,11 @@ int main(int argc, char **argv)
 	sigaction(SIGTERM, &sa, NULL);
 
 	atexit( done );
+#ifndef CONFIG_NO_GLIB2
 	while ((opt = getopt(argc, argv, "hdf:uv")) > 0) {
+#else
+	while ((opt = getopt(argc, argv, "hdf:v")) > 0) {
+#endif
 		switch (opt) {
 		case 'd':
 			debug_mode = 1;
@@ -173,9 +181,11 @@ int main(int argc, char **argv)
 		case 'f':
 			watch_file = optarg;
 			break;
+#ifndef CONFIG_NO_GLIB2
 		case 'u':
 			run_as_user = 1;
 			break;
+#endif
 		case 'h':
 			usage(argv[0]);
 			exit(0);
@@ -200,11 +210,15 @@ int main(int argc, char **argv)
 
 	homedir = pwd->pw_dir;
 	if (uid != 0) {
+#ifndef CONFIG_NO_GLIB2
 		if (run_as_user)
 			return server(master_fd, user_watch_file);
 		if (start() != 0)
 			return server(master_fd, user_watch_file);
 		return 0;
+#else
+		exitApp("root_only");
+#endif
 	}
 
 	read_config(master_fd, watch_file);
-- 
2.21.0

