diff options
Diffstat (limited to 'src/ostree/ostree-trivial-httpd.c')
-rw-r--r-- | src/ostree/ostree-trivial-httpd.c | 337 |
1 files changed, 168 insertions, 169 deletions
diff --git a/src/ostree/ostree-trivial-httpd.c b/src/ostree/ostree-trivial-httpd.c index 855d2cea..facc1527 100644 --- a/src/ostree/ostree-trivial-httpd.c +++ b/src/ostree/ostree-trivial-httpd.c @@ -23,28 +23,31 @@ #include <gio/gunixoutputstream.h> -#include "ot-main.h" -#include "ot-builtins.h" #include "ostree.h" +#include "ot-builtins.h" +#include "ot-main.h" #include "otutil.h" -#include <locale.h> #include <err.h> -#include <sys/socket.h> -#include <sys/prctl.h> +#include <locale.h> #include <signal.h> +#include <sys/prctl.h> +#include <sys/socket.h> -#if ! SOUP_CHECK_VERSION (3, 0, 0) -# define SoupServerMessage SoupMessage -# define soup_server_message_get_method(msg) ((msg)->method) -# define soup_server_message_get_request_headers(msg) ((msg)->request_headers) -# define soup_server_message_get_response_headers(msg) ((msg)->response_headers) -# define soup_server_message_get_response_body(msg) ((msg)->response_body) -# define soup_server_message_set_status(msg, status) soup_message_set_status(msg, status) -# define soup_server_message_set_redirect(msg, status, uri) soup_message_set_redirect(msg, status, uri) -# define soup_server_message_set_response(msg, ct, ru, rb, rl) soup_message_set_response(msg, ct, ru, rb, rl) +#if !SOUP_CHECK_VERSION(3, 0, 0) +#define SoupServerMessage SoupMessage +#define soup_server_message_get_method(msg) ((msg)->method) +#define soup_server_message_get_request_headers(msg) ((msg)->request_headers) +#define soup_server_message_get_response_headers(msg) ((msg)->response_headers) +#define soup_server_message_get_response_body(msg) ((msg)->response_body) +#define soup_server_message_set_status(msg, status) soup_message_set_status (msg, status) +#define soup_server_message_set_redirect(msg, status, uri) \ + soup_message_set_redirect (msg, status, uri) +#define soup_server_message_set_response(msg, ct, ru, rb, rl) \ + soup_message_set_response (msg, ct, ru, rb, rl) #else -# define soup_server_message_set_status(msg, status) soup_server_message_set_status(msg, status, NULL) +#define soup_server_message_set_status(msg, status) \ + soup_server_message_set_status (msg, status, NULL) #endif static char *opt_port_file = NULL; @@ -66,7 +69,8 @@ static gboolean opt_require_basic_auth; static guint emitted_random_500s_count = 0; static guint emitted_random_408s_count = 0; -typedef struct { +typedef struct +{ int root_dfd; gboolean running; GOutputStream *log; @@ -77,30 +81,41 @@ typedef struct { * man page (man/ostree-trivial-httpd.xml) when changing the option list. */ -static GOptionEntry options[] = { - { "daemonize", 'd', 0, G_OPTION_ARG_NONE, &opt_daemonize, "Fork into background when ready", NULL }, - { "autoexit", 0, 0, G_OPTION_ARG_NONE, &opt_autoexit, "Automatically exit when directory is deleted", NULL }, - { "port", 'P', 0, G_OPTION_ARG_INT, &opt_port, "Use the specified TCP port", "PORT" }, - { "port-file", 'p', 0, G_OPTION_ARG_FILENAME, &opt_port_file, "Write port number to PATH (- for standard output)", "PATH" }, - { "force-range-requests", 0, 0, G_OPTION_ARG_NONE, &opt_force_ranges, "Force range requests by only serving half of files", NULL }, - { "require-basic-auth", 0, 0, G_OPTION_ARG_NONE, &opt_require_basic_auth, "Require username foouser, password barpw", NULL }, - { "random-500s", 0, 0, G_OPTION_ARG_INT, &opt_random_500s_percentage, "Generate random HTTP 500 errors approximately for PERCENTAGE requests", "PERCENTAGE" }, - { "random-500s-max", 0, 0, G_OPTION_ARG_INT, &opt_random_500s_max, "Limit HTTP 500 errors to MAX (default 100)", "MAX" }, - { "random-408s", 0, 0, G_OPTION_ARG_INT, &opt_random_408s_percentage, "Generate random HTTP 408 errors approximately for PERCENTAGE requests", "PERCENTAGE" }, - { "random-408s-max", 0, 0, G_OPTION_ARG_INT, &opt_random_408s_max, "Limit HTTP 408 errors to MAX (default 100)", "MAX" }, - { "log-file", 0, 0, G_OPTION_ARG_FILENAME, &opt_log, "Put logs here (use - for stdout)", "PATH" }, - { "expected-cookies", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_expected_cookies, "Expect given cookies in the http request", "KEY=VALUE" }, - { "expected-header", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_expected_headers, "Expect given headers in the http request", "KEY=VALUE" }, - { NULL } -}; - -static void -httpd_log (OtTrivialHttpd *httpd, const gchar *format, ...) __attribute__ ((format(printf, 2, 3))); +static GOptionEntry options[] + = { { "daemonize", 'd', 0, G_OPTION_ARG_NONE, &opt_daemonize, "Fork into background when ready", + NULL }, + { "autoexit", 0, 0, G_OPTION_ARG_NONE, &opt_autoexit, + "Automatically exit when directory is deleted", NULL }, + { "port", 'P', 0, G_OPTION_ARG_INT, &opt_port, "Use the specified TCP port", "PORT" }, + { "port-file", 'p', 0, G_OPTION_ARG_FILENAME, &opt_port_file, + "Write port number to PATH (- for standard output)", "PATH" }, + { "force-range-requests", 0, 0, G_OPTION_ARG_NONE, &opt_force_ranges, + "Force range requests by only serving half of files", NULL }, + { "require-basic-auth", 0, 0, G_OPTION_ARG_NONE, &opt_require_basic_auth, + "Require username foouser, password barpw", NULL }, + { "random-500s", 0, 0, G_OPTION_ARG_INT, &opt_random_500s_percentage, + "Generate random HTTP 500 errors approximately for PERCENTAGE requests", "PERCENTAGE" }, + { "random-500s-max", 0, 0, G_OPTION_ARG_INT, &opt_random_500s_max, + "Limit HTTP 500 errors to MAX (default 100)", "MAX" }, + { "random-408s", 0, 0, G_OPTION_ARG_INT, &opt_random_408s_percentage, + "Generate random HTTP 408 errors approximately for PERCENTAGE requests", "PERCENTAGE" }, + { "random-408s-max", 0, 0, G_OPTION_ARG_INT, &opt_random_408s_max, + "Limit HTTP 408 errors to MAX (default 100)", "MAX" }, + { "log-file", 0, 0, G_OPTION_ARG_FILENAME, &opt_log, "Put logs here (use - for stdout)", + "PATH" }, + { "expected-cookies", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_expected_cookies, + "Expect given cookies in the http request", "KEY=VALUE" }, + { "expected-header", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_expected_headers, + "Expect given headers in the http request", "KEY=VALUE" }, + { NULL } }; + +static void httpd_log (OtTrivialHttpd *httpd, const gchar *format, ...) + __attribute__ ((format (printf, 2, 3))); static void httpd_log (OtTrivialHttpd *httpd, const gchar *format, ...) { - g_autoptr(GString) str = NULL; + g_autoptr (GString) str = NULL; va_list args; gsize written; @@ -108,7 +123,7 @@ httpd_log (OtTrivialHttpd *httpd, const gchar *format, ...) return; { - g_autoptr(GDateTime) now = g_date_time_new_now_local (); + g_autoptr (GDateTime) now = g_date_time_new_now_local (); g_autofree char *timestamp = g_date_time_format (now, "%F %T"); str = g_string_new (timestamp); g_string_append_printf (str, ".%06d - ", g_date_time_get_microsecond (now)); @@ -131,12 +146,13 @@ compare_strings (gconstpointer a, gconstpointer b) } static GString * -get_directory_listing (int dfd, - const char *path) +get_directory_listing (int dfd, const char *path) { - g_autoptr(GPtrArray) entries = g_ptr_array_new_with_free_func (g_free); - g_auto(GLnxDirFdIterator) dfd_iter = { 0, }; - g_autoptr(GError) local_error = NULL; + g_autoptr (GPtrArray) entries = g_ptr_array_new_with_free_func (g_free); + g_auto (GLnxDirFdIterator) dfd_iter = { + 0, + }; + g_autoptr (GError) local_error = NULL; GError **error = &local_error; guint i; char *escaped; @@ -169,13 +185,12 @@ get_directory_listing (int dfd, g_free (escaped); for (i = 0; i < entries->len; i++) { - g_string_append_printf (listing, "<a href=\"%s\">%s</a><br>\r\n", - (char *)entries->pdata[i], + g_string_append_printf (listing, "<a href=\"%s\">%s</a><br>\r\n", (char *)entries->pdata[i], (char *)entries->pdata[i]); g_free (g_steal_pointer (&entries->pdata[i])); } g_string_append (listing, "</body>\r\n</html>\r\n"); - out: +out: if (local_error) g_printerr ("%s\n", local_error->message); return listing; @@ -218,7 +233,7 @@ close_socket (SoupServerMessage *msg, gpointer user_data) static gchar * calculate_etag (GMappedFile *mapping) { - g_autoptr(GBytes) bytes = g_mapped_file_get_bytes (mapping); + g_autoptr (GBytes) bytes = g_mapped_file_get_bytes (mapping); g_autofree gchar *checksum = g_compute_checksum_for_bytes (G_CHECKSUM_SHA256, bytes); return g_strconcat ("\"", checksum, "\"", NULL); } @@ -234,12 +249,11 @@ _server_cookies_from_request (SoupServerMessage *msg) const char *header; const char *host; - header = soup_message_headers_get_one (soup_server_message_get_request_headers (msg), - "Cookie"); + header = soup_message_headers_get_one (soup_server_message_get_request_headers (msg), "Cookie"); if (!header) return NULL; -#if ! SOUP_CHECK_VERSION (3, 0, 0) +#if !SOUP_CHECK_VERSION(3, 0, 0) host = soup_uri_get_host (soup_message_get_uri (msg)); #else host = g_uri_get_host (soup_server_message_get_uri (msg)); @@ -249,7 +263,8 @@ _server_cookies_from_request (SoupServerMessage *msg) while (g_hash_table_iter_next (&iter, &name, &value)) { - if (!name || !value) continue; + if (!name || !value) + continue; cookie = soup_cookie_new (name, value, host, NULL, 0); cookies = g_slist_prepend (cookies, cookie); } @@ -260,17 +275,11 @@ _server_cookies_from_request (SoupServerMessage *msg) } static void -#if ! SOUP_CHECK_VERSION (3, 0, 0) -do_get (OtTrivialHttpd *self, - SoupServer *server, - SoupServerMessage *msg, - const char *path, +#if !SOUP_CHECK_VERSION(3, 0, 0) +do_get (OtTrivialHttpd *self, SoupServer *server, SoupServerMessage *msg, const char *path, SoupClientContext *context) #else -do_get (OtTrivialHttpd *self, - SoupServer *server, - SoupServerMessage *msg, - const char *path) +do_get (OtTrivialHttpd *self, SoupServer *server, SoupServerMessage *msg, const char *path) #endif { char *slash; @@ -285,18 +294,18 @@ do_get (OtTrivialHttpd *self, GSList *l; int i; - for (i = 0 ; opt_expected_cookies[i] != NULL; i++) + for (i = 0; opt_expected_cookies[i] != NULL; i++) { gboolean found = FALSE; gchar *k = opt_expected_cookies[i]; gchar *v = strchr (k, '=') + 1; - for (l = cookies; l != NULL ; l = g_slist_next (l)) + for (l = cookies; l != NULL; l = g_slist_next (l)) { SoupCookie *c = l->data; - if (!strncmp (k, soup_cookie_get_name (c), v - k - 1) && - !strcmp (v, soup_cookie_get_value (c))) + if (!strncmp (k, soup_cookie_get_name (c), v - k - 1) + && !strcmp (v, soup_cookie_get_value (c))) { found = TRUE; break; @@ -316,7 +325,7 @@ do_get (OtTrivialHttpd *self, if (opt_expected_headers) { - for (int i = 0 ; opt_expected_headers[i] != NULL; i++) + for (int i = 0; opt_expected_headers[i] != NULL; i++) { const gchar *kv = opt_expected_headers[i]; const gchar *eq = strchr (kv, '='); @@ -326,7 +335,8 @@ do_get (OtTrivialHttpd *self, { g_autofree char *k = g_strndup (kv, eq - kv); const gchar *expected_v = eq + 1; - const gchar *found_v = soup_message_headers_get_one (soup_server_message_get_request_headers (msg), k); + const gchar *found_v + = soup_message_headers_get_one (soup_server_message_get_request_headers (msg), k); if (!found_v) { @@ -350,17 +360,15 @@ do_get (OtTrivialHttpd *self, goto out; } - if (opt_random_500s_percentage > 0 && - emitted_random_500s_count < opt_random_500s_max && - g_random_int_range (0, 100) < opt_random_500s_percentage) + if (opt_random_500s_percentage > 0 && emitted_random_500s_count < opt_random_500s_max + && g_random_int_range (0, 100) < opt_random_500s_percentage) { emitted_random_500s_count++; soup_server_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); goto out; } - else if (opt_random_408s_percentage > 0 && - emitted_random_408s_count < opt_random_408s_max && - g_random_int_range (0, 100) < opt_random_408s_percentage) + else if (opt_random_408s_percentage > 0 && emitted_random_408s_count < opt_random_408s_max + && g_random_int_range (0, 100) < opt_random_408s_percentage) { emitted_random_408s_count++; soup_server_message_set_status (msg, SOUP_STATUS_REQUEST_TIMEOUT); @@ -397,13 +405,12 @@ do_get (OtTrivialHttpd *self, { g_autofree char *redir_uri = NULL; -#if ! SOUP_CHECK_VERSION (3, 0, 0) +#if !SOUP_CHECK_VERSION(3, 0, 0) redir_uri = g_strdup_printf ("%s/", soup_uri_get_path (soup_message_get_uri (msg))); #else redir_uri = g_strdup_printf ("%s/", g_uri_get_path (soup_server_message_get_uri (msg))); #endif - soup_server_message_set_redirect (msg, SOUP_STATUS_MOVED_PERMANENTLY, - redir_uri); + soup_server_message_set_redirect (msg, SOUP_STATUS_MOVED_PERMANENTLY, redir_uri); } else { @@ -411,7 +418,7 @@ do_get (OtTrivialHttpd *self, if (fstatat (self->root_dfd, index_realpath, &stbuf, 0) != -1) { g_autofree char *index_path = g_strconcat (path, "/index.html", NULL); -#if ! SOUP_CHECK_VERSION (3, 0, 0) +#if !SOUP_CHECK_VERSION(3, 0, 0) do_get (self, server, msg, index_path, context); #else do_get (self, server, msg, index_path); @@ -420,9 +427,8 @@ do_get (OtTrivialHttpd *self, else { GString *listing = get_directory_listing (self->root_dfd, path); - soup_server_message_set_response (msg, "text/html", - SOUP_MEMORY_TAKE, - listing->str, listing->len); + soup_server_message_set_response (msg, "text/html", SOUP_MEMORY_TAKE, listing->str, + listing->len); soup_server_message_set_status (msg, SOUP_STATUS_OK); g_string_free (listing, FALSE); } @@ -443,20 +449,23 @@ do_get (OtTrivialHttpd *self, goto out; } - g_autoptr(GMappedFile) mapping = g_mapped_file_new_from_fd (fd, FALSE, NULL); + g_autoptr (GMappedFile) mapping = g_mapped_file_new_from_fd (fd, FALSE, NULL); if (!mapping) { soup_server_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); goto out; } - (void) close (fd); fd = -1; + (void)close (fd); + fd = -1; /* Send caching headers */ - g_autoptr(GDateTime) last_modified = g_date_time_new_from_unix_utc (stbuf.st_mtim.tv_sec); + g_autoptr (GDateTime) last_modified = g_date_time_new_from_unix_utc (stbuf.st_mtim.tv_sec); if (last_modified != NULL) { - g_autofree gchar *formatted = g_date_time_format (last_modified, "%a, %d %b %Y %H:%M:%S GMT"); - soup_message_headers_append (soup_server_message_get_response_headers (msg), "Last-Modified", formatted); + g_autofree gchar *formatted + = g_date_time_format (last_modified, "%a, %d %b %Y %H:%M:%S GMT"); + soup_message_headers_append (soup_server_message_get_response_headers (msg), + "Last-Modified", formatted); } g_autofree gchar *etag = calculate_etag (mapping); @@ -471,12 +480,14 @@ do_get (OtTrivialHttpd *self, gboolean have_ranges; file_size = g_mapped_file_get_length (mapping); - have_ranges = soup_message_headers_get_ranges(soup_server_message_get_request_headers (msg), file_size, &ranges, &ranges_length); + have_ranges = soup_message_headers_get_ranges ( + soup_server_message_get_request_headers (msg), file_size, &ranges, &ranges_length); if (opt_force_ranges && !have_ranges && g_strrstr (path, "/objects") != NULL) { GSocket *sock; - buffer_length = file_size/2; - soup_message_headers_set_content_length (soup_server_message_get_response_headers (msg), file_size); + buffer_length = file_size / 2; + soup_message_headers_set_content_length ( + soup_server_message_get_response_headers (msg), file_size); soup_message_headers_append (soup_server_message_get_response_headers (msg), "Connection", "close"); @@ -485,7 +496,7 @@ do_get (OtTrivialHttpd *self, * the declared Content-Length. Instead, we * forcibly close the socket at that point. */ -#if ! SOUP_CHECK_VERSION (3, 0, 0) +#if !SOUP_CHECK_VERSION(3, 0, 0) sock = soup_client_context_get_gsocket (context); #else sock = soup_server_message_get_socket (msg); @@ -500,19 +511,20 @@ do_get (OtTrivialHttpd *self, if (ranges_length > 0 && ranges[0].start >= file_size) { soup_server_message_set_status (msg, SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE); - soup_message_headers_free_ranges (soup_server_message_get_request_headers (msg), ranges); + soup_message_headers_free_ranges (soup_server_message_get_request_headers (msg), + ranges); goto out; } - soup_message_headers_free_ranges (soup_server_message_get_request_headers (msg), ranges); + soup_message_headers_free_ranges (soup_server_message_get_request_headers (msg), + ranges); } -#if ! SOUP_CHECK_VERSION (3, 0, 0) +#if !SOUP_CHECK_VERSION(3, 0, 0) if (buffer_length > 0) { SoupBuffer *buffer; buffer = soup_buffer_new_with_owner (g_mapped_file_get_contents (mapping), - buffer_length, - g_mapped_file_ref (mapping), + buffer_length, g_mapped_file_ref (mapping), (GDestroyNotify)g_mapped_file_unref); soup_message_body_append_buffer (msg->response_body, buffer); soup_buffer_free (buffer); @@ -546,10 +558,10 @@ do_get (OtTrivialHttpd *self, } /* Check client’s caching headers. */ - const gchar *if_modified_since = soup_message_headers_get_one (soup_server_message_get_request_headers (msg), - "If-Modified-Since"); - const gchar *if_none_match = soup_message_headers_get_one (soup_server_message_get_request_headers (msg), - "If-None-Match"); + const gchar *if_modified_since = soup_message_headers_get_one ( + soup_server_message_get_request_headers (msg), "If-Modified-Since"); + const gchar *if_none_match = soup_message_headers_get_one ( + soup_server_message_get_request_headers (msg), "If-None-Match"); if (if_none_match != NULL && etag != NULL) { @@ -565,18 +577,19 @@ do_get (OtTrivialHttpd *self, } else if (if_modified_since != NULL && last_modified != NULL) { - g_autoptr(GDateTime) if_modified_since_dt = NULL; -#if ! SOUP_CHECK_VERSION (3, 0, 0) + g_autoptr (GDateTime) if_modified_since_dt = NULL; +#if !SOUP_CHECK_VERSION(3, 0, 0) SoupDate *if_modified_since_sd = soup_date_new_from_string (if_modified_since); if (if_modified_since_sd != NULL) - if_modified_since_dt = g_date_time_new_from_unix_utc (soup_date_to_time_t (if_modified_since_sd)); + if_modified_since_dt + = g_date_time_new_from_unix_utc (soup_date_to_time_t (if_modified_since_sd)); #else if_modified_since_dt = soup_date_time_new_from_http_string (if_modified_since); #endif - if (if_modified_since_dt != NULL && - g_date_time_compare (last_modified, if_modified_since_dt) <= 0) + if (if_modified_since_dt != NULL + && g_date_time_compare (last_modified, if_modified_since_dt) <= 0) { soup_server_message_set_status (msg, SOUP_STATUS_NOT_MODIFIED); soup_message_body_truncate (soup_server_message_get_response_body (msg)); @@ -591,16 +604,13 @@ do_get (OtTrivialHttpd *self, soup_server_message_set_status (msg, SOUP_STATUS_OK); } } - out: +out: { -#if ! SOUP_CHECK_VERSION (3, 0, 0) +#if !SOUP_CHECK_VERSION(3, 0, 0) guint status = 0; g_autofree gchar *reason = NULL; - g_object_get (msg, - "status-code", &status, - "reason-phrase", &reason, - NULL); + g_object_get (msg, "status-code", &status, "reason-phrase", &reason, NULL); #else guint status = soup_server_message_get_status (msg); const char *reason = soup_server_message_get_reason_phrase (msg); @@ -612,20 +622,19 @@ do_get (OtTrivialHttpd *self, } static void -#if ! SOUP_CHECK_VERSION (3, 0, 0) -httpd_callback (SoupServer *server, SoupServerMessage *msg, - const char *path, GHashTable *query, +#if !SOUP_CHECK_VERSION(3, 0, 0) +httpd_callback (SoupServer *server, SoupServerMessage *msg, const char *path, GHashTable *query, SoupClientContext *context, gpointer data) #else -httpd_callback (SoupServer *server, SoupServerMessage *msg, - const char *path, GHashTable *query, gpointer data) +httpd_callback (SoupServer *server, SoupServerMessage *msg, const char *path, GHashTable *query, + gpointer data) #endif { OtTrivialHttpd *self = data; const char *meth = soup_server_message_get_method (msg); - if (!strcmp (meth, "GET") || !strcmp(meth, "HEAD")) -#if ! SOUP_CHECK_VERSION (3, 0, 0) + if (!strcmp (meth, "GET") || !strcmp (meth, "HEAD")) +#if !SOUP_CHECK_VERSION(3, 0, 0) do_get (self, server, msg, path, context); #else do_get (self, server, msg, path); @@ -635,17 +644,14 @@ httpd_callback (SoupServer *server, SoupServerMessage *msg, } static gboolean -basic_auth_callback (SoupAuthDomain *auth_domain, SoupServerMessage *msg, - const char *username, const char *password, gpointer data) +basic_auth_callback (SoupAuthDomain *auth_domain, SoupServerMessage *msg, const char *username, + const char *password, gpointer data) { - return g_str_equal (username, "foouser") && g_str_equal (password, "barpw"); + return g_str_equal (username, "foouser") && g_str_equal (password, "barpw"); } static void -on_dir_changed (GFileMonitor *mon, - GFile *file, - GFile *other, - GFileMonitorEvent event, +on_dir_changed (GFileMonitor *mon, GFile *file, GFile *other, GFileMonitorEvent event, gpointer user_data) { OtTrivialHttpd *self = user_data; @@ -662,13 +668,15 @@ static gboolean run (int argc, char **argv, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; - g_autoptr(GOptionContext) context = NULL; + g_autoptr (GOptionContext) context = NULL; const char *dirpath; - OtTrivialHttpd appstruct = { 0, }; + OtTrivialHttpd appstruct = { + 0, + }; OtTrivialHttpd *app = &appstruct; int pipefd[2] = { -1, -1 }; glnx_unref_object SoupServer *server = NULL; - g_autoptr(GFileMonitor) dirmon = NULL; + g_autoptr (GFileMonitor) dirmon = NULL; context = g_option_context_new ("[DIR] - Simple webserver"); g_option_context_add_main_entries (context, options, NULL); @@ -688,21 +696,22 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error) if (!(opt_random_500s_percentage >= 0 && opt_random_500s_percentage <= 99)) { - g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, - "Invalid --random-500s=%u", opt_random_500s_percentage); + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid --random-500s=%u", + opt_random_500s_percentage); goto out; } if (!(opt_random_408s_percentage >= 0 && opt_random_408s_percentage <= 99)) { - g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, - "Invalid --random-408s=%u", opt_random_408s_percentage); + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid --random-408s=%u", + opt_random_408s_percentage); goto out; } if (opt_daemonize && (g_strcmp0 (opt_log, "-") == 0)) { - ot_util_usage_error (context, "Cannot use --log-file=- and --daemonize at the same time", error); + ot_util_usage_error (context, "Cannot use --log-file=- and --daemonize at the same time", + error); goto out; } @@ -719,7 +728,7 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error) goto out; } - pid_t pid = fork(); + pid_t pid = fork (); if (pid == -1) { glnx_set_error_from_errno (error); @@ -784,14 +793,11 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error) } else { - g_autoptr(GFile) log_file = NULL; - GFileOutputStream* log_stream; + g_autoptr (GFile) log_file = NULL; + GFileOutputStream *log_stream; log_file = g_file_new_for_path (opt_log); - log_stream = g_file_create (log_file, - G_FILE_CREATE_PRIVATE, - cancellable, - error); + log_stream = g_file_create (log_file, G_FILE_CREATE_PRIVATE, cancellable, error); if (!log_stream) goto out; stream = G_OUTPUT_STREAM (log_stream); @@ -805,22 +811,19 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error) if (!soup_server_listen_all (server, opt_port, 0, error)) goto out; #else - server = soup_server_new (SOUP_SERVER_PORT, opt_port, - SOUP_SERVER_SERVER_HEADER, "ostree-httpd ", + server = soup_server_new (SOUP_SERVER_PORT, opt_port, SOUP_SERVER_SERVER_HEADER, "ostree-httpd ", NULL); #endif if (opt_require_basic_auth) { -#if ! SOUP_CHECK_VERSION (3, 0, 0) - glnx_unref_object SoupAuthDomain *auth_domain = - soup_auth_domain_basic_new (SOUP_AUTH_DOMAIN_REALM, "auth-test", - SOUP_AUTH_DOMAIN_ADD_PATH, "/", - SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK, basic_auth_callback, - NULL); +#if !SOUP_CHECK_VERSION(3, 0, 0) + glnx_unref_object SoupAuthDomain *auth_domain = soup_auth_domain_basic_new ( + SOUP_AUTH_DOMAIN_REALM, "auth-test", SOUP_AUTH_DOMAIN_ADD_PATH, "/", + SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK, basic_auth_callback, NULL); #else - glnx_unref_object SoupAuthDomain *auth_domain = - soup_auth_domain_basic_new ("realm", "auth-test", NULL); + glnx_unref_object SoupAuthDomain *auth_domain + = soup_auth_domain_basic_new ("realm", "auth-test", NULL); soup_auth_domain_add_path (auth_domain, "/"); soup_auth_domain_basic_set_auth_callback (auth_domain, basic_auth_callback, NULL, NULL); #endif @@ -833,8 +836,8 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error) g_autofree char *portstr = NULL; #if SOUP_CHECK_VERSION(2, 48, 0) GSList *listeners = soup_server_get_listeners (server); - g_autoptr(GSocket) listener = NULL; - g_autoptr(GSocketAddress) addr = NULL; + g_autoptr (GSocket) listener = NULL; + g_autoptr (GSocketAddress) addr = NULL; g_assert (listeners); listener = g_object_ref (listeners->data); @@ -846,7 +849,8 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error) g_assert (G_IS_INET_SOCKET_ADDRESS (addr)); - portstr = g_strdup_printf ("%u\n", g_inet_socket_address_get_port ((GInetSocketAddress*)addr)); + portstr + = g_strdup_printf ("%u\n", g_inet_socket_address_get_port ((GInetSocketAddress *)addr)); #else portstr = g_strdup_printf ("%u\n", soup_server_get_port (server)); #endif @@ -881,17 +885,17 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error) goto out; } /* Daemonising: close stdout/stderr so $() et al work on us */ - if (freopen("/dev/null", "r", stdin) == NULL) + if (freopen ("/dev/null", "r", stdin) == NULL) { glnx_set_prefix_error_from_errno (error, "%s", "freopen: "); goto out; } - if (freopen("/dev/null", "w", stdout) == NULL) + if (freopen ("/dev/null", "w", stdout) == NULL) { glnx_set_prefix_error_from_errno (error, "%s", "freopen: "); goto out; } - if (freopen("/dev/null", "w", stderr) == NULL) + if (freopen ("/dev/null", "w", stderr) == NULL) { glnx_set_prefix_error_from_errno (error, "%s", "freopen: "); goto out; @@ -902,14 +906,12 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error) if (opt_autoexit) { gboolean is_symlink = FALSE; - g_autoptr(GFile) root = NULL; - g_autoptr(GFileInfo) info = NULL; + g_autoptr (GFile) root = NULL; + g_autoptr (GFileInfo) info = NULL; root = g_file_new_for_path (dirpath); - info = g_file_query_info (root, - G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK, - G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, - cancellable, error); + info = g_file_query_info (root, G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, cancellable, error); if (!info) goto out; @@ -929,7 +931,7 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error) g_main_context_iteration (NULL, TRUE); ret = TRUE; - out: +out: if (pipefd[0] >= 0) { /* Read end in the parent. This should only be open on errors. */ @@ -942,21 +944,20 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error) g_assert_false (ret); guint8 buf = 1; g_debug ("Writing %u to parent", buf); - (void) TEMP_FAILURE_RETRY (write (pipefd[1], &buf, 1)); + (void)TEMP_FAILURE_RETRY (write (pipefd[1], &buf, 1)); glnx_close_fd (&pipefd[1]); } if (app->root_dfd != -1) - (void) close (app->root_dfd); + (void)close (app->root_dfd); g_clear_object (&app->log); return ret; } int -main (int argc, - char **argv) +main (int argc, char **argv) { - g_autoptr(GError) error = NULL; - g_autoptr(GCancellable) cancellable = NULL; + g_autoptr (GError) error = NULL; + g_autoptr (GCancellable) cancellable = NULL; setlocale (LC_ALL, ""); @@ -964,10 +965,8 @@ main (int argc, if (!run (argc, argv, cancellable, &error)) { - g_printerr ("%s%serror:%s%s %s\n", - ot_get_red_start (), ot_get_bold_start (), - ot_get_bold_end (), ot_get_red_end (), - error->message); + g_printerr ("%s%serror:%s%s %s\n", ot_get_red_start (), ot_get_bold_start (), + ot_get_bold_end (), ot_get_red_end (), error->message); return 1; } |