/[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 684 - (hide annotations)
Thu Jun 15 18:18:08 2006 UTC (15 years, 7 months ago) by michael
File MIME type: text/x-chdr
File size: 9730 byte(s)
- Moved ircd_get_platform to libio and killed version.c

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     struct tm *lt, *gm;
93     struct tm gmbuf;
94    
95     if (!lclock)
96     lclock = CurrentTime;
97    
98     gm = gmtime(&lclock);
99     memcpy(&gmbuf, gm, sizeof(gmbuf));
100     gm = &gmbuf;
101     lt = localtime(&lclock);
102    
103     ircsprintf(buf, "%d/%d/%d %02d.%02d",
104     lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
105     lt->tm_hour, lt->tm_min);
106    
107     return buf;
108     }
109    
110     /* small_file_date()
111     * Make a small YYYYMMDD formatted string suitable for a
112     * dated file stamp.
113     */
114     char *
115     small_file_date(time_t lclock)
116     {
117     static char timebuffer[MAX_DATE_STRING];
118     struct tm *tmptr;
119    
120     if (!lclock)
121     time(&lclock);
122    
123     tmptr = localtime(&lclock);
124     strftime(timebuffer, MAX_DATE_STRING, "%Y%m%d", tmptr);
125    
126     return timebuffer;
127     }
128    
129 michael 655
130     #ifdef _WIN32
131     /* Copyright (C) 2001 Free Software Foundation, Inc.
132     *
133     * Get name and information about current kernel.
134     */
135     int
136     uname(struct utsname *uts)
137     {
138     enum { WinNT, Win95, Win98, WinUnknown };
139     OSVERSIONINFO osver;
140     SYSTEM_INFO sysinfo;
141     DWORD sLength;
142     DWORD os = WinUnknown;
143    
144     memset(uts, 0, sizeof(*uts));
145    
146 michael 656 osver.dwOSVersionInfoSize = sizeof(osver);
147     GetVersionEx(&osver);
148     GetSystemInfo(&sysinfo);
149 michael 655
150     switch (osver.dwPlatformId)
151 michael 656 {
152 michael 655 case VER_PLATFORM_WIN32_NT: /* NT, Windows 2000 or Windows XP */
153     if (osver.dwMajorVersion == 4)
154 michael 656 strcpy (uts->sysname, "Windows NT4x"); /* NT4x */
155 michael 655 else if (osver.dwMajorVersion <= 3)
156 michael 656 strcpy (uts->sysname, "Windows NT3x"); /* NT3x */
157 michael 655 else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion < 1)
158 michael 656 strcpy (uts->sysname, "Windows 2000"); /* 2k */
159 michael 655 else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2)
160 michael 656 strcpy (uts->sysname, "Windows 2003"); /* 2003 */
161 michael 655 else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1)
162 michael 656 strcpy (uts->sysname, "Windows XP"); /* XP */
163     else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 0)
164     strcpy (uts->sysname, "Windows Vista"); /* Vista */
165 michael 655 os = WinNT;
166     break;
167    
168     case VER_PLATFORM_WIN32_WINDOWS: /* Win95, Win98 or WinME */
169     if ((osver.dwMajorVersion > 4) ||
170     ((osver.dwMajorVersion == 4) && (osver.dwMinorVersion > 0)))
171 michael 656 {
172     if (osver.dwMinorVersion >= 90)
173     strcpy (uts->sysname, "Windows ME"); /* ME */
174     else
175     strcpy (uts->sysname, "Windows 98"); /* 98 */
176     os = Win98;
177     }
178 michael 655 else
179 michael 656 {
180     strcpy (uts->sysname, "Windows 95"); /* 95 */
181     os = Win95;
182     }
183 michael 655 break;
184    
185     case VER_PLATFORM_WIN32s: /* Windows 3.x */
186     strcpy (uts->sysname, "Windows");
187     break;
188 michael 656 }
189 michael 655
190 michael 656 sprintf(uts->version, "%ld.%02ld",
191     osver.dwMajorVersion, osver.dwMinorVersion);
192 michael 655
193     if (osver.szCSDVersion[0] != '\0' &&
194     (strlen (osver.szCSDVersion) + strlen (uts->version) + 1) <
195     sizeof (uts->version))
196     {
197     strcat (uts->version, " ");
198     strcat (uts->version, osver.szCSDVersion);
199     }
200    
201     sprintf (uts->release, "build %ld", osver.dwBuildNumber & 0xFFFF);
202    
203     switch (sysinfo.wProcessorArchitecture)
204     {
205     case PROCESSOR_ARCHITECTURE_PPC:
206     strcpy (uts->machine, "ppc");
207     break;
208     case PROCESSOR_ARCHITECTURE_ALPHA:
209     strcpy (uts->machine, "alpha");
210     break;
211     case PROCESSOR_ARCHITECTURE_MIPS:
212     strcpy (uts->machine, "mips");
213     break;
214     case PROCESSOR_ARCHITECTURE_INTEL:
215     /*
216     * dwProcessorType is only valid in Win95 and Win98 and WinME
217     * wProcessorLevel is only valid in WinNT
218     */
219     switch (os)
220 michael 656 {
221 michael 655 case Win95:
222     case Win98:
223     switch (sysinfo.dwProcessorType)
224 michael 656 {
225 michael 655 case PROCESSOR_INTEL_386:
226     case PROCESSOR_INTEL_486:
227     case PROCESSOR_INTEL_PENTIUM:
228 michael 656 sprintf(uts->machine, "i%ld", sysinfo.dwProcessorType);
229 michael 655 break;
230     default:
231 michael 656 strcpy(uts->machine, "i386");
232 michael 655 break;
233     }
234     break;
235     case WinNT:
236     switch(sysinfo.wProcessorArchitecture)
237     {
238     case PROCESSOR_ARCHITECTURE_INTEL:
239     sprintf (uts->machine, "x86(%d)", sysinfo.wProcessorLevel);
240     break;
241     case PROCESSOR_ARCHITECTURE_IA64:
242     sprintf (uts->machine, "ia64(%d)", sysinfo.wProcessorLevel);
243     break;
244     #ifdef PROCESSOR_ARCHITECTURE_AMD64
245     case PROCESSOR_ARCHITECTURE_AMD64:
246     sprintf (uts->machine, "x86_64(%d)", sysinfo.wProcessorLevel);
247     break;
248     #endif
249     default:
250     sprintf (uts->machine, "unknown(%d)", sysinfo.wProcessorLevel);
251     break;
252     }
253     break;
254     default:
255 michael 656 strcpy(uts->machine, "unknown");
256 michael 655 }
257     break;
258     default:
259     strcpy (uts->machine, "unknown");
260     break;
261     }
262    
263 michael 656 sLength = sizeof(uts->nodename) - 1;
264     GetComputerName(uts->nodename, &sLength);
265 michael 655 return 0;
266     }
267     #endif
268    
269 michael 684 char *
270     libio_get_platform(char *str, size_t size)
271     {
272     struct utsname uts;
273    
274     uname(&uts);
275     snprintf(str, size, "%s %s %s %s %s", uts.sysname, uts.nodename,
276     uts.release, uts.version, uts.machine);
277     return str;
278     }
279    
280 adx 30 #ifdef HAVE_LIBCRYPTO
281     char *
282     ssl_get_cipher(SSL *ssl)
283     {
284     static char buffer[128];
285     const char *name = NULL;
286     int bits;
287    
288     switch (ssl->session->ssl_version)
289     {
290     case SSL2_VERSION:
291     name = "SSLv2";
292     break;
293    
294     case SSL3_VERSION:
295     name = "SSLv3";
296     break;
297    
298     case TLS1_VERSION:
299     name = "TLSv1";
300     break;
301    
302     default:
303     name = "UNKNOWN";
304     }
305    
306     SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &bits);
307    
308     snprintf(buffer, sizeof(buffer), "%s %s-%d",
309     name, SSL_get_cipher(ssl), bits);
310    
311     return buffer;
312     }
313     #endif
314 adx 68
315     void
316     set_time(void)
317     {
318     struct timeval newtime;
319     #ifdef _WIN32
320     FILETIME ft;
321    
322     GetSystemTimeAsFileTime(&ft);
323     if (ft.dwLowDateTime < 0xd53e8000)
324     ft.dwHighDateTime--;
325     ft.dwLowDateTime -= 0xd53e8000;
326     ft.dwHighDateTime -= 0x19db1de;
327    
328     newtime.tv_sec = (*(uint64_t *) &ft) / 10000000;
329     newtime.tv_usec = (*(uint64_t *) &ft) / 10 % 1000000;
330     #else
331     newtime.tv_sec = 0;
332     newtime.tv_usec = 0;
333     gettimeofday(&newtime, NULL);
334     #endif
335    
336     if (newtime.tv_sec < CurrentTime)
337     {
338     ilog(L_CRIT, "System clock is running backwards - (%lu < %lu)",
339     (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
340     set_back_events(CurrentTime - newtime.tv_sec);
341     }
342    
343     SystemTime.tv_sec = newtime.tv_sec;
344     SystemTime.tv_usec = newtime.tv_usec;
345     }
346 adx 86
347 michael 642 /* setup_corefile()
348     *
349     * inputs - nothing
350     * output - nothing
351     * side effects - setups corefile to system limits.
352     * -kre
353     */
354 adx 86 void
355 michael 642 setup_corefile(void)
356     {
357     #ifdef HAVE_SYS_RESOURCE_H
358     struct rlimit rlim; /* resource limits */
359    
360     /* Set corefilesize to maximum */
361     if (!getrlimit(RLIMIT_CORE, &rlim))
362     {
363     rlim.rlim_cur = rlim.rlim_max;
364     setrlimit(RLIMIT_CORE, &rlim);
365     }
366     #endif
367     }
368    
369     void
370 metalrock 362 libio_init(int daemonn)
371 adx 86 {
372 adx 87 #ifndef _WIN32
373 metalrock 362 if (daemonn)
374 adx 87 close_standard_fds();
375     #endif
376    
377 adx 86 /* It ain't random, but it ought to be a little harder to guess */
378     srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
379    
380     set_time();
381     eventInit();
382     fdlist_init();
383     init_comm();
384     #ifndef NOBALLOC
385     initBlockHeap();
386     #endif
387     init_dlink_nodes();
388     dbuf_init();
389     #ifndef _WIN32
390     init_resolver();
391     #endif
392     }

Properties

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

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