Guest User

Untitled

a guest
Apr 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. static void service_shm(int segment_id) {
  2. char *file;
  3. char *response;
  4. int fd, len;
  5. struct stat sb;
  6. shm_thread_t *shm = &shm_thread[segment_id];
  7.  
  8.  
  9. pthread_cond_wait(&shm->cond, &shm->mutex);
  10. file = strdup(&shm->data[1]);
  11.  
  12. strdecode(file, file);
  13. if (file[0] == '\0')
  14. file = "./";
  15. len = strlen(file);
  16. if (file[0] == '/' || strcmp(file, "..") == 0 || strncmp(file, "../", 3) == 0 || strstr(file, "/../") != (char *) 0 || strcmp(&(file[len - 3]), "/..") == 0) {
  17. free(file);
  18. response = make_error(400, "Bad Request", (char *) 0, "Illegal filename.");
  19. write_shm(segment_id, response, strlen(response));
  20. free(response);
  21. close_shm(segment_id);
  22. return;
  23. }
  24. if (stat(file, &sb) < 0) {
  25. free(file);
  26. response = make_error(404, "Not Found", (char *) 0, "File not found.");
  27. write_shm(segment_id, response, strlen(response));
  28. free(response);
  29. close_shm(segment_id);
  30. return;
  31. }
  32. fd = open(file, O_RDONLY, 0);
  33. if (fd < 0) {
  34. free(file);
  35. response = make_error(403, "Forbidden", (char *) 0, "File is protected.");
  36. write_shm(segment_id, response, strlen(response));
  37. free(response);
  38. close_shm(segment_id);
  39. return;
  40. }
  41.  
  42. free(file);
  43. response = make_headers(200, "Ok", (char *) 0, get_mime_type(file), sb.st_size, sb.st_mtime);
  44. write_shm(segment_id, response, strlen(response));
  45. free(response);
  46.  
  47. do {
  48. len = read(fd, shm->data, SHM_BUF_SIZE);
  49. shm->data_size = len;
  50. pthread_cond_signal(&shm->cond);
  51. pthread_cond_wait(&shm->cond, &shm->mutex);
  52. } while (len > 0);
  53.  
  54. close(fd);
  55. close_shm(segment_id);
  56.  
  57. }
  58.  
  59. static void write_shm(int segment_id, char *data, int len) {
  60. shm_thread_t *shm = &shm_thread[segment_id];
  61. shm->data_size = len < SHM_BUF_SIZE ? len : SHM_BUF_SIZE;
  62. memcpy(shm->data, data, shm->data_size);
  63. pthread_cond_signal(&shm->cond);
  64. pthread_cond_wait(&shm->cond, &shm->mutex);
  65. }
  66.  
  67. static void close_shm(int segment_id) {
  68. shm_thread_t *shm = &shm_thread[segment_id];
  69. shm->data_size = 0;
  70. shm->done = 1;
  71. pthread_cond_signal(&shm->cond);
  72. pthread_cond_wait(&shm->cond, &shm->mutex);
  73. }
Add Comment
Please, Sign In to add comment