PageRenderTime 23ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/runtime/Clib/csystem.c

https://github.com/mbrock/bigloo-llvm
C | 418 lines | 270 code | 52 blank | 96 comment | 23 complexity | c92259654ec7b12e67537a16e20b52fa MD5 | raw file
  1. /*=====================================================================*/
  2. /* serrano/prgm/project/bigloo/runtime/Clib/csystem.c */
  3. /* ------------------------------------------------------------- */
  4. /* Author : Manuel Serrano */
  5. /* Creation : Wed Jan 20 08:45:23 1993 */
  6. /* Last change : Tue Dec 28 12:31:39 2010 (serrano) */
  7. /* Copyright : 2002-10 Manuel Serrano */
  8. /* ------------------------------------------------------------- */
  9. /* System interface */
  10. /*=====================================================================*/
  11. #include <time.h>
  12. #include <sys/time.h>
  13. #include <signal.h>
  14. #include <string.h>
  15. #ifdef _MINGW_VER
  16. # define _BGL_WIN32_VER
  17. # include <io.h>
  18. # include <winsock2.h>
  19. # define lstat stat
  20. #else
  21. # include <sys/times.h>
  22. # ifdef _MSC_VER
  23. # define _BGL_WIN32_VER
  24. # include <io.h>
  25. # include <winsock2.h>
  26. # define lstat stat
  27. # else
  28. # include <unistd.h>
  29. # include <sys/socket.h>
  30. # include <netinet/in.h>
  31. # include <arpa/inet.h>
  32. # include <netdb.h>
  33. # endif
  34. #endif
  35. #include <bigloo.h>
  36. #if BGL_HAVE_GETUID
  37. # include <sys/types.h>
  38. # include <pwd.h>
  39. #else
  40. #define uid_t int
  41. #endif
  42. /*---------------------------------------------------------------------*/
  43. /* Signal mutex */
  44. /*---------------------------------------------------------------------*/
  45. static obj_t signal_mutex = BUNSPEC;
  46. DEFINE_STRING( signal_mutex_name, _1, "signal-mutex", 12 );
  47. static obj_t getuid_mutex = BUNSPEC;
  48. DEFINE_STRING( getuid_mutex_name, _2, "getuid-mutex", 12 );
  49. /*---------------------------------------------------------------------*/
  50. /* bgl_init_signal ... */
  51. /*---------------------------------------------------------------------*/
  52. void
  53. bgl_init_signal() {
  54. if( signal_mutex == BUNSPEC ) {
  55. signal_mutex = bgl_make_mutex( signal_mutex_name );
  56. }
  57. if( getuid_mutex == BUNSPEC ) {
  58. getuid_mutex = bgl_make_mutex( getuid_mutex_name );
  59. }
  60. }
  61. /*---------------------------------------------------------------------*/
  62. /* get_handler ... */
  63. /*---------------------------------------------------------------------*/
  64. static obj_t
  65. get_handler( int num ) {
  66. obj_t handler = BGL_SIG_HANDLERS()[ num ];
  67. /* Re-install the signal handler because some OS (such as Solaris) */
  68. /* de-install it when the signal is raised. */
  69. #if !HAVE_SIGACTION
  70. signal( num, (void (*)(int))(get_handler) );
  71. #endif
  72. return ((obj_t (*)())PROCEDURE_ENTRY(handler))( handler, BINT( num ), BEOA );
  73. }
  74. /*---------------------------------------------------------------------*/
  75. /* obj_t ... */
  76. /*---------------------------------------------------------------------*/
  77. obj_t
  78. c_signal( int sig, obj_t obj ) {
  79. bgl_mutex_lock( signal_mutex );
  80. if( PROCEDUREP( obj ) ) {
  81. /* store the obj in the signal table */
  82. BGL_SIG_HANDLERS()[ sig ] = obj;
  83. #if HAVE_SIGACTION
  84. {
  85. struct sigaction sigact;
  86. sigemptyset( &(sigact.sa_mask) );
  87. sigact.sa_handler = (void (*)( int ))get_handler;
  88. sigact.sa_flags = SA_RESTART;
  89. sigaction( sig, &sigact, NULL );
  90. }
  91. #else
  92. signal( (int)sig, (void (*)( int ))get_handler );
  93. #endif
  94. } else {
  95. /* store the obj in the signal table */
  96. BGL_SIG_HANDLERS()[ sig ] = obj;
  97. if( obj == BTRUE ) {
  98. signal( (int)sig, SIG_IGN );
  99. } else {
  100. if( obj == BFALSE ) {
  101. signal( (int)sig, SIG_DFL );
  102. }
  103. }
  104. }
  105. bgl_mutex_unlock( signal_mutex );
  106. return BUNSPEC;
  107. }
  108. /*---------------------------------------------------------------------*/
  109. /* obj_t */
  110. /* get_signal_handler ... */
  111. /*---------------------------------------------------------------------*/
  112. obj_t
  113. get_signal_handler( int sig ) {
  114. return BGL_SIG_HANDLERS()[ sig ];
  115. }
  116. /*---------------------------------------------------------------------*/
  117. /* int */
  118. /* bgl_sigprocmask ... */
  119. /*---------------------------------------------------------------------*/
  120. BGL_RUNTIME_DEF int
  121. bgl_sigprocmask( int set ) {
  122. #if HAVE_SIGPROCMASK
  123. if( !set ) {
  124. sigset_t mask;
  125. sigprocmask( SIG_SETMASK, 0, &mask );
  126. return sigprocmask( SIG_UNBLOCK, &mask, 0 );
  127. } else {
  128. return sigprocmask( SIG_SETMASK, (const sigset_t *)&set, 0 );
  129. }
  130. #else
  131. return 0;
  132. #endif
  133. }
  134. /*---------------------------------------------------------------------*/
  135. /* c_date ... */
  136. /*---------------------------------------------------------------------*/
  137. char *
  138. c_date() {
  139. #if( defined( sony_news ) )
  140. long now;
  141. #else
  142. time_t now;
  143. #endif
  144. now = time( 0L );
  145. return ctime( &now );
  146. }
  147. /*---------------------------------------------------------------------*/
  148. /* long */
  149. /* bgl_last_modification_time ... */
  150. /*---------------------------------------------------------------------*/
  151. long
  152. bgl_last_modification_time( char *file ) {
  153. struct stat _stat;
  154. if( lstat( file, &_stat ) )
  155. return -1;
  156. else
  157. return (long)(_stat.st_mtime);
  158. }
  159. /*---------------------------------------------------------------------*/
  160. /* long */
  161. /* bgl_file_size ... */
  162. /*---------------------------------------------------------------------*/
  163. BGL_RUNTIME_DEF long
  164. bgl_file_size( char *file ) {
  165. struct stat _stat;
  166. if( stat( file, &_stat ) )
  167. return -1;
  168. else
  169. return (long)_stat.st_size;
  170. }
  171. /*---------------------------------------------------------------------*/
  172. /* long */
  173. /* bgl_file_uid ... */
  174. /*---------------------------------------------------------------------*/
  175. long
  176. bgl_file_uid( char *file ) {
  177. struct stat _stat;
  178. if( lstat( file, &_stat ) )
  179. return -1;
  180. else
  181. return _stat.st_uid;
  182. }
  183. /*---------------------------------------------------------------------*/
  184. /* long */
  185. /* bgl_file_gid ... */
  186. /*---------------------------------------------------------------------*/
  187. long
  188. bgl_file_gid( char *file ) {
  189. struct stat _stat;
  190. if( lstat( file, &_stat ) )
  191. return -1;
  192. else
  193. return _stat.st_gid;
  194. }
  195. /*---------------------------------------------------------------------*/
  196. /* long */
  197. /* bgl_file_mode ... */
  198. /*---------------------------------------------------------------------*/
  199. long
  200. bgl_file_mode( char *file ) {
  201. struct stat _stat;
  202. if( stat( file, &_stat ) )
  203. return -1;
  204. else
  205. return _stat.st_mode;
  206. }
  207. /*---------------------------------------------------------------------*/
  208. /* int */
  209. /* bgl_chmod ... */
  210. /*---------------------------------------------------------------------*/
  211. int
  212. bgl_chmod( char *file, int read, int write, int exec ) {
  213. # ifndef _BGL_WIN32_VER
  214. return chmod( file,
  215. (read ? S_IRUSR : 0) |
  216. (write ? S_IWUSR : 0) |
  217. (exec ? S_IXUSR : 0) );
  218. # else
  219. return _chmod( file,
  220. (read ? S_IREAD : 0) |
  221. (write ? S_IWRITE : 0) );
  222. # endif
  223. }
  224. /*---------------------------------------------------------------------*/
  225. /* int */
  226. /* bgl_setenv ... */
  227. /*---------------------------------------------------------------------*/
  228. int
  229. bgl_setenv( char *id, char *val ) {
  230. size_t l1 = strlen( id ), l2 = strlen( val );
  231. char *s = malloc( l1 + l2 + 2 );
  232. strcpy( s, id );
  233. s[ l1 ] = '=';
  234. strcpy( &s[ l1 + 1 ], val );
  235. return putenv( s );
  236. }
  237. /*---------------------------------------------------------------------*/
  238. /* obj_t */
  239. /* bgl_time ... */
  240. /*---------------------------------------------------------------------*/
  241. BGL_RUNTIME_DEF obj_t
  242. bgl_time( obj_t thunk ) {
  243. #ifdef _MINGW_VER
  244. obj_t env = BGL_CURRENT_DYNAMIC_ENV();
  245. BGL_ENV_MVALUES_NUMBER_SET( env, 4 );
  246. BGL_ENV_MVALUES_VAL_SET( env, 1, 0 );
  247. BGL_ENV_MVALUES_VAL_SET( env, 2, 0 );
  248. BGL_ENV_MVALUES_VAL_SET( env, 3, 0 );
  249. return PROCEDURE_ENTRY( thunk )( thunk, BEOA );
  250. #else
  251. static long ctick = 0;
  252. obj_t env = BGL_CURRENT_DYNAMIC_ENV();
  253. struct tms buf1, buf2;
  254. clock_t t1, t2;
  255. obj_t res;
  256. if( !ctick ) ctick = sysconf( _SC_CLK_TCK );
  257. t1 = times( &buf1 );
  258. res = PROCEDURE_ENTRY( thunk )( thunk, BEOA );
  259. t2 = times( &buf2 );
  260. BGL_ENV_MVALUES_NUMBER_SET( env, 4 );
  261. # define BTICK( v ) BINT( (v) * 1000 / ctick )
  262. BGL_ENV_MVALUES_VAL_SET( env, 1, BTICK( t2 - t1 ) );
  263. BGL_ENV_MVALUES_VAL_SET( env, 2, BTICK( buf2.tms_stime - buf1.tms_stime ) );
  264. BGL_ENV_MVALUES_VAL_SET( env, 3, BTICK( buf2.tms_utime - buf1.tms_utime ) );
  265. # undef BTICK
  266. return res;
  267. #endif
  268. }
  269. /*---------------------------------------------------------------------*/
  270. /* int */
  271. /* bgl_getuid ... */
  272. /*---------------------------------------------------------------------*/
  273. BGL_RUNTIME_DEF int
  274. bgl_getuid() {
  275. #if BGL_HAVE_GETUID
  276. return getuid();
  277. #else
  278. return 0;
  279. #endif
  280. }
  281. /*---------------------------------------------------------------------*/
  282. /* int */
  283. /* bgl_setuid ... */
  284. /*---------------------------------------------------------------------*/
  285. BGL_RUNTIME_DEF int
  286. bgl_setuid( uid_t uid ) {
  287. #if BGL_HAVE_GETUID
  288. if( !setuid( uid ) ) {
  289. return uid;
  290. } else {
  291. C_SYSTEM_FAILURE( BGL_ERROR, "setuid", strerror( errno ), BINT( uid ) );
  292. return uid;
  293. }
  294. #else
  295. C_SYSTEM_FAILURE( BGL_ERROR, "setuid",
  296. "operation not supported", BINT( uid ) );
  297. return uid;
  298. #endif
  299. }
  300. /*---------------------------------------------------------------------*/
  301. /* obj_t */
  302. /* passwd2list ... */
  303. /*---------------------------------------------------------------------*/
  304. #if BGL_HAVE_GETUID
  305. static obj_t
  306. passwd2list( struct passwd *pw ) {
  307. if( !pw ) {
  308. return BFALSE;
  309. } else {
  310. obj_t res;
  311. /* the shell */
  312. res = MAKE_PAIR( string_to_bstring( pw->pw_shell ), BNIL );
  313. /* the home directory */
  314. res = MAKE_PAIR( string_to_bstring( pw->pw_dir ), res );
  315. /* the real name */
  316. #if BGL_HAVE_GECOS
  317. res = MAKE_PAIR( string_to_bstring( pw->pw_gecos ), res );
  318. #endif
  319. /* the group id */
  320. res = MAKE_PAIR( BINT( pw->pw_gid ), res );
  321. /* the user id */
  322. res = MAKE_PAIR( BINT( pw->pw_uid ), res );
  323. /* the password */
  324. res = MAKE_PAIR( string_to_bstring( pw->pw_passwd ), res );
  325. /* the name */
  326. res = MAKE_PAIR( string_to_bstring( pw->pw_name ), res );
  327. return res;
  328. }
  329. }
  330. #endif
  331. /*---------------------------------------------------------------------*/
  332. /* obj_t */
  333. /* bgl_getpwnam ... */
  334. /*---------------------------------------------------------------------*/
  335. obj_t
  336. bgl_getpwnam( char *name ) {
  337. #if BGL_HAVE_GETUID
  338. struct passwd *pw;
  339. obj_t res;
  340. bgl_mutex_lock( getuid_mutex );
  341. pw = getpwnam( name );
  342. res = passwd2list( pw );
  343. bgl_mutex_unlock( getuid_mutex );
  344. return res;
  345. #else
  346. return BFALSE;
  347. #endif
  348. }
  349. /*---------------------------------------------------------------------*/
  350. /* obj_t */
  351. /* bgl_getpwuid ... */
  352. /*---------------------------------------------------------------------*/
  353. obj_t
  354. bgl_getpwuid( uid_t uid ) {
  355. #if BGL_HAVE_GETUID
  356. struct passwd *pw;
  357. obj_t res;
  358. bgl_mutex_lock( getuid_mutex );
  359. pw = getpwuid( uid );
  360. res = passwd2list( pw );
  361. bgl_mutex_unlock( getuid_mutex );
  362. return res;
  363. #else
  364. return BFALSE;
  365. #endif
  366. }