/[svn]/ircd-hybrid/libio/misc/misc.c
ViewVC logotype

Annotation of /ircd-hybrid/libio/misc/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 868 - (hide annotations)
Mon Sep 3 15:35:30 2007 UTC (13 years, 2 months ago) by adx
File MIME type: text/x-chdr
File size: 9864 byte(s)
+ rand() was actually not initialized - same sequences after each reboot...

  This is TOO obvious. How's it possible that noone's ever noticed this?

1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * s_misc.c: Yet another miscellaneous functions file.
4     *
5     * Copyright (C) 2002 by the past and present ircd coders, and others.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22 knight 31 * $Id$
23 adx 30 */
24    
25 adx 86 #define IN_MISC_C
26 adx 30 #include "stdinc.h"
27    
28 michael 684 #ifndef _WIN32
29     #include <sys/utsname.h>
30     #endif
31    
32 adx 65 struct timeval SystemTime;
33 adx 30
34     static const char *months[] =
35     {
36     "January", "February", "March", "April",
37     "May", "June", "July", "August",
38     "September", "October", "November","December"
39     };
40    
41     static const char *weekdays[] =
42     {
43     "Sunday", "Monday", "Tuesday", "Wednesday",
44     "Thursday", "Friday", "Saturday"
45     };
46    
47     char *
48     date(time_t lclock)
49     {
50     static char buf[80], plus;
51     struct tm *lt, *gm;
52     struct tm gmbuf;
53     int minswest;
54    
55     if (!lclock)
56     lclock = CurrentTime;
57     gm = gmtime(&lclock);
58     memcpy(&gmbuf, gm, sizeof(gmbuf));
59     gm = &gmbuf;
60     lt = localtime(&lclock);
61    
62     /*
63     * There is unfortunately no clean portable way to extract time zone
64     * offset information, so do ugly things.
65     */
66     minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
67    
68     if (lt->tm_yday != gm->tm_yday)
69     {
70     if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
71     (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
72     minswest -= 24 * 60;
73     else
74     minswest += 24 * 60;
75     }
76    
77     plus = (minswest > 0) ? '-' : '+';
78     if (minswest < 0)
79     minswest = -minswest;
80    
81     ircsprintf(buf, "%s %s %d %d -- %02u:%02u:%02u %c%02u:%02u",
82     weekdays[lt->tm_wday], months[lt->tm_mon],lt->tm_mday,
83     lt->tm_year + 1900, lt->tm_hour, lt->tm_min, lt->tm_sec,
84     plus, minswest/60, minswest%60);
85     return buf;
86     }
87    
88     const char *
89     smalldate(time_t lclock)
90     {
91     static char buf[MAX_DATE_STRING];
92 bear 851 static time_t last_time = 0;
93 adx 30 struct tm *lt, *gm;
94     struct tm gmbuf;
95    
96     if (!lclock)
97     lclock = CurrentTime;
98    
99 bear 851 // Check if our cache still applies
100     if (lclock == last_time)
101     return buf;
102    
103     last_time = lclock;
104    
105 adx 30 gm = gmtime(&lclock);
106     memcpy(&gmbuf, gm, sizeof(gmbuf));
107     gm = &gmbuf;
108     lt = localtime(&lclock);
109    
110     ircsprintf(buf, "%d/%d/%d %02d.%02d",
111     lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
112     lt->tm_hour, lt->tm_min);
113    
114     return buf;
115     }
116    
117     /* small_file_date()
118     * Make a small YYYYMMDD formatted string suitable for a
119     * dated file stamp.
120     */
121     char *
122     small_file_date(time_t lclock)
123     {
124     static char timebuffer[MAX_DATE_STRING];
125     struct tm *tmptr;
126    
127     if (!lclock)
128     time(&lclock);
129    
130     tmptr = localtime(&lclock);
131     strftime(timebuffer, MAX_DATE_STRING, "%Y%m%d", tmptr);
132    
133     return timebuffer;
134     }
135    
136 michael 655
137     #ifdef _WIN32
138     /* Copyright (C) 2001 Free Software Foundation, Inc.
139     *
140     * Get name and information about current kernel.
141     */
142     int
143     uname(struct utsname *uts)
144     {
145     enum { WinNT, Win95, Win98, WinUnknown };
146     OSVERSIONINFO osver;
147     SYSTEM_INFO sysinfo;
148     DWORD sLength;
149     DWORD os = WinUnknown;
150    
151     memset(uts, 0, sizeof(*uts));
152    
153 michael 656 osver.dwOSVersionInfoSize = sizeof(osver);
154     GetVersionEx(&osver);
155     GetSystemInfo(&sysinfo);
156 michael 655
157     switch (osver.dwPlatformId)
158 michael 656 {
159 michael 655 case VER_PLATFORM_WIN32_NT: /* NT, Windows 2000 or Windows XP */
160     if (osver.dwMajorVersion == 4)
161 michael 656 strcpy (uts->sysname, "Windows NT4x"); /* NT4x */
162 michael 655 else if (osver.dwMajorVersion <= 3)
163 michael 656 strcpy (uts->sysname, "Windows NT3x"); /* NT3x */
164 michael 655 else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion < 1)
165 michael 656 strcpy (uts->sysname, "Windows 2000"); /* 2k */
166 michael 655 else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2)
167 michael 656 strcpy (uts->sysname, "Windows 2003"); /* 2003 */
168 michael 655 else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1)
169 michael 656 strcpy (uts->sysname, "Windows XP"); /* XP */
170     else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 0)
171     strcpy (uts->sysname, "Windows Vista"); /* Vista */
172 michael 655 os = WinNT;
173     break;
174    
175     case VER_PLATFORM_WIN32_WINDOWS: /* Win95, Win98 or WinME */
176     if ((osver.dwMajorVersion > 4) ||
177     ((osver.dwMajorVersion == 4) && (osver.dwMinorVersion > 0)))
178 michael 656 {
179     if (osver.dwMinorVersion >= 90)
180     strcpy (uts->sysname, "Windows ME"); /* ME */
181     else
182     strcpy (uts->sysname, "Windows 98"); /* 98 */
183     os = Win98;
184     }
185 michael 655 else
186 michael 656 {
187     strcpy (uts->sysname, "Windows 95"); /* 95 */
188     os = Win95;
189     }
190 michael 655 break;
191    
192     case VER_PLATFORM_WIN32s: /* Windows 3.x */
193     strcpy (uts->sysname, "Windows");
194     break;
195 michael 656 }
196 michael 655
197 michael 656 sprintf(uts->version, "%ld.%02ld",
198     osver.dwMajorVersion, osver.dwMinorVersion);
199 michael 655
200     if (osver.szCSDVersion[0] != '\0' &&
201     (strlen (osver.szCSDVersion) + strlen (uts->version) + 1) <
202     sizeof (uts->version))
203     {
204     strcat (uts->version, " ");
205     strcat (uts->version, osver.szCSDVersion);
206     }
207    
208     sprintf (uts->release, "build %ld", osver.dwBuildNumber & 0xFFFF);
209    
210     switch (sysinfo.wProcessorArchitecture)
211     {
212     case PROCESSOR_ARCHITECTURE_PPC:
213     strcpy (uts->machine, "ppc");
214     break;
215     case PROCESSOR_ARCHITECTURE_ALPHA:
216     strcpy (uts->machine, "alpha");
217     break;
218     case PROCESSOR_ARCHITECTURE_MIPS:
219     strcpy (uts->machine, "mips");
220     break;
221     case PROCESSOR_ARCHITECTURE_INTEL:
222     /*
223     * dwProcessorType is only valid in Win95 and Win98 and WinME
224     * wProcessorLevel is only valid in WinNT
225     */
226     switch (os)
227 michael 656 {
228 michael 655 case Win95:
229     case Win98:
230     switch (sysinfo.dwProcessorType)
231 michael 656 {
232 michael 655 case PROCESSOR_INTEL_386:
233     case PROCESSOR_INTEL_486:
234     case PROCESSOR_INTEL_PENTIUM:
235 michael 656 sprintf(uts->machine, "i%ld", sysinfo.dwProcessorType);
236 michael 655 break;
237     default:
238 michael 656 strcpy(uts->machine, "i386");
239 michael 655 break;
240     }
241     break;
242     case WinNT:
243     switch(sysinfo.wProcessorArchitecture)
244     {
245     case PROCESSOR_ARCHITECTURE_INTEL:
246     sprintf (uts->machine, "x86(%d)", sysinfo.wProcessorLevel);
247     break;
248     case PROCESSOR_ARCHITECTURE_IA64:
249     sprintf (uts->machine, "ia64(%d)", sysinfo.wProcessorLevel);
250     break;
251     #ifdef PROCESSOR_ARCHITECTURE_AMD64
252     case PROCESSOR_ARCHITECTURE_AMD64:
253     sprintf (uts->machine, "x86_64(%d)", sysinfo.wProcessorLevel);
254     break;
255     #endif
256     default:
257     sprintf (uts->machine, "unknown(%d)", sysinfo.wProcessorLevel);
258     break;
259     }
260     break;
261     default:
262 michael 656 strcpy(uts->machine, "unknown");
263 michael 655 }
264     break;
265     default:
266     strcpy (uts->machine, "unknown");
267     break;
268     }
269    
270 michael 656 sLength = sizeof(uts->nodename) - 1;
271     GetComputerName(uts->nodename, &sLength);
272 michael 655 return 0;
273     }
274     #endif
275    
276 michael 684 char *
277     libio_get_platform(char *str, size_t size)
278     {
279     struct utsname uts;
280    
281     uname(&uts);
282     snprintf(str, size, "%s %s %s %s %s", uts.sysname, uts.nodename,
283     uts.release, uts.version, uts.machine);
284     return str;
285     }
286    
287 adx 30 #ifdef HAVE_LIBCRYPTO
288     char *
289     ssl_get_cipher(SSL *ssl)
290     {
291     static char buffer[128];
292     const char *name = NULL;
293     int bits;
294    
295     switch (ssl->session->ssl_version)
296     {
297     case SSL2_VERSION:
298     name = "SSLv2";
299     break;
300    
301     case SSL3_VERSION:
302     name = "SSLv3";
303     break;
304    
305     case TLS1_VERSION:
306     name = "TLSv1";
307     break;
308    
309     default:
310     name = "UNKNOWN";
311     }
312    
313     SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &bits);
314    
315     snprintf(buffer, sizeof(buffer), "%s %s-%d",
316     name, SSL_get_cipher(ssl), bits);
317    
318     return buffer;
319     }
320     #endif
321 adx 68
322     void
323     set_time(void)
324     {
325     struct timeval newtime;
326     #ifdef _WIN32
327     FILETIME ft;
328    
329     GetSystemTimeAsFileTime(&ft);
330     if (ft.dwLowDateTime < 0xd53e8000)
331     ft.dwHighDateTime--;
332     ft.dwLowDateTime -= 0xd53e8000;
333     ft.dwHighDateTime -= 0x19db1de;
334    
335     newtime.tv_sec = (*(uint64_t *) &ft) / 10000000;
336     newtime.tv_usec = (*(uint64_t *) &ft) / 10 % 1000000;
337     #else
338     newtime.tv_sec = 0;
339     newtime.tv_usec = 0;
340     gettimeofday(&newtime, NULL);
341     #endif
342    
343     if (newtime.tv_sec < CurrentTime)
344     {
345     ilog(L_CRIT, "System clock is running backwards - (%lu < %lu)",
346     (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
347     set_back_events(CurrentTime - newtime.tv_sec);
348     }
349    
350     SystemTime.tv_sec = newtime.tv_sec;
351     SystemTime.tv_usec = newtime.tv_usec;
352     }
353 adx 86
354 michael 642 /* setup_corefile()
355     *
356     * inputs - nothing
357     * output - nothing
358     * side effects - setups corefile to system limits.
359     * -kre
360     */
361 adx 86 void
362 michael 642 setup_corefile(void)
363     {
364     #ifdef HAVE_SYS_RESOURCE_H
365     struct rlimit rlim; /* resource limits */
366    
367     /* Set corefilesize to maximum */
368     if (!getrlimit(RLIMIT_CORE, &rlim))
369     {
370     rlim.rlim_cur = rlim.rlim_max;
371     setrlimit(RLIMIT_CORE, &rlim);
372     }
373     #endif
374     }
375    
376     void
377 metalrock 362 libio_init(int daemonn)
378 adx 86 {
379 adx 87 #ifndef _WIN32
380 metalrock 362 if (daemonn)
381 adx 87 close_standard_fds();
382     #endif
383    
384 adx 868 set_time();
385    
386     // It ain't random, but it ought to be a little harder to guess
387 adx 86 srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
388    
389     eventInit();
390     fdlist_init();
391     init_comm();
392     #ifndef NOBALLOC
393     initBlockHeap();
394     #endif
395     init_dlink_nodes();
396     dbuf_init();
397     #ifndef _WIN32
398     init_resolver();
399     #endif
400     }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28