/* * ISA 563, Spring 2011 * Copyleft by Muhammad Abdulla */ #include #include #include #include #include /* * nonreentrant.c -- demonstrates problems associated with non-reentrant * functions. Namely, issues that arise when a non-reentrant * function is interrupted by another function. * * On my machine, however, rather than giving a "getpwnam error", * the program just gets blocked. Maybe getpwnam uses some kind * of a lock in its implementation now. */ static void my_alarm(int signo) { struct passwd *rootptr; fprintf(stderr, "in signal handler (1)\n"); if ((rootptr = getpwnam("root")) == NULL) fprintf(stderr, "getpwnam(root) error"); fprintf(stderr, "in signal handler (2)\n"); alarm(1); } int main(void) { struct passwd *ptr; signal(SIGALRM, my_alarm); alarm(1); for ( ; ; ) { if ((ptr = getpwnam("mabdulla")) == NULL) fprintf(stderr, "getpwnam error"); if (strcmp(ptr->pw_name, "mabdulla") != 0) fprintf(stderr, "return value corrupted!, pw_name = %s\n", ptr->pw_name); fprintf(stderr, "."); } return 0; }