#include #include #include #include #include #include #include #include #define SOCKET_PATH "selinux.filename.label" #define MAX_REQUEST_LEN 8192 #define offsetof(st, m) ((size_t)((char *)&((st *)0)->m - (char *)0)) #include clock_t startm, stopm; #define START if ( (startm = clock()) == -1) {printf("Error calling clock");exit(1);} #define STOP if ( (stopm = clock()) == -1) {printf("Error calling clock");exit(1);} #define PRINTTIME printf( "%6.3f seconds used by the processor.\n", ((double)stopm-startm)/CLOCKS_PER_SEC); static int make_request(char *pathname, mode_t mode) { int rc, sockfd; struct sockaddr_un sockaddr_un; ssize_t buflen, len; char buffer[MAX_REQUEST_LEN]; /* put the mode at the front of the buffer */ buflen = snprintf(buffer, sizeof(buffer), "%x %s", mode, pathname); if (buflen < 0) return -1; if (buflen == sizeof(buffer)) { errno = -ENOSPC; return -1; } /* create socket to talk to server */ sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0); if (sockfd < 0) return -1; /* set up address of the server */ sockaddr_un.sun_family = AF_UNIX; sockaddr_un.sun_path[0] = '\0'; memcpy(&sockaddr_un.sun_path[1], SOCKET_PATH, strlen(SOCKET_PATH)); /* this size of addr is really screwy to calculate, there be dragons here */ len = offsetof(struct sockaddr_un, sun_path) + strlen(SOCKET_PATH) + 1; /* connect to server */ rc = connect(sockfd, (const struct sockaddr *)&sockaddr_un, len); if (rc < 0) return -1; /* send the request */ len = send(sockfd, buffer, buflen + 1, 0); if (len <= 0) return -1; /* get the context */ len = recv(sockfd, buffer, MAX_REQUEST_LEN, 0); if (len <= 0) return -1; #ifdef VERBOSE printf("pathname=%s mode=%x context=%s\n", pathname, mode, buffer); #endif return 0; } int main(int argc, char *argv[]) { mode_t mode = 0; int rc, i; if (argc < 2) { printf("usage: %s filename [mode]\n", argv[0]); return 0; } if (argc >= 3) { unsigned long model; errno = 0; /* To distinguish success/failure after call */ model = strtoul(argv[2], NULL, 16); if ((errno == ERANGE && model == ULONG_MAX) || (errno != 0 && model == 0)) { perror("strtol"); exit(EXIT_FAILURE); } mode = (mode_t)model; } START for (i = 0; i < 1000; i++) { rc = make_request(argv[1], mode); if (rc) return -1; } STOP PRINTTIME return 0; }