Mercurial > hg
annotate mcabber/src/utils.c @ 446:9f4e9e9aaf08
Minor changes & cleanup
See diff for details.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 25 Sep 2005 00:44:11 +0200 |
parents | 4470868f90e5 |
children | dfd9c62b3a39 |
rev | line source |
---|---|
241 | 1 /* |
2 * utils.c -- Various utility functions | |
393 | 3 * |
241 | 4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> |
5 * ut_* functions are derived from Cabber debug/log code. | |
6 * from_iso8601() comes from the Gaim project. | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or (at | |
11 * your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
21 * USA | |
22 */ | |
23 | |
24 | 24 #include <stdio.h> |
25 #include <stdlib.h> | |
77 | 26 #include <string.h> |
24 | 27 #include <stdarg.h> |
28 #include <time.h> | |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
29 #include <unistd.h> |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
30 #include <sys/types.h> |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
31 #include <sys/stat.h> |
24 | 32 |
241 | 33 #include <config.h> |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
34 #include "logprint.h" |
241 | 35 |
76 | 36 static int DebugEnabled; |
77 | 37 static char *FName; |
24 | 38 |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
241
diff
changeset
|
39 void ut_InitDebug(unsigned int level, const char *filename) |
24 | 40 { |
77 | 41 FILE *fp; |
428
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
42 struct stat buf; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
43 int err; |
77 | 44 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
45 if (level < 1) { |
138 | 46 DebugEnabled = 0; |
47 FName = NULL; | |
48 return; | |
49 } | |
50 | |
51 if (filename) | |
52 FName = strdup(filename); | |
77 | 53 else { |
138 | 54 FName = getenv("HOME"); |
55 if (!FName) | |
56 FName = "/tmp/mcabberlog"; | |
57 else { | |
58 char *tmpname = malloc(strlen(FName) + 12); | |
59 strcpy(tmpname, FName); | |
60 strcat(tmpname, "/mcabberlog"); | |
61 FName = tmpname; | |
62 } | |
77 | 63 } |
24 | 64 |
65 DebugEnabled = level; | |
76 | 66 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
67 fp = fopen(FName, "a"); |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
68 if (!fp) { |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
69 fprintf(stderr, "ERROR: Cannot open tracelog file\n"); |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
70 return; |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
71 } |
428
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
72 |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
73 err = fstat(fileno(fp), &buf); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
74 if (err || buf.st_uid != geteuid()) { |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
75 fclose(fp); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
76 DebugEnabled = 0; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
77 FName = NULL; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
78 if (err) { |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
79 fprintf(stderr, "ERROR: cannot stat the tracelog file!\n"); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
80 } else { |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
81 fprintf(stderr, "ERROR: tracelog file does not belong to you!\n"); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
82 } |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
83 return; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
84 } |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
85 fchmod(fileno(fp), S_IRUSR|S_IWUSR); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
86 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
87 fprintf(fp, "New trace log started.\n" |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
88 "----------------------\n"); |
24 | 89 fclose(fp); |
90 } | |
91 | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
92 void ut_WriteLog(unsigned int flag, const char *data) |
24 | 93 { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
94 if (!DebugEnabled || !FName) return; |
393 | 95 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
96 if (((DebugEnabled == 2) && (flag & (LPRINT_LOG|LPRINT_DEBUG))) || |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
97 ((DebugEnabled == 1) && (flag & LPRINT_LOG))) { |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
98 FILE *fp = fopen(FName, "a+"); |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
99 if (!fp) { |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
100 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file"); |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
101 return; |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
102 } |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
103 fputs(data, fp); |
24 | 104 fclose(fp); |
105 } | |
106 } | |
107 | |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
108 // checkset_perm(name, setmode) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
109 // Check the permissions of the "name" file/dir |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
110 // If setmode is true, correct the permissions if they are wrong |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
111 // Return values: -1 == bad file/dir, 0 == success, 1 == cannot correct |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
112 int checkset_perm(const char *name, unsigned int setmode) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
113 { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
114 int fd; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
115 struct stat buf; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
116 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
117 fd = lstat(name, &buf); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
118 if (fd == -1) return -1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
119 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
120 if (buf.st_uid != geteuid()) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
121 scr_LogPrint(LPRINT_LOGNORM, "Wrong file owner [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
122 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
123 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
124 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
125 if (buf.st_mode & (S_IRGRP | S_IWGRP | S_IXGRP) || |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
126 buf.st_mode & (S_IROTH | S_IWOTH | S_IXOTH)) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
127 if (setmode) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
128 mode_t newmode = 0; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
129 scr_LogPrint(LPRINT_LOGNORM, "Bad permissions [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
130 if (S_ISDIR(buf.st_mode)) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
131 newmode |= S_IXUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
132 newmode |= S_IRUSR | S_IWUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
133 if (chmod(name, newmode)) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
134 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Failed to correct permissions!"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
135 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
136 } |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
137 scr_LogPrint(LPRINT_LOGNORM, "Permissions have been corrected"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
138 } else { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
139 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Bad permissions [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
140 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
141 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
142 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
143 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
144 return 0; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
145 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
146 |
241 | 147 // to_iso8601(dststr, timestamp) |
148 // Convert timestamp to iso8601 format, and store it in dststr. | |
149 // NOTE: dststr should be at last 19 chars long. | |
150 // Return should be 0 | |
151 int to_iso8601(char *dststr, time_t timestamp) | |
152 { | |
153 struct tm *tm_time; | |
154 int ret; | |
155 | |
156 tm_time = gmtime(×tamp); | |
157 | |
158 ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ", | |
159 1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday, | |
160 tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec); | |
161 | |
162 return ((ret == -1) ? -1 : 0); | |
163 } | |
164 | |
165 // from_iso8601(timestamp, utc) | |
166 // This function comes from the Gaim project, gaim_str_to_time(). | |
167 // (Actually date may not be pure iso-8601) | |
168 // Thanks, guys! | |
169 time_t from_iso8601(const char *timestamp, int utc) | |
170 { | |
171 struct tm t; | |
172 time_t retval = 0; | |
173 char buf[32]; | |
174 char *c; | |
175 int tzoff = 0; | |
176 | |
177 time(&retval); | |
178 localtime_r(&retval, &t); | |
179 | |
180 snprintf(buf, sizeof(buf), "%s", timestamp); | |
181 c = buf; | |
182 | |
183 /* 4 digit year */ | |
184 if (!sscanf(c, "%04d", &t.tm_year)) return 0; | |
185 c+=4; | |
186 if (*c == '-') | |
187 c++; | |
188 | |
189 t.tm_year -= 1900; | |
190 | |
191 /* 2 digit month */ | |
192 if (!sscanf(c, "%02d", &t.tm_mon)) return 0; | |
193 c+=2; | |
194 if (*c == '-') | |
195 c++; | |
196 | |
197 t.tm_mon -= 1; | |
198 | |
199 /* 2 digit day */ | |
200 if (!sscanf(c, "%02d", &t.tm_mday)) return 0; | |
201 c+=2; | |
202 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */ | |
203 c++; /* skip the "T" */ | |
204 | |
205 /* 2 digit hour */ | |
206 if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 || | |
207 sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) { | |
208 int tzhrs, tzmins; | |
209 c+=8; | |
210 if (*c == '.') /* dealing with precision we don't care about */ | |
211 c += 4; | |
212 | |
213 if ((*c == '+' || *c == '-') && | |
214 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { | |
215 tzoff = tzhrs*60*60 + tzmins*60; | |
216 if (*c == '+') | |
217 tzoff *= -1; | |
218 } | |
219 | |
220 if (tzoff || utc) { | |
221 | |
222 //#ifdef HAVE_TM_GMTOFF | |
223 tzoff += t.tm_gmtoff; | |
224 //#else | |
225 //# ifdef HAVE_TIMEZONE | |
226 // tzset(); /* making sure */ | |
227 // tzoff -= timezone; | |
228 //# endif | |
229 //#endif | |
230 } | |
231 } | |
232 } | |
233 | |
234 t.tm_isdst = -1; | |
235 | |
236 retval = mktime(&t); | |
237 | |
238 retval += tzoff; | |
239 | |
240 return retval; | |
241 } | |
419 | 242 |
243 // Should only be used for delays < 1s | |
244 inline void safe_usleep(unsigned int usec) | |
245 { | |
246 struct timespec req; | |
247 req.tv_sec = 0; | |
248 req.tv_nsec = (long)usec * 1000L; | |
249 nanosleep(&req, NULL); | |
250 } |