| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <fcntl.h> |
| 8 |
#include <string.h> |
| 9 |
#include <sys/sendfile.h> |
| 10 |
#include <sys/stat.h> |
| 11 |
#include <sys/types.h> |
| 12 |
#include <unistd.h> |
| 13 |
|
| 14 |
#include "server.h" |
| 15 |
|
| 16 |
/* HTML source for the start of the page we generate. */ |
| 17 |
|
| 18 |
static char* page_start = |
| 19 |
"<html>\n" |
| 20 |
" <body>\n" |
| 21 |
" <pre>\n"; |
| 22 |
|
| 23 |
/* HTML source for the end of the page we generate. */ |
| 24 |
|
| 25 |
static char* page_end = |
| 26 |
" </pre>\n" |
| 27 |
" </body>\n" |
| 28 |
"</html>\n"; |
| 29 |
|
| 30 |
/* HTML source for the page indicating there was a problem opening |
| 31 |
/proc/issue. */ |
| 32 |
|
| 33 |
static char* error_page = |
| 34 |
"<html>\n" |
| 35 |
" <body>\n" |
| 36 |
" <p>Error: Could not open /proc/issue.</p>\n" |
| 37 |
" </body>\n" |
| 38 |
"</html>\n"; |
| 39 |
|
| 40 |
/* HTML source indicating an error. */ |
| 41 |
|
| 42 |
static char* error_message = "Error reading /proc/issue."; |
| 43 |
|
| 44 |
void module_generate (int fd) |
| 45 |
{ |
| 46 |
int input_fd; |
| 47 |
struct stat file_info; |
| 48 |
int rval; |
| 49 |
|
| 50 |
/* Open /etc/issue. */ |
| 51 |
input_fd = open ("/etc/issue", O_RDONLY); |
| 52 |
if (input_fd == -1) |
| 53 |
system_error ("open"); |
| 54 |
/* Obtain file information about it. */ |
| 55 |
rval = fstat (input_fd, &file_info); |
| 56 |
|
| 57 |
if (rval == -1) |
| 58 |
/* Either we couldn't open the file or we couldn't read from it. */ |
| 59 |
write (fd, error_page, strlen (error_page)); |
| 60 |
else { |
| 61 |
int rval; |
| 62 |
off_t offset = 0; |
| 63 |
|
| 64 |
/* Write the start of the page. */ |
| 65 |
write (fd, page_start, strlen (page_start)); |
| 66 |
/* Copy from /proc/issue to the client socket. */ |
| 67 |
rval = sendfile (fd, input_fd, &offset, file_info.st_size); |
| 68 |
if (rval == -1) |
| 69 |
/* Something went wrong sending the contents of /proc/issue. |
| 70 |
Write an error message. */ |
| 71 |
write (fd, error_message, strlen (error_message)); |
| 72 |
/* End the page. */ |
| 73 |
write (fd, page_end, strlen (page_end)); |
| 74 |
} |
| 75 |
|
| 76 |
close (input_fd); |
| 77 |
} |