#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "btime_int.h" static unsigned int get_btime(void); int main(int argc, char **argv) { int sd; char msg[BTIME_MSGLEN]; struct sockaddr_in cli_addr; int cli_addr_len; ssize_t nbytes; openlog("btimed", LOG_PID, LOG_USER); /* Running out of (x)inetd the socket was duped onto stdin. */ sd = fileno(stdin); memset(&cli_addr, 0, sizeof cli_addr); cli_addr_len = sizeof cli_addr; nbytes = recvfrom(sd, &msg, BTIME_MSGLEN, MSG_WAITALL, (struct sockaddr *)&cli_addr, &cli_addr_len); memset(msg, 0, BTIME_MSGLEN); sprintf(msg, "%u\n", get_btime()); sendto(sd, &msg, BTIME_MSGLEN, MSG_DONTWAIT, (struct sockaddr *)&cli_addr, cli_addr_len); return 0; } /* *--------------------------------------------------------------------------- * * get_btime -- * * Return machine's boot time. * * Returns: * 0 on failure * non-zero on success. * *--------------------------------------------------------------------------- */ static unsigned int get_btime(void) { FILE *statf; char line[1024]; unsigned int btime = 0; if ((statf = fopen("/proc/stat", "r")) == NULL) { syslog(LOG_ERR, "/proc/stat open failure: %s\n", strerror(errno)); exit(1); } while (fgets(line, 1024, statf) != NULL) { if (strstr(line, "btime") != NULL) { sscanf(line, "%*s%u", &btime); } } fclose(statf); return btime; }