1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
/* CWirc - X-Chat plugin for sending and receiving raw morse code over IRC
(c) Pierre-Philippe Coupard - 18/06/2003
Frontend application.
This program is distributed under the terms of the GNU General Public License
See the COPYING file for details
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "types.h"
#include "cwirc.h"
#include "common.h"
#include "rcfile.h"
#include "io.h"
#include "gui.h"
#include "extension.h"
#include "ipc.h"
/* Global variables */
struct cwirc_shm_block *sharedmem;
int shmid;
int ext_shmid; /* Extension API's shared memory id */
struct cwirc_extension_api *ext_sharedmem;
int io_process_pid=-1;
/* Prototypes */
static void sigchld_hdlr(int nothing);
static void clean_exit_hdlr(int nothing);
/* Main Function */
int main(int argc,char *argv[])
{
char *errmsg;
int caught_sigs_not_sigchld[]={SIGHUP,SIGINT,SIGKILL,SIGPIPE,SIGIO, \
SIGPROF,SIGTERM,SIGUSR1,SIGUSR2,SIGVTALRM,SIGURG,-1};
struct sigaction sigact;
int normal_priority;
int i;
/* Find out our current priority */
errno=0;
normal_priority=getpriority(PRIO_PROCESS,0);
if(errno) /* Error finding out the priority ? */
normal_priority=-99; /* Forget about the whole priority thing */
/* Try to renice ourselves, then immediately drop root privileges. If this
fails, it's not an error as the user would only install the frontend
binary suid root to allow it to renice itself, to cure "scratchy" sound
with low-end soundcards. If the user is happy with I/O performances
without renicing, so much the better. */
if(normal_priority!=-99)
setpriority(PRIO_PROCESS,0,-20);
setreuid(getuid(),getuid());
setregid(getgid(),getgid());
/* We need one argument only, and it needs to be the shared memory id */
if(argc!=2 || (shmid=strtol(argv[1],NULL,0))==0)
{
printf("Error: %s is a frontend for the CWirc X-Chat plugin.\n",argv[0]);
printf(" It cannot be used as a standalone application.\n");
usleep(250000);
return(-1);
}
/* Attach to the shared memory block */
if((sharedmem=(struct cwirc_shm_block *)cwirc_shm_attach(shmid))
==(struct cwirc_shm_block *)-1)
{
printf("CWirc (frontend) : error : can't attach to the shared memory.\n");
usleep(250000);
return(-1);
}
/* Check the plugin's version number against ours */
if(strncmp(sharedmem->version,VERSION,strlen(VERSION)))
{
printf("CWirc (frontend) : error : plugin/frontend version mismatch.\n");
usleep(250000);
return(-1);
}
sharedmem->mouseinputbutton0=0;
sharedmem->mouseinputbutton1=0;
sharedmem->cwcodeset=0;
sharedmem->decoded_msg_buf[0]=0;
sharedmem->decoded_msg_buf_char_markers[0]=0;
sharedmem->decoded_msg_wpm=-1;
sharedmem->decoded_msg_updated=0;
sharedmem->reset_decoder=0;
sharedmem->sidetone_mode=0;
/* Read/check the config file */
if((errmsg=cwirc_parse_rcfile(RCFILE))!=NULL)
{
printf("CWirc : error : %s",errmsg);
cwirc_shm_detach(sharedmem);
usleep(250000);
return(-1);
}
/* Seed the RNG with something vaguely random */
srand(time(NULL));
/* Create a shared memory block for the extension API */
while((i=rand())==shmid);
if((ext_shmid=cwirc_shm_alloc(i,sizeof(struct cwirc_extension_api)))==-1)
{
printf("CWirc : error : can't create shared memory for the extension API");
cwirc_shm_detach(sharedmem);
usleep(250000);
return(-1);
}
/* Attach to the extension API's shared memory block */
if((ext_sharedmem=(struct cwirc_extension_api *)cwirc_shm_attach(ext_shmid))
==(struct cwirc_extension_api *)-1)
{
printf("CWirc (frontend) : error : can't attach to the extension API's "
"shared memory\n");
cwirc_shm_free(ext_shmid);
cwirc_shm_detach(sharedmem);
usleep(250000);
return(-1);
}
/* Create 2 semaphores to sync an extension program and allow it to safely
get audio out of the extension API's audio buffer */
while((i=rand())==sharedmem->semid);
if((ext_sharedmem->semid=cwirc_sem_create(i,2))==-1)
{
printf("CWirc (frontend) : error : can't create sync semaphores for the "
"extension API\n");
cwirc_shm_detach(ext_sharedmem);
cwirc_shm_free(ext_shmid);
cwirc_shm_detach(sharedmem);
usleep(250000);
return(-1);
}
/* Acquire the 2 semaphores before any extension program has a chance to
start */
for(i=0;i<2;i++)
if(cwirc_sem_P(ext_sharedmem->semid,i)!=0)
{
printf("CWirc (frontend) : error : can't acquire sync semaphore #%d for "
"the extension API\n",i);
cwirc_shm_detach(ext_sharedmem);
cwirc_shm_free(ext_shmid);
cwirc_shm_detach(sharedmem);
usleep(250000);
return(-1);
}
/* Initialize the extension API */
ext_sharedmem->out_audiobuf_start=0;
ext_sharedmem->out_audiobuf_end=0;
ext_sharedmem->in_key=0;
ext_sharedmem->pid=-1;
/* Make sure we catch all signals other than SIGCHLD to exit cleanly */
for(i=0;caught_sigs_not_sigchld[i]!=-1;i++)
signal(caught_sigs_not_sigchld[i],&clean_exit_hdlr);
/* Make sure we catch the I/O process' death (but not its stopping) */
sigact.sa_handler=sigchld_hdlr;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags=SA_NOCLDSTOP;
sigaction(SIGCHLD,&sigact,NULL);
/* Spawn the I/O process */
if((io_process_pid=cwirc_spawn_io_process(shmid,ext_shmid))==-1)
{
printf("CWirc : error : can't spawn I/O process.\n");
cwirc_sem_destroy(ext_sharedmem->semid);
cwirc_shm_detach(ext_sharedmem);
cwirc_shm_free(ext_shmid);
cwirc_shm_detach(sharedmem);
usleep(250000);
return(-1);
}
/* The I/O process is running, possibly with nicer priority. The user
interface doesn't need that however, so renice ourselves down now. */
if(normal_priority!=-99)
setpriority(PRIO_PROCESS,0,normal_priority);
printf("CWirc enabled!\n");
fflush(stdout);
/* Run the user interface */
cwirc_ui(ext_shmid);
/* If an extension program is running, kill it */
if(ext_sharedmem->pid!=-1)
{
/* Try nicely at first */
kill(ext_sharedmem->pid,SIGHUP);
sleep(1);
/* If the process still isn't dead and reaped by now, be a little more
persuasive */
if(ext_sharedmem->pid!=-1)
{
kill(ext_sharedmem->pid,SIGKILL);
sleep(1);
}
}
/* Clean things up */
cwirc_sem_destroy(ext_sharedmem->semid);
cwirc_shm_detach(ext_sharedmem);
cwirc_shm_free(ext_shmid);
cwirc_shm_detach(sharedmem);
return(0);
}
/* SIGCHLD signal handler */
static void sigchld_hdlr(int nothing)
{
int pid;
/* Reap the child process that died */
pid=wait(NULL);
if(pid==io_process_pid) /* I/O process died */
sharedmem->stop_frontend=1; /* Terminate the entire frontend. */
else
if(pid==ext_sharedmem->pid) /* Extension process died */
ext_sharedmem->pid=-1;
}
/* Signal handler to make sure we reap the I/O process before dying. */
static void clean_exit_hdlr(int nothing)
{
/* Terminate the I/O process. */
sharedmem->stop_frontend=1;
}
|