Software /
code /
prosody
Comparison
util-src/pposix.c @ 10480:94cacf9fd0ae
util.*.c: Add static qualifiers everywhere
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 01 Dec 2019 20:25:20 +0100 |
parent | 10411:db2a06b9ff98 |
child | 10799:763bb2ce3f60 |
comparison
equal
deleted
inserted
replaced
10479:d362934437eb | 10480:94cacf9fd0ae |
---|---|
135 return 2; | 135 return 2; |
136 } | 136 } |
137 | 137 |
138 /* Syslog support */ | 138 /* Syslog support */ |
139 | 139 |
140 const char *const facility_strings[] = { | 140 static const char *const facility_strings[] = { |
141 "auth", | 141 "auth", |
142 #if !(defined(sun) || defined(__sun)) | 142 #if !(defined(sun) || defined(__sun)) |
143 "authpriv", | 143 "authpriv", |
144 #endif | 144 #endif |
145 "cron", | 145 "cron", |
161 "syslog", | 161 "syslog", |
162 "user", | 162 "user", |
163 "uucp", | 163 "uucp", |
164 NULL | 164 NULL |
165 }; | 165 }; |
166 int facility_constants[] = { | 166 static int facility_constants[] = { |
167 LOG_AUTH, | 167 LOG_AUTH, |
168 #if !(defined(sun) || defined(__sun)) | 168 #if !(defined(sun) || defined(__sun)) |
169 LOG_AUTHPRIV, | 169 LOG_AUTHPRIV, |
170 #endif | 170 #endif |
171 LOG_CRON, | 171 LOG_CRON, |
197 prepending the changed string, and if the string it points to ceases to | 197 prepending the changed string, and if the string it points to ceases to |
198 exist, the results are undefined. Most portable is to use a string | 198 exist, the results are undefined. Most portable is to use a string |
199 constant. | 199 constant. |
200 " -- syslog manpage | 200 " -- syslog manpage |
201 */ | 201 */ |
202 char *syslog_ident = NULL; | 202 static char *syslog_ident = NULL; |
203 | 203 |
204 int lc_syslog_open(lua_State *L) { | 204 static int lc_syslog_open(lua_State *L) { |
205 int facility = luaL_checkoption(L, 2, "daemon", facility_strings); | 205 int facility = luaL_checkoption(L, 2, "daemon", facility_strings); |
206 facility = facility_constants[facility]; | 206 facility = facility_constants[facility]; |
207 | 207 |
208 luaL_checkstring(L, 1); | 208 luaL_checkstring(L, 1); |
209 | 209 |
215 | 215 |
216 openlog(syslog_ident, LOG_PID, facility); | 216 openlog(syslog_ident, LOG_PID, facility); |
217 return 0; | 217 return 0; |
218 } | 218 } |
219 | 219 |
220 const char *const level_strings[] = { | 220 static const char *const level_strings[] = { |
221 "debug", | 221 "debug", |
222 "info", | 222 "info", |
223 "notice", | 223 "notice", |
224 "warn", | 224 "warn", |
225 "error", | 225 "error", |
226 NULL | 226 NULL |
227 }; | 227 }; |
228 int level_constants[] = { | 228 static int level_constants[] = { |
229 LOG_DEBUG, | 229 LOG_DEBUG, |
230 LOG_INFO, | 230 LOG_INFO, |
231 LOG_NOTICE, | 231 LOG_NOTICE, |
232 LOG_WARNING, | 232 LOG_WARNING, |
233 LOG_CRIT, | 233 LOG_CRIT, |
234 -1 | 234 -1 |
235 }; | 235 }; |
236 int lc_syslog_log(lua_State *L) { | 236 static int lc_syslog_log(lua_State *L) { |
237 int level = level_constants[luaL_checkoption(L, 1, "notice", level_strings)]; | 237 int level = level_constants[luaL_checkoption(L, 1, "notice", level_strings)]; |
238 | 238 |
239 if(lua_gettop(L) == 3) { | 239 if(lua_gettop(L) == 3) { |
240 syslog(level, "%s: %s", luaL_checkstring(L, 2), luaL_checkstring(L, 3)); | 240 syslog(level, "%s: %s", luaL_checkstring(L, 2), luaL_checkstring(L, 3)); |
241 } else { | 241 } else { |
243 } | 243 } |
244 | 244 |
245 return 0; | 245 return 0; |
246 } | 246 } |
247 | 247 |
248 int lc_syslog_close(lua_State *L) { | 248 static int lc_syslog_close(lua_State *L) { |
249 (void)L; | 249 (void)L; |
250 closelog(); | 250 closelog(); |
251 | 251 |
252 if(syslog_ident) { | 252 if(syslog_ident) { |
253 free(syslog_ident); | 253 free(syslog_ident); |
255 } | 255 } |
256 | 256 |
257 return 0; | 257 return 0; |
258 } | 258 } |
259 | 259 |
260 int lc_syslog_setmask(lua_State *L) { | 260 static int lc_syslog_setmask(lua_State *L) { |
261 int level_idx = luaL_checkoption(L, 1, "notice", level_strings); | 261 int level_idx = luaL_checkoption(L, 1, "notice", level_strings); |
262 int mask = 0; | 262 int mask = 0; |
263 | 263 |
264 do { | 264 do { |
265 mask |= LOG_MASK(level_constants[level_idx]); | 265 mask |= LOG_MASK(level_constants[level_idx]); |
269 return 0; | 269 return 0; |
270 } | 270 } |
271 | 271 |
272 /* getpid */ | 272 /* getpid */ |
273 | 273 |
274 int lc_getpid(lua_State *L) { | 274 static int lc_getpid(lua_State *L) { |
275 lua_pushinteger(L, getpid()); | 275 lua_pushinteger(L, getpid()); |
276 return 1; | 276 return 1; |
277 } | 277 } |
278 | 278 |
279 /* UID/GID functions */ | 279 /* UID/GID functions */ |
280 | 280 |
281 int lc_getuid(lua_State *L) { | 281 static int lc_getuid(lua_State *L) { |
282 lua_pushinteger(L, getuid()); | 282 lua_pushinteger(L, getuid()); |
283 return 1; | 283 return 1; |
284 } | 284 } |
285 | 285 |
286 int lc_getgid(lua_State *L) { | 286 static int lc_getgid(lua_State *L) { |
287 lua_pushinteger(L, getgid()); | 287 lua_pushinteger(L, getgid()); |
288 return 1; | 288 return 1; |
289 } | 289 } |
290 | 290 |
291 int lc_setuid(lua_State *L) { | 291 static int lc_setuid(lua_State *L) { |
292 int uid = -1; | 292 int uid = -1; |
293 | 293 |
294 if(lua_gettop(L) < 1) { | 294 if(lua_gettop(L) < 1) { |
295 return 0; | 295 return 0; |
296 } | 296 } |
344 lua_pushboolean(L, 0); | 344 lua_pushboolean(L, 0); |
345 lua_pushstring(L, "invalid-uid"); | 345 lua_pushstring(L, "invalid-uid"); |
346 return 2; | 346 return 2; |
347 } | 347 } |
348 | 348 |
349 int lc_setgid(lua_State *L) { | 349 static int lc_setgid(lua_State *L) { |
350 int gid = -1; | 350 int gid = -1; |
351 | 351 |
352 if(lua_gettop(L) < 1) { | 352 if(lua_gettop(L) < 1) { |
353 return 0; | 353 return 0; |
354 } | 354 } |
402 lua_pushboolean(L, 0); | 402 lua_pushboolean(L, 0); |
403 lua_pushstring(L, "invalid-gid"); | 403 lua_pushstring(L, "invalid-gid"); |
404 return 2; | 404 return 2; |
405 } | 405 } |
406 | 406 |
407 int lc_initgroups(lua_State *L) { | 407 static int lc_initgroups(lua_State *L) { |
408 int ret; | 408 int ret; |
409 gid_t gid; | 409 gid_t gid; |
410 struct passwd *p; | 410 struct passwd *p; |
411 | 411 |
412 if(!lua_isstring(L, 1)) { | 412 if(!lua_isstring(L, 1)) { |
466 } | 466 } |
467 | 467 |
468 return 2; | 468 return 2; |
469 } | 469 } |
470 | 470 |
471 int lc_umask(lua_State *L) { | 471 static int lc_umask(lua_State *L) { |
472 char old_mode_string[7]; | 472 char old_mode_string[7]; |
473 mode_t old_mode = umask(strtoul(luaL_checkstring(L, 1), NULL, 8)); | 473 mode_t old_mode = umask(strtoul(luaL_checkstring(L, 1), NULL, 8)); |
474 | 474 |
475 snprintf(old_mode_string, sizeof(old_mode_string), "%03o", old_mode); | 475 snprintf(old_mode_string, sizeof(old_mode_string), "%03o", old_mode); |
476 old_mode_string[sizeof(old_mode_string) - 1] = 0; | 476 old_mode_string[sizeof(old_mode_string) - 1] = 0; |
477 lua_pushstring(L, old_mode_string); | 477 lua_pushstring(L, old_mode_string); |
478 | 478 |
479 return 1; | 479 return 1; |
480 } | 480 } |
481 | 481 |
482 int lc_mkdir(lua_State *L) { | 482 static int lc_mkdir(lua_State *L) { |
483 int ret = mkdir(luaL_checkstring(L, 1), S_IRUSR | S_IWUSR | S_IXUSR | 483 int ret = mkdir(luaL_checkstring(L, 1), S_IRUSR | S_IWUSR | S_IXUSR |
484 | S_IRGRP | S_IWGRP | S_IXGRP | 484 | S_IRGRP | S_IWGRP | S_IXGRP |
485 | S_IROTH | S_IXOTH); /* mode 775 */ | 485 | S_IROTH | S_IXOTH); /* mode 775 */ |
486 | 486 |
487 lua_pushboolean(L, ret == 0); | 487 lua_pushboolean(L, ret == 0); |
502 * Any negative limit will be replace with the current limit by an additional call of getrlimit(). | 502 * Any negative limit will be replace with the current limit by an additional call of getrlimit(). |
503 * | 503 * |
504 * Example usage: | 504 * Example usage: |
505 * pposix.setrlimit("NOFILE", 1000, 2000) | 505 * pposix.setrlimit("NOFILE", 1000, 2000) |
506 */ | 506 */ |
507 int string2resource(const char *s) { | 507 static int string2resource(const char *s) { |
508 if(!strcmp(s, "CORE")) { | 508 if(!strcmp(s, "CORE")) { |
509 return RLIMIT_CORE; | 509 return RLIMIT_CORE; |
510 } | 510 } |
511 | 511 |
512 if(!strcmp(s, "CPU")) { | 512 if(!strcmp(s, "CPU")) { |
552 | 552 |
553 #endif | 553 #endif |
554 return -1; | 554 return -1; |
555 } | 555 } |
556 | 556 |
557 rlim_t arg_to_rlimit(lua_State *L, int idx, rlim_t current) { | 557 static rlim_t arg_to_rlimit(lua_State *L, int idx, rlim_t current) { |
558 switch(lua_type(L, idx)) { | 558 switch(lua_type(L, idx)) { |
559 case LUA_TSTRING: | 559 case LUA_TSTRING: |
560 | 560 |
561 if(strcmp(lua_tostring(L, idx), "unlimited") == 0) { | 561 if(strcmp(lua_tostring(L, idx), "unlimited") == 0) { |
562 return RLIM_INFINITY; | 562 return RLIM_INFINITY; |
573 default: | 573 default: |
574 return luaL_argerror(L, idx, "unexpected type"); | 574 return luaL_argerror(L, idx, "unexpected type"); |
575 } | 575 } |
576 } | 576 } |
577 | 577 |
578 int lc_setrlimit(lua_State *L) { | 578 static int lc_setrlimit(lua_State *L) { |
579 struct rlimit lim; | 579 struct rlimit lim; |
580 int arguments = lua_gettop(L); | 580 int arguments = lua_gettop(L); |
581 int rid = -1; | 581 int rid = -1; |
582 | 582 |
583 if(arguments < 1 || arguments > 3) { | 583 if(arguments < 1 || arguments > 3) { |
612 | 612 |
613 lua_pushboolean(L, 1); | 613 lua_pushboolean(L, 1); |
614 return 1; | 614 return 1; |
615 } | 615 } |
616 | 616 |
617 int lc_getrlimit(lua_State *L) { | 617 static int lc_getrlimit(lua_State *L) { |
618 int arguments = lua_gettop(L); | 618 int arguments = lua_gettop(L); |
619 const char *resource = NULL; | 619 const char *resource = NULL; |
620 int rid = -1; | 620 int rid = -1; |
621 struct rlimit lim; | 621 struct rlimit lim; |
622 | 622 |
657 } | 657 } |
658 | 658 |
659 return 3; | 659 return 3; |
660 } | 660 } |
661 | 661 |
662 int lc_abort(lua_State *L) { | 662 static int lc_abort(lua_State *L) { |
663 (void)L; | 663 (void)L; |
664 abort(); | 664 abort(); |
665 return 0; | 665 return 0; |
666 } | 666 } |
667 | 667 |
668 int lc_uname(lua_State *L) { | 668 static int lc_uname(lua_State *L) { |
669 struct utsname uname_info; | 669 struct utsname uname_info; |
670 | 670 |
671 if(uname(&uname_info) != 0) { | 671 if(uname(&uname_info) != 0) { |
672 lua_pushnil(L); | 672 lua_pushnil(L); |
673 lua_pushstring(L, strerror(errno)); | 673 lua_pushstring(L, strerror(errno)); |
690 lua_setfield(L, -2, "domainname"); | 690 lua_setfield(L, -2, "domainname"); |
691 #endif | 691 #endif |
692 return 1; | 692 return 1; |
693 } | 693 } |
694 | 694 |
695 int lc_setenv(lua_State *L) { | 695 static int lc_setenv(lua_State *L) { |
696 const char *var = luaL_checkstring(L, 1); | 696 const char *var = luaL_checkstring(L, 1); |
697 const char *value; | 697 const char *value; |
698 | 698 |
699 /* If the second argument is nil or nothing, unset the var */ | 699 /* If the second argument is nil or nothing, unset the var */ |
700 if(lua_isnoneornil(L, 2)) { | 700 if(lua_isnoneornil(L, 2)) { |
719 lua_pushboolean(L, 1); | 719 lua_pushboolean(L, 1); |
720 return 1; | 720 return 1; |
721 } | 721 } |
722 | 722 |
723 #ifdef WITH_MALLINFO | 723 #ifdef WITH_MALLINFO |
724 int lc_meminfo(lua_State *L) { | 724 static int lc_meminfo(lua_State *L) { |
725 struct mallinfo info = mallinfo(); | 725 struct mallinfo info = mallinfo(); |
726 lua_createtable(L, 0, 5); | 726 lua_createtable(L, 0, 5); |
727 /* This is the total size of memory allocated with sbrk by malloc, in bytes. */ | 727 /* This is the total size of memory allocated with sbrk by malloc, in bytes. */ |
728 lua_pushinteger(L, (unsigned)info.arena); | 728 lua_pushinteger(L, (unsigned)info.arena); |
729 lua_setfield(L, -2, "allocated"); | 729 lua_setfield(L, -2, "allocated"); |
747 /* | 747 /* |
748 * Append some data to a file handle | 748 * Append some data to a file handle |
749 * Attempt to allocate space first | 749 * Attempt to allocate space first |
750 * Truncate to original size on failure | 750 * Truncate to original size on failure |
751 */ | 751 */ |
752 int lc_atomic_append(lua_State *L) { | 752 static int lc_atomic_append(lua_State *L) { |
753 int err; | 753 int err; |
754 size_t len; | 754 size_t len; |
755 | 755 |
756 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE); | 756 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE); |
757 const char *data = luaL_checklstring(L, 2, &len); | 757 const char *data = luaL_checklstring(L, 2, &len); |