/[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 642 - (hide annotations)
Wed Jun 7 11:03:17 2006 UTC (16 years, 1 month ago) by michael
File MIME type: text/x-chdr
File size: 5290 byte(s)
- Moved setup_corefile() to libio

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 adx 65 struct timeval SystemTime;
29 adx 30
30     static const char *months[] =
31     {
32     "January", "February", "March", "April",
33     "May", "June", "July", "August",
34     "September", "October", "November","December"
35     };
36    
37     static const char *weekdays[] =
38     {
39     "Sunday", "Monday", "Tuesday", "Wednesday",
40     "Thursday", "Friday", "Saturday"
41     };
42    
43     char *
44     date(time_t lclock)
45     {
46     static char buf[80], plus;
47     struct tm *lt, *gm;
48     struct tm gmbuf;
49     int minswest;
50    
51     if (!lclock)
52     lclock = CurrentTime;
53     gm = gmtime(&lclock);
54     memcpy(&gmbuf, gm, sizeof(gmbuf));
55     gm = &gmbuf;
56     lt = localtime(&lclock);
57    
58     /*
59     * There is unfortunately no clean portable way to extract time zone
60     * offset information, so do ugly things.
61     */
62     minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
63    
64     if (lt->tm_yday != gm->tm_yday)
65     {
66     if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
67     (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
68     minswest -= 24 * 60;
69     else
70     minswest += 24 * 60;
71     }
72    
73     plus = (minswest > 0) ? '-' : '+';
74     if (minswest < 0)
75     minswest = -minswest;
76    
77     ircsprintf(buf, "%s %s %d %d -- %02u:%02u:%02u %c%02u:%02u",
78     weekdays[lt->tm_wday], months[lt->tm_mon],lt->tm_mday,
79     lt->tm_year + 1900, lt->tm_hour, lt->tm_min, lt->tm_sec,
80     plus, minswest/60, minswest%60);
81     return buf;
82     }
83    
84     const char *
85     smalldate(time_t lclock)
86     {
87     static char buf[MAX_DATE_STRING];
88     struct tm *lt, *gm;
89     struct tm gmbuf;
90    
91     if (!lclock)
92     lclock = CurrentTime;
93    
94     gm = gmtime(&lclock);
95     memcpy(&gmbuf, gm, sizeof(gmbuf));
96     gm = &gmbuf;
97     lt = localtime(&lclock);
98    
99     ircsprintf(buf, "%d/%d/%d %02d.%02d",
100     lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
101     lt->tm_hour, lt->tm_min);
102    
103     return buf;
104     }
105    
106     /* small_file_date()
107     * Make a small YYYYMMDD formatted string suitable for a
108     * dated file stamp.
109     */
110     char *
111     small_file_date(time_t lclock)
112     {
113     static char timebuffer[MAX_DATE_STRING];
114     struct tm *tmptr;
115    
116     if (!lclock)
117     time(&lclock);
118    
119     tmptr = localtime(&lclock);
120     strftime(timebuffer, MAX_DATE_STRING, "%Y%m%d", tmptr);
121    
122     return timebuffer;
123     }
124    
125     #ifdef HAVE_LIBCRYPTO
126     char *
127     ssl_get_cipher(SSL *ssl)
128     {
129     static char buffer[128];
130     const char *name = NULL;
131     int bits;
132    
133     switch (ssl->session->ssl_version)
134     {
135     case SSL2_VERSION:
136     name = "SSLv2";
137     break;
138    
139     case SSL3_VERSION:
140     name = "SSLv3";
141     break;
142    
143     case TLS1_VERSION:
144     name = "TLSv1";
145     break;
146    
147     default:
148     name = "UNKNOWN";
149     }
150    
151     SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &bits);
152    
153     snprintf(buffer, sizeof(buffer), "%s %s-%d",
154     name, SSL_get_cipher(ssl), bits);
155    
156     return buffer;
157     }
158     #endif
159 adx 68
160     void
161     set_time(void)
162     {
163     struct timeval newtime;
164     #ifdef _WIN32
165     FILETIME ft;
166    
167     GetSystemTimeAsFileTime(&ft);
168     if (ft.dwLowDateTime < 0xd53e8000)
169     ft.dwHighDateTime--;
170     ft.dwLowDateTime -= 0xd53e8000;
171     ft.dwHighDateTime -= 0x19db1de;
172    
173     newtime.tv_sec = (*(uint64_t *) &ft) / 10000000;
174     newtime.tv_usec = (*(uint64_t *) &ft) / 10 % 1000000;
175     #else
176     newtime.tv_sec = 0;
177     newtime.tv_usec = 0;
178     gettimeofday(&newtime, NULL);
179     #endif
180    
181     if (newtime.tv_sec < CurrentTime)
182     {
183     ilog(L_CRIT, "System clock is running backwards - (%lu < %lu)",
184     (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
185     set_back_events(CurrentTime - newtime.tv_sec);
186     }
187    
188     SystemTime.tv_sec = newtime.tv_sec;
189     SystemTime.tv_usec = newtime.tv_usec;
190     }
191 adx 86
192 michael 642 /* setup_corefile()
193     *
194     * inputs - nothing
195     * output - nothing
196     * side effects - setups corefile to system limits.
197     * -kre
198     */
199 adx 86 void
200 michael 642 setup_corefile(void)
201     {
202     #ifdef HAVE_SYS_RESOURCE_H
203     struct rlimit rlim; /* resource limits */
204    
205     /* Set corefilesize to maximum */
206     if (!getrlimit(RLIMIT_CORE, &rlim))
207     {
208     rlim.rlim_cur = rlim.rlim_max;
209     setrlimit(RLIMIT_CORE, &rlim);
210     }
211     #endif
212     }
213    
214     void
215 metalrock 362 libio_init(int daemonn)
216 adx 86 {
217 adx 87 #ifndef _WIN32
218 metalrock 362 if (daemonn)
219 adx 87 close_standard_fds();
220     #endif
221    
222 adx 86 /* It ain't random, but it ought to be a little harder to guess */
223     srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
224    
225     set_time();
226     eventInit();
227     fdlist_init();
228     init_comm();
229     #ifndef NOBALLOC
230     initBlockHeap();
231     #endif
232     init_dlink_nodes();
233     dbuf_init();
234     #ifndef _WIN32
235     init_resolver();
236     #endif
237     }

Properties

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

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