summaryrefslogtreecommitdiff
path: root/other/b-scan/tmp/src/system.c
diff options
context:
space:
mode:
authorRoot THC2026-02-24 12:42:47 +0000
committerRoot THC2026-02-24 12:42:47 +0000
commitc9cbeced5b3f2bdd7407e29c0811e65954132540 (patch)
treeaefc355416b561111819de159ccbd86c3004cf88 /other/b-scan/tmp/src/system.c
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/b-scan/tmp/src/system.c')
-rw-r--r--other/b-scan/tmp/src/system.c349
1 files changed, 349 insertions, 0 deletions
diff --git a/other/b-scan/tmp/src/system.c b/other/b-scan/tmp/src/system.c
new file mode 100644
index 0000000..a3ccc94
--- /dev/null
+++ b/other/b-scan/tmp/src/system.c
@@ -0,0 +1,349 @@
1#include <sys/types.h>
2#include <sys/ipc.h>
3#include <sys/shm.h>
4#include <sys/mman.h>
5#include <sys/time.h>
6#include <stdio.h>
7#include <unistd.h>
8#include <stdlib.h>
9#include <string.h>
10#include <ctype.h>
11#include <fcntl.h>
12#include <time.h>
13
14#ifndef SHMMNI
15#define SHMMNI 100
16#endif
17
18#define MAXSHM 4 /* its bscan. we need <4 shm's */
19
20static int shm_id = -1; /* last used id */
21static int shm_c = -1; /* shm_alloc counter */
22
23static struct _shm
24{
25 int id;
26 void *ptr;
27}
28shm[MAXSHM];
29
30
31/*
32 * uhm. hard job for the process coz the process
33 * does not get the shm_id. uhm. i should make a static traking list
34 * of all shared memory segments (addr <-> id mapping maybe ?)
35 * on the other hand...since an attachment count is maintained for
36 * the shared memory segment the segment gets removed when the last
37 * process using the segment terminates or detaches it.
38 * hmm. Seems the following function is completly useless....:>
39 * Hey. why are you reading my comments ? write me: anonymous@segfault.net!
40 */
41int
42shmfree (int shm_id)
43{
44 if (shm_id < 0)
45 return (-1);
46 return (shmctl (shm_id, IPC_RMID, 0));
47}
48
49/*
50 * kill ALL shm's
51 * uhm. this is brutal. but shm is risky. you can bring
52 * down ANY system if you waste all shm's. Shm's dont get
53 * freed on process-exit !!! syslog will fail, inetd will fail, ..
54 * any program that tries to alloc shared memory...nono good :>
55 * root can use 'ipcrm shm <id>' do free the shm's.
56 * that's why we use this brutal "killall"-method.
57 * something else: killall-method is realy BRUTAL. believe me!
58 * If you have other functions registered on exit (atexit)
59 * and you try to reference to a shm within these function...you are lost
60 * Unexpected things will happen....
61 */
62void
63shmkillall ()
64{
65 int c;
66
67 for (c = 0; c < shm_c; c++)
68 shmfree (shm[c].id);
69}
70
71/*
72 * allocate shared memory (poor but fast IPC)
73 * the value returned is a pointer to the allocated
74 * memory, which is suitably aligned for any kind of
75 * variable, or NULL if the request fails.
76 *
77 * TODO: on SVR4 use open("/dev/zero", O_RDWR); and mmap trick for speedup
78 */
79void *
80shmalloc (int flag, size_t length)
81{
82 void *shm_addr;
83 int c = 0;
84
85 if (shm_c == -1) /* init all the internal shm stuff */
86 {
87 atexit (shmkillall);
88 shm_c = 0;
89 }
90
91 if (shm_c >= MAXSHM)
92 return (NULL); /* no space left in list. no bscan ?? */
93
94 if (flag == 0)
95 flag = (IPC_CREAT | IPC_EXCL | SHM_R | SHM_W);
96
97 while (c < SHMMNI) /* brute force a NEW shared memory section */
98 if ((shm_id = shmget (getpid () + c++, length, flag)) != -1)
99 break;
100 else
101 return (NULL);
102
103 if ((shm_addr = shmat (shm_id, NULL, 0)) == NULL)
104 return (NULL);
105
106 shm[shm_c].id = shm_id;
107 shm[shm_c].ptr = shm_addr;
108 shm_c++; /* increase shm-counter */
109
110 return (shm_addr);
111}
112
113#ifdef WITH_NANOSLEEP
114/* add lib '-lrt' */
115/*
116 * nanosec must be in the range 0 to 999 999 999
117 * ..we dont care about signals here...
118 */
119void
120do_nanosleep (time_t sec, long nsec)
121{
122 struct timespec mynano;
123 mynano.tv_sec = sec;
124 mynano.tv_nsec = nsec;
125 nanosleep (&mynano, NULL);
126}
127#endif
128
129
130/*
131 * xchange data p1 <-> p2 of length len
132 */
133void
134xchange (void *p1, void *p2, int len)
135{
136 unsigned char buf[len];
137
138 memcpy (buf, p1, len);
139 memcpy (p1, p2, len);
140 memcpy (p2, buf, len);
141}
142
143/*
144 * calculate time-difference now - in
145 * and return diff in 'now'
146 */
147void
148time_diff (struct timeval *in, struct timeval *now)
149{
150 if ((now->tv_usec -= in->tv_usec) < 0)
151 {
152 now->tv_sec--;
153 now->tv_usec += 1000000;
154 }
155 now->tv_sec -= in->tv_sec;
156}
157
158/*
159 * converts a 'esc-sequenced' string to normal string
160 * return string in dst.
161 * returns 0 on success
162 * todo: \ddd decoding
163 */
164int
165ctoreal(char *src, char *dst)
166{
167 char c;
168
169 if ((src == NULL) || (dst == NULL))
170 {
171 dst = NULL;
172 return(0); /* yes, its ok. */
173 }
174
175 while (*src != '\0')
176 if (*src == '\\')
177 {
178 switch((c = *++src))
179 {
180 case 'n':
181 *dst++ = '\n';
182 break;
183 case 'r':
184 *dst++ = '\r';
185 break;
186 case 't':
187 *dst++ = '\t';
188 break;
189 case '\\':
190 *dst++ = '\\';
191 break;
192 case 's':
193 *dst++ = ' ';
194 break;
195 default:
196 *dst++ = c;
197 /* printf("unknown escape sequence 0x%2.2x\n", c);*/
198 break;
199 }
200 src++;
201 } else
202 {
203 *dst++ = *src++;
204 }
205 *dst = '\0';
206 return(0);
207}
208
209
210/*
211 * parse data, format data and print to fd (only prinatable chars)
212 * supress \r, nonprintable -> '_';
213 * output line by line [\n] with 'prefix' before each line.
214 * prefix is a 0-terminated string
215 */
216void
217save_write(FILE *fd, char *prefix, unsigned char *data, int data_len)
218{
219 int c;
220 unsigned char *ptr = data;
221 unsigned char *startptr = data;
222 const char trans[] =
223 "................................ !\"#$%&'()*+,-./0123456789"
224 ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
225 "nopqrstuvwxyz{|}~...................................."
226 "....................................................."
227 "........................................";
228
229
230 if (prefix == NULL)
231 prefix = "";
232
233 for (c = 0; c < data_len; c++)
234 {
235 if (*data == '\r') /* i dont like these */
236 {
237 data++;
238 continue;
239 }
240 if (*data == '\n')
241 {
242 *ptr = '\0';
243 fprintf (fd, "%s%s\n", prefix, startptr);
244 startptr = ++data;
245 ptr = startptr;
246 continue;
247 }
248
249 *ptr++ = trans[*data++];
250
251 }
252
253 if (ptr != startptr)
254 {
255 *ptr = '\0';
256 fprintf (fd, "%s%s\n", prefix, startptr);
257 }
258
259}
260
261/*
262 * check if data contains any non-printable chars [except \n]
263 * return 0 if yes,,,,1 if not.
264 */
265int
266isprintdata(char *ptr, int len)
267{
268 char c;
269
270 while(len-- > 0)
271 {
272 c = *ptr++;
273 if (c == '\n')
274 continue;
275 if (!isprint((int)c))
276 return(0);
277 }
278
279 return(1);
280}
281
282/*
283 * convert some data into hex string
284 * We DO 0 terminate the string.
285 * dest = destination
286 * destlen = max. length of dest (size of allocated memory)
287 * data = (non)printable input data
288 * len = input data len
289 * return 0 on success, 1 if data does not fit into dest, -1 on error
290 */
291int
292dat2hexstr(unsigned char *dest, unsigned int destlen, unsigned char *data,
293 unsigned int len)
294{
295 unsigned int i = 0;
296 unsigned int slen = 0;
297 unsigned char *ptr = dest;
298 unsigned char c;
299 char hex[] = "0123456789ABCDEF";
300
301 memset(dest, '\0', destlen);
302
303 while (i++ < len)
304 {
305 c = *data++;
306 if (slen + 3 < destlen)
307 {
308 *dest++ = hex[c / 16];
309 *dest++ = hex[c % 16];
310 *dest++ = ' ';
311 slen += 3;
312 ptr += 3;
313 } else {
314 return(1);
315 }
316 }
317
318 return(0);
319}
320
321
322/* dat2strip
323 *
324 * print the data at `data', which is `len' bytes long to the char
325 * array `dest', which is `destlen' characters long. filter out any
326 * non-printables. NUL terminate the dest array in every case.
327 *
328 * return the number of characters written
329 */
330
331int
332dat2strip(unsigned char *dest, unsigned int destlen, unsigned char *data,
333 unsigned int len)
334{
335 unsigned char *dp;
336
337 for (dp = dest ; dp - dest < destlen && len > 0 ; --len, ++data, ++dp) {
338 if (isprint (*data))
339 *dp = *data;
340 }
341
342 if (dp - dest < destlen)
343 *dp = '\0';
344 dest[destlen - 1] = '\0';
345
346 return (dp - dest);
347}
348
349