⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 系统任务管理器
💻 C
📖 第 1 页 / 共 2 页
字号:
			{			arg[0] = '\0';			sscanf(buf, "%31s %127s", config, arg);			parse_config(config, arg);			}		}	fclose(f);	}const gchar *gkrellmd_config_getline(GkrellmdMonitor *mon)	{	GList			*list;	PluginConfigRec	*cfg;	if (!mon->privat)		{		mon->privat = g_new0(GkrellmdMonitorPrivate, 1);		mon->privat->config_list = gkrellmd_plugin_config_list;		}	for (list = mon->privat->config_list; list; list = list->next)		{		cfg = (PluginConfigRec *) list->data;		if (!strcmp(cfg->name, mon->name))			{			mon->privat->config_list = list->next;			return cfg->line;			}		}	return NULL;	}static voidread_config(void)	{	gchar	*path;	_GK.update_HZ = 3;	_GK.max_clients = 2;	_GK.server_port = GKRELLMD_SERVER_PORT;	_GK.fs_interval = 2;	_GK.nfs_interval = 16;	_GK.inet_interval = 1;#if defined(GKRELLMD_SYS_ETC)	path = g_strdup_printf("%s/%s", GKRELLMD_SYS_ETC, GKRELLMD_CONFIG);	load_config(path);	g_free(path);#endif#if defined(GKRELLMD_LOCAL_ETC)	path = g_strdup_printf("%s/%s", GKRELLMD_LOCAL_ETC, GKRELLMD_CONFIG);	load_config(path);	g_free(path);#endif	_GK.homedir = (gchar *) g_get_home_dir();	if (!_GK.homedir)		_GK.homedir = ".";#if !defined(WIN32)	path = g_strdup_printf("%s/.%s", _GK.homedir, GKRELLMD_CONFIG);#else	path = g_strdup_printf("%s/%s", _GK.homedir, GKRELLMD_CONFIG);#endif	load_config(path);	g_free(path);	}static voidusage(void)	{	printf(_("usage: gkrellmd [options]\n"));	printf(_("options:\n"));	printf(_("   -u, --update-hz F         Monitor update frequency\n"));	printf(_("   -m, --max-clients N       Number of simultaneous clients\n"));	printf(_("   -P, --port P              Server port to listen on\n"));	printf(_("   -a, --allow-host host     Allow connections from specified hosts\n"));	printf(_("   -c, --clear-hosts         Clears the current list of allowed hosts\n"));	printf(_("       --io-timeout N        Close connection after N seconds of no I/O\n"));	printf(_("       --reconnect-timeout N Try to connect every N seconds after\n"	         "                             a disconnect\n"));#if !defined(WIN32)	printf(_("       --mailbox path        Send local mailbox counts to gkrellm clients.\n"));	printf(_("   -d, --detach              Run in background and detach from terminal\n"));	printf(_("   -U, --user username       Change to this username after startup\n"));	printf(_("   -G, --group groupname     Change to this group after startup\n"));#endif	printf(_("   -p,  --plugin name        Enable a command line plugin\n"));	printf(_("   -pe, --plugin-enable name Enable an installed plugin\n"));	printf(_("       --plist               List plugins and exit\n"));	printf(_("       --plog                Print plugin install log\n"));	printf(_("   -V, --verbose\n"));	printf(_("   -v, --version\n"));	}static voidget_args(gint argc, gchar **argv)	{	gchar	*opt, *arg;	gint	i, r;	for (i = 1; i < argc; ++i)		{		opt = argv[i];		arg = argv[i + 1];		if (*opt == '-')			{			++opt;			if (*opt == '-')				++opt;			}		if (!strcmp(opt, "verbose") || !strcmp(opt, "V"))			{			_GK.verbose += 1;			continue;			}		if (!strcmp(opt, "plist"))			{			_GK.list_plugins = TRUE;			continue;			}		if (!strcmp(opt, "plog"))			{			_GK.log_plugins = TRUE;			continue;			}		else if (!strcmp(opt, "help") || !strcmp(opt, "h"))			{			usage();			exit(0);			}		else if (!strcmp(opt, "version") || !strcmp(opt, "v"))			{			printf("gkrellmd %d.%d.%d%s\n", GKRELLMD_VERSION_MAJOR,					GKRELLMD_VERSION_MINOR, GKRELLMD_VERSION_REV,					GKRELLMD_EXTRAVERSION);			exit(0);			}		else if (   i < argc				 && ((r = parse_config(opt, (i < argc - 1) ? arg : NULL)) >= 0)				)			{			i += r;			continue;			}		printf(_("Bad arg: %s\n"), argv[i]);		usage();		exit(0);		}	}static int *socksetup(int af)	{	struct addrinfo	hints, *res, *r;	gint			maxs, *s, *socks;#ifndef HAVE_GETADDRINFO	struct sockaddr_in	sin;#else	gchar			portnumber[6];	gint			error;#endif	memset(&hints, 0, sizeof(hints));	hints.ai_socktype = SOCK_STREAM;#ifdef HAVE_GETADDRINFO	hints.ai_flags = AI_PASSIVE;	hints.ai_family = af;	snprintf(portnumber, sizeof(portnumber), "%d", _GK.server_port);	error = getaddrinfo(NULL, portnumber, &hints, &res);	if (error)		{		fprintf(stderr, "gkrellmd %s\n", gai_strerror(error));		return NULL;		}#else	/* Set up the address structure for the listen socket and bind the	|  listen address to the socket.	*/	hints.ai_family = PF_INET;	hints.ai_addrlen = sizeof(struct sockaddr_in);	hints.ai_next = NULL;	hints.ai_addr = (struct sockaddr *) &sin;	sin.sin_family = PF_INET;	sin.sin_addr.s_addr = INADDR_ANY;	sin.sin_port = htons(_GK.server_port);	res = &hints;#endif	/* count max number of sockets we may open */	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)		;	socks = malloc((maxs + 1) * sizeof(int));	if (!socks)		{		fprintf(stderr,			"gkrellmd couldn't allocate memory for sockets\n");		return NULL;		}	*socks = 0;   /* num of sockets counter at start of array */	s = socks + 1;	for (r = res; r; r = r->ai_next)		{		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);		if (*s < 0)			continue;		/* SO_REUSEADDR flag allows the server to restart immediately		*/		if (1)			{			const int on = 1;			if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)				{				fprintf(stderr, "gkrellmd setsockopt (SO_REUSEADDR) failed\n");				close(*s);				continue;				}			}#ifdef IPV6_V6ONLY		if (r->ai_family == AF_INET6)			{			const int on = 1;			if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)				{				fprintf(stderr, "gkrellmd setsockopt (IPV6_V6ONLY) failed\n");				close(*s);				continue;				}			}#endif		if (bind(*s, r->ai_addr, r->ai_addrlen) < 0)			{			close(*s);			continue;			}		(*socks)++;		s++;	}#ifdef HAVE_GETADDRINFO	if (res)		freeaddrinfo(res);#endif	if (*socks == 0)		{		fprintf(stderr, "gkrellmd couldn't bind to any socket\n");		free(socks);		return NULL;		}	return socks;	}/* XXX: Recent glibc seems to have daemon(), too. */#if defined(BSD4_4)#define HAVE_DAEMON#endif#if !defined(HAVE_DAEMON) && !defined(WIN32) && !defined(__solaris__)#include <paths.h>#endif#if !defined(_PATH_DEVNULL)#define _PATH_DEVNULL	"/dev/null"#endifstatic voiddetach_from_terminal(void)	{#if !defined(WIN32)#if !defined(HAVE_DAEMON)	gint	i, fd;#endif	if (getppid() == 1)	 /* already a daemon */		return;#if defined(HAVE_DAEMON)	if (daemon(0, 0))		{		fprintf(stderr, "gkrellmd detach failed: %s\n", strerror(errno));		remove_pidfile();		exit(1);		}#else	i = fork();	if (i > 0)		exit(0);	if (i < 0 || setsid() == -1)		/* new session process group */		{		fprintf(stderr, "gkrellmd detach failed: %s\n", strerror(errno));		remove_pidfile();		exit(1);		}	if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1)		{		dup2(fd, STDIN_FILENO);		dup2(fd, STDOUT_FILENO);		dup2(fd, STDERR_FILENO);		if (fd > 2)			close(fd);		}	chdir("/");#endif//	signal(SIGCHLD, SIG_IGN);	signal(SIGTSTP, SIG_IGN);	signal(SIGTTOU, SIG_IGN);	signal(SIGTTIN, SIG_IGN);	signal(SIGHUP, SIG_IGN);#if !defined(MSG_NOSIGNAL)	signal(SIGPIPE, SIG_IGN);#endif#endif	}static voiddrop_privileges(void)	{#if !defined(WIN32)	if (drop_privs.gid > (uid_t)0)		{		(void) setgroups((size_t)0, (gid_t*)0);		(void) setgid(drop_privs.gid);		}	if (drop_privs.uid > (uid_t)0)		(void) setuid(drop_privs.uid);#endif	}#if defined(WIN32) && defined(_WINDOWS)int APIENTRY WinMain(HINSTANCE	hInstance,					HINSTANCE	hPrevInstance,					LPSTR		lpCmdLine,					int			nCmdShow)	{	int		argc;	gchar	**argv;	gchar	*cmd;#elsegintmain(gint argc, gchar **argv)	{#endif#ifdef HAVE_GETADDRINFO	struct sockaddr_storage client_addr;#else	struct sockaddr_in	client_addr;#endif	fd_set				read_fds, test_fds;	struct timeval		tv;	GkrellmdClient		*client;	size_t				addr_len;	gint				fd, server_fd, client_fd, nbytes, i;	gint				max_fd = -1;	gint				listen_fds = 0;	gint				interval, result;#if defined(WIN32) && defined(_WINDOWS)    setupServerWindow(hInstance);    // need to get exe too	cmd = GetCommandLine();	g_shell_parse_argv(cmd, &argc, &argv, NULL);#endif#ifdef ENABLE_NLS#ifdef LOCALEDIR	bindtextdomain(PACKAGE_D, LOCALEDIR);#endif	textdomain(PACKAGE_D);	bind_textdomain_codeset(PACKAGE_D, "UTF-8");#endif	/* ENABLE_NLS */	read_config();	get_args(argc, argv);	if (_GK.verbose)		printf("update_HZ=%d\n", _GK.update_HZ);	if (   detach_flag	    && !_GK.log_plugins && !_GK.list_plugins && _GK.debug_level == 0	   )		detach_from_terminal();	else		{		signal(SIGTERM, cb_sigterm);		signal(SIGQUIT, cb_sigterm);		signal(SIGINT, cb_sigterm);		signal(SIGTSTP, SIG_IGN);		}	make_pidfile();	gkrellm_sys_main_init();	drop_privileges();#if GLIB_CHECK_VERSION(2,0,0)	g_thread_init(NULL);#endif	_GK.start_time = time(0);	if (_GK.update_HZ < 1 || _GK.update_HZ > 10)		_GK.update_HZ = 3;	if (_GK.fs_interval < 1 || _GK.fs_interval > 1000)		_GK.fs_interval = 2;	if (_GK.nfs_interval > 10000)		_GK.nfs_interval = 16;	if (_GK.inet_interval > 20)		_GK.inet_interval = 20;	gkrellmd_load_monitors();	_GK.server_fd = socksetup(PF_UNSPEC);	if (_GK.server_fd == NULL)		{		fprintf(stderr, "gkrellmd socket() failed: %s\n", strerror(errno));		remove_pidfile();		exit(1);		}	/* Listen on the socket so a client gkrellm can connect.	*/	FD_ZERO(&read_fds);	for (i = 1; i <= _GK.server_fd[0]; ++i)		{		if (listen(_GK.server_fd[i], 5) == -1)			{			close(_GK.server_fd[i]);			continue;			}		++listen_fds;		FD_SET(_GK.server_fd[i], &read_fds);		if (max_fd < _GK.server_fd[i])			max_fd = _GK.server_fd[i];		}	if (listen_fds <= 0)		{		fprintf(stderr, "gkrellmd listen() failed: %s\n", strerror(errno));		remove_pidfile();		exit(1);		}	interval = 1000000 / _GK.update_HZ;#if defined(WIN32) && defined(_WINDOWS)    while (notDone == 0)        {            {                MSG msg;                while (PeekMessage(&msg, (HWND) NULL, 0, 0, PM_REMOVE)) {                    TranslateMessage(&msg);                     DispatchMessage(&msg);                 }            }#else    while(1)		{#endif		test_fds = read_fds;#ifdef HAVE_GETADDRINFO		addr_len = sizeof(struct sockaddr_storage);#else		addr_len = sizeof(struct sockaddr_in);#endif		tv.tv_usec = interval;		tv.tv_sec = 0;		if ((result = select(max_fd + 1, &test_fds, NULL, NULL, &tv) == -1))			{			if (errno == EINTR)				continue;			fprintf(stderr, "gkrellmd select() failed: %s\n", strerror(errno));			remove_pidfile();			exit(1);			}#if 0	/* BUG, result is 0 when test_fds has a set fd!! */		if (result == 0)				{			gkrellmd_update_monitors();			continue;			}#endif		for (fd = 0; fd <= max_fd; ++fd)			{			if (!FD_ISSET(fd, &test_fds))				continue;			server_fd = -1;			for (i = 1; i <= _GK.server_fd[0]; ++i)				if (fd == _GK.server_fd[i])					{					server_fd = fd;					break;					}			if (server_fd >= 0)				{				client_fd = accept(server_fd,						(struct sockaddr *) &client_addr,						(socklen_t *) &addr_len);				if (client_fd == -1)					{					fprintf(stderr, "gkrellmd accept() failed: %s\n",							strerror(errno));					remove_pidfile();					exit(1);					}				if (client_fd > max_fd)					max_fd = client_fd;				client = accept_client(client_fd,							(struct sockaddr *)&client_addr, addr_len);				if (!client)					{					close(client_fd);					continue;					}				FD_SET(client_fd, &read_fds);				gkrellmd_serve_setup(client);				if (_GK.verbose)					printf("gkrellmd accepted client: %s:%u\n",						client->hostname,						ntohs(((struct sockaddr_in *)&client_addr)->sin_port));				}			else				{				ioctl(fd, FIONREAD, &nbytes);				if (nbytes == 0)					{					remove_client(fd);					FD_CLR(fd, &read_fds);					}				else					gkrellmd_client_read(fd, nbytes);				}			}		gkrellmd_update_monitors();		}	return 0;	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -