/* * Copyright (C) 2006 Tony Sin(x) '76 * All rights reserved. * */ /* * GNU GENERAL PUBLIC LICENSE * Version 2, June 1991 * * Copyright (C) 1989, 1991 Free Software Foundation, Inc. * 675 Mass Ave, Cambridge, MA 02139, USA * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. * * 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 * */ #ifdef HAVE_CONFIG_H #ifndef __main_config_h__ #define __main_config_h__ #include "../config.h" #endif #include #include #include #include #include #include #include #include #include #include #include #endif #include "cmdfifo.h" #include "exception.h" cCmdFifo::cCmdFifo() { pthread_attr_t thattr; pthread_t thid; int err; try { no_exception = false; if ((unlink(CLAMCOUR_CMD_FIFO) == -1) && (errno != ENOENT)) throw cException(CMDFIFO_CLASS_NAME,CMDFIFO_CLASS_NAME,"unlink",errno); if (mkfifo(CLAMCOUR_CMD_FIFO,0666) == -1) throw cException(CMDFIFO_CLASS_NAME,CMDFIFO_CLASS_NAME,"mkfifo",errno); if ((err = pthread_attr_init(&thattr))) throw cException(CMDFIFO_CLASS_NAME,CMDFIFO_CLASS_NAME,"pthread_attr_init",err); if ((err = pthread_attr_setdetachstate(&thattr,PTHREAD_CREATE_DETACHED))) throw cException(CMDFIFO_CLASS_NAME,CMDFIFO_CLASS_NAME,"pthread_attr_setdetachstate",err); if ((err = pthread_create(&thid,&thattr,taskProc,(void *) this))) throw cException(CMDFIFO_CLASS_NAME,CMDFIFO_CLASS_NAME,"pthread_create",err); syslog(LOG_INFO,"command FIFO ready"); } catch (cException c_e) { if (strlen(c_e.str) > 0) syslog(LOG_ERR,"%s",c_e.str); } } cCmdFifo::~cCmdFifo() { try { if (unlink(CLAMCOUR_CMD_FIFO) == -1) throw cException(CMDFIFO_CLASS_NAME,CMDFIFO_CLASS_NAME_D,"unlink",errno); syslog(LOG_INFO,"command FIFO closed"); } catch (cException c_e) { if (strlen(c_e.str) > 0) syslog(LOG_ERR,"%s",c_e.str); } } void *cCmdFifo::taskProc(void *arg) { int fifoId; char cmdBuf[CMD_BUF_LENGTH]; cCmdFifo *ptr = (cCmdFifo *) arg; bool time_to_quit = false; int cmdBufLen; try { if ((fifoId = open(CLAMCOUR_CMD_FIFO,O_RDWR)) == -1) throw cException(CMDFIFO_CLASS_NAME,"taskProc","open",errno); do { if ((cmdBufLen = read(fifoId,cmdBuf,CMD_BUF_LENGTH)) == -1) { time_to_quit = true; cmdBuf[0] = '\0'; if (!ptr->no_exception) throw cException(CMDFIFO_CLASS_NAME,"taskProc","read",errno); } if (!time_to_quit) { cmdBuf[cmdBufLen] = '\0'; if (!strncmp(cmdBuf,"reloadDB",strlen("reloadDB"))) { if (kill(getpid(),SIGUSR1) == -1) throw cException(CMDFIFO_CLASS_NAME,"taskProc","kill",errno); } else if (!strncmp(cmdBuf,"quitFIFO",strlen("quitFIFO"))) time_to_quit = true; else syslog(LOG_ERR,"cCmdFifoTask: unknown command"); } } while (!time_to_quit); if (close(fifoId) == -1) throw cException(CMDFIFO_CLASS_NAME,"taskProc","close",errno); } catch (cException c_e) { syslog(LOG_ERR,"%s",c_e.str); } delete ptr; return NULL; }