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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/*
*
* Copyright (C) Cyclades Corporation, 1999-1999. All rights reserved.
*
*
* system.c
* Unix system-dependent routines
*
* History
* 08/17/1999 V.1.0.0 Initial revision
*
* Oct-27-2001 V.1.0.1
* Debug messages are only sent if debug is on
* SIGCLD may occur when syslogd is too busy
*/
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/param.h>
# include <sys/times.h>
# include <unistd.h>
# include <sys/time.h>
# include <syslog.h>
# include <errno.h>
# include <string.h>
# include <signal.h>
# include <stdlib.h>
# include <stdarg.h>
# include <stdio.h>
# define _TSR_SYSTEM_
#include "inc/cyclades-ser-cli.h"
#include "inc/system.h"
#include "inc/tsrio.h"
#include "inc/sock.h"
#include "inc/dev.h"
/*
* Internal Variables
*/
static int Start_time;
static int End_time;
static struct tms Timest;
/*
* Internal Functions
*/
static void sysc_tout(int sig);
static void rot(int sig);
static void user_hangup(int sig);
static void sys_times(char * buf);
void
init_system(void)
{
int sig ;
(void) setpgrp(); /* Detach from tty */
(void) umask(0); /* File creation mask */
Start_time = times (&Timest);
openlog (Pgname, LOG_PID | LOG_CONS, LOG_LOCAL2);
# ifdef TSR_MEASURE
start_measure();
# endif
(void) setbuf (stdout, NULL); /* Real time messages */
(void) setbuf (stderr, NULL);
for (sig = 1; sig < NSIG ; sig++) {
switch (sig) {
case SIGPIPE :
case SIGCONT : /* Close pty bug */
case SIGHUP :
(void) signal (sig, SIG_IGN);
break;
case SIGBUS :
case SIGSEGV :
break;
case SIGALRM :
(void) signal (sig, sysc_tout);
break;
case SIGUSR1 :
(void) signal (sig, user_hangup);
break;
default :
(void) signal (sig, rot);
break;
}
}
}
void
sysdelay(int msecs)
{
struct timeval tv;
tv.tv_sec = msecs / 1000;
tv.tv_usec = (msecs % 1000) * 1000;
(void) select (0,0,0,0,&tv);
}
void
sysmessage(int type, const char * const format, ...)
{
char buf[512];
va_list args;
const char * pritext;
int priority;
va_start(args, format);
vsprintf(buf, format, args);
if (Console) {
switch (type) {
case MSG_DEBUG:
pritext = "DEBUG";
break;
case MSG_INFO:
pritext = "INFO";
break;
case MSG_NOTICE:
pritext = "NOTICE";
break;
case MSG_WARNING:
pritext = "WARNING";
break;
case MSG_ERR:
default:
pritext = "ERR";
break;
}
fprintf (stderr, "%s: %s: %s", Idmsg, pritext, buf);
} else {
switch (type) {
case MSG_DEBUG:
priority = LOG_DEBUG;
break;
case MSG_INFO:
priority = LOG_INFO;
break;
case MSG_NOTICE:
priority = LOG_NOTICE;
break;
case MSG_WARNING:
priority = LOG_WARNING;
break;
case MSG_ERR:
default:
priority = LOG_ERR;
break;
}
if (priority != LOG_DEBUG || Debug > 0)
syslog (priority, "%s: %s", Idmsg, buf);
}
va_end(args);
}
void
mindelay(void)
{
struct timeval tv;
int usecs;
usecs = 1000000 / HZ; /* one CPU tick */
tv.tv_sec = 0;
tv.tv_usec = usecs;
(void) select (0,0,0,0,&tv);
}
void
doexit (int val)
{
char timbuf[64];
# ifdef TSR_MEASURE
cpu_measure();
# endif
dev_unlink();
sock_unlink();
End_time = times (&Timest);
sys_times(timbuf);
if (val) {
sysmessage (MSG_ERR,"Exiting with %d code (%s)\n",
val, timbuf);
} else {
sysmessage (MSG_NOTICE,"Exiting with %d code (%s)\n",
val, timbuf);
}
exit(val);
}
void
dev_unlink(void)
{
close(P_sfd);
P_sfd = -1;
close(P_mfd);
P_mfd = -1;
unlink(P_devname); /* remove old link */
if(P_contrname[0])
unlink(P_contrname); /* remove old control socket */
}
static void
rot (int sig)
{
char timbuf[64];
# ifdef TSR_MEASURE
cpu_measure(1);
# endif
dev_unlink();
sock_unlink();
End_time = times (&Timest);
sys_times(timbuf);
if ( sig == SIGTERM ) {
sysmessage (MSG_INFO,"Normal shutdown (SIGTERM) (%s)\n",
timbuf);
exit(E_NORMAL);
} else {
sysmessage (MSG_ERR, "signal %d received (%s)\n\n",
sig, timbuf);
exit (E_SIGNAL);
}
}
unsigned char *
mem_get (int size)
{
return ((unsigned char *) malloc (size));
}
void
mem_free (void * ptr)
{
free (ptr);
}
static void
sysc_tout(int sig)
{
alarm(0);
(void) signal (sig, sysc_tout);
}
static void
user_hangup(int sig)
{
Hang_up = TRUE;
(void) signal (sig, user_hangup);
}
static void
sys_times(char *buf)
{
int usr, sys, tot, pru, prs, prt, secs;
tot = End_time - Start_time;
usr = (int) Timest.tms_utime;
sys = (int) Timest.tms_stime;
if (tot == 0) tot = 1;
pru = usr * 100 / tot;
prs = sys * 100 / tot;
prt = pru + prs;
secs = tot / HZ;
sprintf(buf, "%6d, %3d%%, %3d%%, %3d%%", secs, pru, prs, prt);
}
|