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

📄 util.c

📁 VoiFax is a program that manage voice/data/fax modem in the same manner of vgetty and mgetty.VoiFax
💻 C
字号:
/*  voifax general purpose functions library *  Copyright (C) 2004  Simone Freddio & Andrea Emanuelli * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include "util.h"#include "common.h"#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <sys/signal.h>#include <sys/shm.h>#include <errno.h>static t_watchdog_list *wd;static int wd_shm_id;static int wd_search_free_slot(void);int memory_search(char *array, int array_size, char *target, int target_size){	int a;	for (a = 0; a < array_size - target_size; a++)	{		if (memcmp(&array[a], target, target_size) == 0)		{			return a;		}	}	return -1;}int remove_special_chars(char *string){	int len = strlen(string);	char *work_string = malloc(len + 1);	int a, idx;		strcpy(work_string, string);	idx = 0;	for (a = 0; a < len; a++)	{		switch (work_string[a])		{			case '\n':			case '\r':				break;							default:				string[idx] = work_string[a];				idx++;				break;		}	}	string[idx] = 0;	free(work_string);		return true;}static int wd_search_free_slot(void){	int a;	for (a = 0; a < WATCHDOG_NUMBER; a++)	{		if (wd->list[a].valid == false)			return a;	}	return -1;}int wd_init(void){	char module[] = "Watchdog handler";	char trc[200];	int a, end = false;	int ppid;	t_watchdog_list *wdl;		/* Create a shared memory segment for the watchdog list*/	wd_shm_id = shmget(IPC_PRIVATE, sizeof(t_watchdog_list), IPC_CREAT | 511);	if (wd_shm_id == -1)	{		sprintf(trc, "Shared memory id: %d, errno %d", wd_shm_id, errno);		TRC(TRC_INFO, module, trc);		return false;	}	/* Map shared memory segment on parent heap */	wd = shmat(wd_shm_id, NULL, 0);	if ((int)wd == -1)	{		sprintf(trc, "Shared memory ptr: %p, errno %d", (void *)wd, errno);		TRC(TRC_INFO, module, trc);		return false;	}	ppid = getpid();	if (fork() == 0)	{		/* Map shared memory segment on child heap also */		wdl = shmat(wd_shm_id, NULL, 0);		if ((int)wdl == -1)		{			sprintf(trc, "Child shared memory ptr: %p, errno %d", (void *)wd, errno);			TRC(TRC_INFO, module, trc);			return false;		}		while (end == false)		{			sleep(1);			for (a = 0; a < WATCHDOG_NUMBER; a++)			{				if (wdl->list[a].valid)				{					sprintf(trc, "Watchdoc '%s' value %d", 									wdl->list[a].name, wdl->list[a].counter);					TRC(TRC_INFO, module, trc);					if (wdl->list[a].counter <= 0)					{						sprintf(trc, "Watchdoc '%s' reached zero!", wdl->list[a].name);						TRC(TRC_ERR, module, trc);						kill(ppid, SIGTERM);						end = true;					}					wdl->list[a].counter--;				}			}		}		/* Destroy shared memory segment */		shmctl(wd_shm_id, IPC_RMID, NULL);		exit(0);	}	return true;}int wd_create(int initial_value, char *name){	char module[] = "wd_create";	int idx = wd_search_free_slot();	char trc[200];	if (idx == -1)	{		TRC(TRC_ERR, module, "No enough room for watchdogs");		return -1;	}	strcpy(wd->list[idx].name, name);	wd->list[idx].counter = initial_value;	wd->list[idx].valid = true;	sprintf(trc, "Registered watchdog '%s', (%d), handler %d", 					name, initial_value, idx);	TRC(TRC_INFO, module, trc);	return idx;}int wd_refresh(int wd_handler){	char module[] = "wd_refresh";	if (!wd->list[wd_handler].valid)	{		TRC(TRC_ERR, module, "Invalid handler");		return false;	}		if (wd->list[wd_handler].counter < WATCHDOG_MAXVALUE)		wd->list[wd_handler].counter++;	return true;}int wd_destroy(int wd_handler){	char module[] = "wd_destroy";	if (!wd->list[wd_handler].valid)	{		TRC(TRC_ERR, module, "Invalid handler");		return false;	}		wd->list[wd_handler].valid = false;	strcpy(wd->list[wd_handler].name, "");	return true;}void wd_close(void){	int a;	for (a = 0; a < WATCHDOG_NUMBER; a++)		if (wd->list[a].valid)			wd_destroy(a);		shmctl(wd_shm_id, IPC_RMID, NULL);}

⌨️ 快捷键说明

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