1 /************************************************/
4 /* Jean-Marc Pigeon <jmp@safe.ca> 2009 */
6 /************************************************/
7 /* This program is free software; you can */
8 /* redistribute it and/or modify it under the */
9 /* terms of the GNU General Public License as */
10 /* published by the Free Software Foundation */
11 /* version 2 of the License */
13 /* This program is distributed in the hope that */
14 /* it will be useful, but WITHOUT ANY WARRANTY; */
15 /* without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR */
17 /* PURPOSE. See the GNU General Public License */
18 /* for more details. */
20 /* You should have received a copy of the GNU */
21 /* General Public License along with this */
22 /* program; if not, write to the Free Software */
23 /* Foundation, Inc., 51 Franklin Street, */
24 /* Fifth Floor, Boston, MA 02110-1301, USA. */
25 /************************************************/
28 /* take care of all low level process */
31 /************************************************/
38 #include <sys/resource.h>
39 #include <sys/prctl.h>
46 #define DEVNULL "/dev/null"
48 /*Process command override structure (proc) */
50 int max; /*title max size */
51 char **newargv; /*newly set argv list */
52 char *title; /*title storage area */
55 /*storage area for /proc title display */
56 static TITLTYP *title=(TITLTYP *)0;
60 /************************************************/
62 /* Procedure to catch a SIGALRM signal */
64 /************************************************/
65 static void trppace(int sig)
68 #define OPTR "subprc.c:trppace,"
70 static u_long tics=(u_long)0;
77 (void) fprintf(stderr,"%s Unexpected signal <%s> received\n",
78 OPTR,sys_siglist[sig]);
79 (void) fflush(stderr);
86 /************************************************/
88 /* Procedure to free used title memory */
90 /************************************************/
91 char **prc_cleantitle()
94 if (title!=(TITLTYP *)0) {
95 title->newargv=(char **)vec_freelstlst((void **)title->newargv,VECFREE);
96 if (environ!=(char **)0) {
99 for (i=0;environ[i]!=(char *)0;i++) {
100 (void) free(environ[i]);
101 environ[i]=(char *)0;
103 (void) free(environ);
114 /************************************************/
116 /* Procedure to find and limit space to */
117 /* be used as status information available */
118 /* from proc (ps ax) */
120 /************************************************/
121 char **prc_preptitle(int argc,char *argv[],char *env[])
124 extern char **environ;
129 (void) prc_cleantitle();
130 title=(TITLTYP *)calloc(1,sizeof(TITLTYP));
131 if (argv!=(char **)0) {
134 title->title=argv[0];
135 title->newargv=(char **)vec_addlstlst((void **)title->newargv,(void *)strdup(argv[0]));
136 for (i=1;argv[i]!=(char *)0;i++) {
139 lastend=argv[i]+strlen(argv[i]);
140 valeur=strdup(argv[i]);
141 title->newargv=(char **)vec_addlstlst((void **)title->newargv,(void *)valeur);
145 if (env!=(char **)0) {
149 for (i=0;env[i]!=(char *)0;i++) {
152 lastend=env[i]+strlen(env[i]);
153 valeur=strdup(env[i]);
154 (void) putenv(valeur);
157 title->max=lastend-title->title;
158 return title->newargv;
163 /************************************************/
165 /* Procedure to update title information. */
166 /* title information is available via the */
169 /************************************************/
170 void prc_settitle(const char *fmt,...)
176 if ((title!=(TITLTYP *)0)&&(title->title!=(char *)0)) {
177 (void) bzero(title->title,title->max);
178 (void) vsnprintf(title->title,title->max,fmt,args);
185 /************************************************/
187 /* Procedure to put a process in background*/
190 /************************************************/
191 void prc_divedivedive(int foreground)
194 #define OPDI "dbgprc.c:prc_divedivedive,"
199 (void) fprintf(stderr,"%s, Unable to dive! (error=<%s>)",
200 OPDI,strerror(errno));
201 /*lets continue if foregorund mode */
204 /*we are now in background mode */
205 if (foreground==0) /*always */
209 /*waiting for ballast to fill up :-}} */
211 (void) exit(0); /*just exit */
212 break; /*never reached */
219 /************************************************/
221 /* Procedure to start or stop a 'Pacemaker'*/
222 /* within the application. */
224 /************************************************/
225 void prc_pace(u_long millisec,int onoff)
228 int static done=false;
229 struct sigaction oldsa;
233 struct sigaction newsa;
234 struct itimerval period;
237 newsa.sa_handler=trppace;
238 (void) sigemptyset(&newsa.sa_mask);
239 (void) sigaction(SIGALRM,&newsa,&oldsa);
240 period.it_value.tv_sec=millisec/1000;
241 period.it_value.tv_usec=millisec*1000;
242 period.it_interval.tv_sec=period.it_value.tv_sec;
243 period.it_interval.tv_usec=period.it_value.tv_usec;
244 (void) setitimer(ITIMER_REAL,&period,(struct itimerval *)0);
250 struct itimerval period;
252 period.it_value.tv_sec=0;
253 period.it_value.tv_usec=0;
254 period.it_interval.tv_sec=period.it_value.tv_sec;
255 period.it_interval.tv_usec=period.it_value.tv_usec;
256 (void) setitimer(ITIMER_REAL,&period,(struct itimerval *)0);
257 (void) sigaction(SIGALRM,&oldsa,(struct sigaction *)0);