/* tping.c A threaded ping tool. Makes use of OS provided ping tool. Copyright 2006 Srihari Vijayaraghavan Created on 12 May 2006 Last modified on 19 May 2006 Version 0.00 Released under the terms of GPL2. */ #include #include #include #include #include #define MAX_HOSTNAME_LEN 256 #define MAX_CMDLINE (MAX_HOSTNAME_LEN + 30) FILE *HOSTS_FILE; pthread_mutex_t hosts_file_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t console_mutex = PTHREAD_MUTEX_INITIALIZER; /* perror() */ void perr(char *msg) { perror(msg); exit(errno); } /* pthread_mutex_lock() */ void lock(pthread_mutex_t *mutex) { if (pthread_mutex_lock(mutex)) perr("pthread_mutex_lock()"); } /* pthread_mutex_unlock() */ void unlock(pthread_mutex_t *mutex) { if (pthread_mutex_unlock(mutex)) perr("pthread_mutex_unlock()"); } /* read one host per attempt. serialise using a mutex */ int get_a_hostname(FILE *F, char *hostname) { int i; lock(&hosts_file_mutex); i = fscanf(F, "%s", hostname); unlock(&hosts_file_mutex); return i; /* could be EOF */ } /* print ping failure message to stdout */ void print_ping_failure_msg(char *hostname) { lock(&console_mutex); printf("Failed to ping %s\n", hostname); unlock(&console_mutex); } /* ping a given hostname */ int ping_host(char *hostname) { char cmd[MAX_CMDLINE] = "ping -c1 "; strcat(cmd, hostname); strcat(cmd, " > /dev/null 2>&1"); return system(cmd); } /* our thread */ void ping_pthread(FILE *F) { char hostname[MAX_HOSTNAME_LEN]; while (get_a_hostname(F, hostname) != EOF) if (ping_host(hostname)) print_ping_failure_msg(hostname); pthread_exit(EXIT_SUCCESS); } int main(int argc, char **argv) { long numthreads, i; pthread_t *ping_threads; /* usage info */ if (argc != 3) { fprintf(stderr, "Usage: %s numthreads hostfname\n", *argv); exit(EXIT_FAILURE); } /* how many threads? */ numthreads = strtol(argv[1], NULL, 10); if (numthreads == EINVAL || numthreads == ERANGE) perr("strtol()"); if (!(ping_threads = malloc(numthreads * sizeof(pthread_t)))) perr("malloc()"); /* open the hosts file */ if (!(HOSTS_FILE = fopen(argv[2], "r"))) perr("fopen()"); /* create the threads */ for (i = 0; i < numthreads && !feof(HOSTS_FILE); i++) if (pthread_create(&ping_threads[i], NULL, (void *) &ping_pthread, HOSTS_FILE)) perr("pthread_create()"); /* join the threads */ while (i--) if (pthread_join(ping_threads[i], NULL)) perr("pthread_join()"); /* time to quit */ free(ping_threads); if (fclose(HOSTS_FILE)) perr("fclose()"); exit(EXIT_SUCCESS); }