]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
add some support for "update" command to boot status daemon
authorRay Strode <rstrode@redhat.com>
Mon, 4 Jun 2007 03:00:41 +0000 (23:00 -0400)
committerRay Strode <rstrode@redhat.com>
Mon, 4 Jun 2007 03:00:41 +0000 (23:00 -0400)
src/ply-boot-client.c
src/ply-boot-client.h
src/ply-boot-server.c

index ebcd76ff8572cbd44a9b471c22b9b477f3863b55..76080a4beb882a9912a26c84efb8715517379cf8 100644 (file)
@@ -24,7 +24,9 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 
@@ -51,7 +53,8 @@ struct _ply_boot_client
 typedef struct
 {
   ply_boot_client_t *client;
-  char *string;
+  char *command;
+  char *argument;
   ply_boot_client_response_handler_t handler;
   ply_boot_client_response_handler_t failed_handler;
   void *user_data;
@@ -60,7 +63,6 @@ typedef struct
 static void ply_boot_client_cancel_request (ply_boot_client_t         *client,
                                             ply_boot_client_request_t *request);
 
-
 ply_boot_client_t *
 ply_boot_client_new (void)
 {
@@ -148,7 +150,8 @@ ply_boot_client_connect (ply_boot_client_t *client,
 
 static ply_boot_client_request_t *
 ply_boot_client_request_new (ply_boot_client_t                  *client,
-                             const char                         *request_string,
+                             const char                         *request_command,
+                             const char                         *request_argument,
                              ply_boot_client_response_handler_t  handler,
                              ply_boot_client_response_handler_t  failed_handler,
                              void                               *user_data)
@@ -156,12 +159,14 @@ ply_boot_client_request_new (ply_boot_client_t                  *client,
   ply_boot_client_request_t *request;
 
   assert (client != NULL);
-  assert (request_string != NULL);
+  assert (request_command != NULL);
   assert (handler != NULL);
 
   request = calloc (1, sizeof (ply_boot_client_request_t));
   request->client = client;
-  request->string = strdup (request_string);
+  request->command = strdup (request_command);
+  if (request_argument != NULL)
+    request->argument = strdup (request_argument);
   request->handler = handler;
   request->failed_handler = failed_handler;
   request->user_data = user_data;
@@ -174,7 +179,7 @@ ply_boot_client_request_free (ply_boot_client_request_t *request)
 {
   if (request == NULL)
     return;
-  free (request->string);
+  free (request->command);
   free (request);
 }
 
@@ -236,16 +241,55 @@ ply_boot_client_process_incoming_replies (ply_boot_client_t *client)
     }
 }
 
+static char *
+ply_boot_client_get_request_string (ply_boot_client_t         *client,
+                                    ply_boot_client_request_t *request,
+                                    size_t                    *request_size)
+{
+  char *request_string;
+
+  assert (client != NULL);
+  assert (request != NULL);
+  assert (request_size != NULL);
+
+  assert (request->command != NULL);
+
+  if (request->argument == NULL)
+    {
+      request_string = strdup (request->command);
+      *request_size = strlen (request_string) + 1;
+      return request_string;
+    }
+
+  assert (strlen (request->argument) <= UCHAR_MAX);
+
+  request_string = NULL;
+  asprintf (&request_string, "%s\002%c%s", request->command, 
+            (char) (strlen (request->argument) + 1), request->argument);
+  *request_size = strlen (request_string) + 1;
+
+  return request_string;
+}
+
 static bool
 ply_boot_client_send_request (ply_boot_client_t         *client,
                               ply_boot_client_request_t *request)
 {
-  if (!ply_write (client->socket_fd, request->string,
-                  strlen (request->string)))
+  char *request_string;
+  size_t request_size;
+
+  assert (client != NULL);
+  assert (request != NULL);
+
+  request_string = ply_boot_client_get_request_string (client, request,
+                                                       &request_size);
+  if (!ply_write (client->socket_fd, request_string, request_size))
     {
+      free (request_string);
       ply_boot_client_cancel_request (client, request);
       return false;
     }
+  free (request_string);
 
   if (client->daemon_has_reply_watch == NULL)
     {
@@ -290,32 +334,36 @@ ply_boot_client_process_pending_requests (ply_boot_client_t *client)
 }
 
 static void
-ply_boot_client_queue_request (ply_boot_client_t                 *client,
-                               const char                        *request_string,
+ply_boot_client_queue_request (ply_boot_client_t                  *client,
+                               const char                         *request_command,
+                               const char                         *request_argument,
                                ply_boot_client_response_handler_t  handler,
                                ply_boot_client_response_handler_t  failed_handler,
-                               void                              *user_data)
+                               void                               *user_data)
 {
   ply_boot_client_request_t *request;
 
   assert (client != NULL);
   assert (client->loop != NULL);
   assert (client->socket_fd >= 0);
-  assert (request_string != NULL);
+  assert (request_command != NULL);
+  assert (request_argument == NULL || strlen (request_argument) <= UCHAR_MAX);
   assert (handler != NULL);
 
   if (client->daemon_can_take_request_watch == NULL)
     {
       assert (ply_list_get_length (client->requests_to_send) == 0);
-      client->daemon_can_take_request_watch = ply_event_loop_watch_fd (client->loop, client->socket_fd,
-                                               PLY_EVENT_LOOP_FD_STATUS_CAN_TAKE_DATA,
-                                               (ply_event_handler_t)
-                                               ply_boot_client_process_pending_requests,
-                                               NULL, client);
+      client->daemon_can_take_request_watch = 
+          ply_event_loop_watch_fd (client->loop, client->socket_fd,
+                                   PLY_EVENT_LOOP_FD_STATUS_CAN_TAKE_DATA,
+                                   (ply_event_handler_t)
+                                   ply_boot_client_process_pending_requests,
+                                   NULL, client);
     }
 
-  request = ply_boot_client_request_new (client, request_string, handler,
-                                         failed_handler, user_data);
+  request = ply_boot_client_request_new (client, request_command,
+                                         request_argument, 
+                                         handler, failed_handler, user_data);
   ply_list_append_data (client->requests_to_send, request);
 }
 
@@ -328,7 +376,20 @@ ply_boot_client_ping_daemon (ply_boot_client_t                  *client,
   assert (client != NULL);
 
   ply_boot_client_queue_request (client, PLY_BOOT_PROTOCOL_REQUEST_TYPE_PING,
-                                 handler, failed_handler, user_data);
+                                 NULL, handler, failed_handler, user_data);
+}
+
+void
+ply_boot_client_update_daemon (ply_boot_client_t                  *client,
+                               const char                         *status,
+                               ply_boot_client_response_handler_t  handler,
+                               ply_boot_client_response_handler_t  failed_handler,
+                               void                               *user_data)
+{
+  assert (client != NULL);
+
+  ply_boot_client_queue_request (client, PLY_BOOT_PROTOCOL_REQUEST_TYPE_UPDATE,
+                                 status, handler, failed_handler, user_data);
 }
 
 void
@@ -393,13 +454,26 @@ static void
 on_pinged (ply_event_loop_t *loop)
 {
   printf ("PING!\n");
+}
+
+static void
+on_ping_failed (ply_event_loop_t *loop)
+{
+  printf ("PING FAILED! %m\n");
+  ply_event_loop_exit (loop, 1);
+}
+
+static void
+on_update (ply_event_loop_t *loop)
+{
+  printf ("UPDATE!\n");
   ply_event_loop_exit (loop, 0);
 }
 
 static void
-on_failed (ply_event_loop_t *loop)
+on_update_failed (ply_event_loop_t *loop)
 {
-  printf ("FAILED! %m\n");
+  printf ("UPDATE FAILED! %m\n");
   ply_event_loop_exit (loop, 1);
 }
 
@@ -435,9 +509,15 @@ main (int    argc,
   ply_boot_client_attach_to_event_loop (client, loop);
   ply_boot_client_ping_daemon (client, 
                                (ply_boot_client_response_handler_t) on_pinged,
-                               (ply_boot_client_response_handler_t) on_failed,
+                               (ply_boot_client_response_handler_t) on_ping_failed,
                                loop);
 
+  ply_boot_client_update_daemon (client, 
+                                 "loading",
+                                 (ply_boot_client_response_handler_t) on_update,
+                                 (ply_boot_client_response_handler_t) on_update_failed,
+                                 loop);
+
   exit_code = ply_event_loop_run (loop);
 
   ply_boot_client_free (client);
index 96d123f1d4518cd601cc8d9344526799cb190b3c..eff20ade09af2da2c957a7a4d7661558f7da8fae 100644 (file)
@@ -46,6 +46,11 @@ void ply_boot_client_ping_daemon (ply_boot_client_t                  *client,
                                   ply_boot_client_response_handler_t  handler,
                                   ply_boot_client_response_handler_t  failed_handler,
                                   void                               *user_data);
+void ply_boot_client_update_daemon (ply_boot_client_t                  *client,
+                                    const char                         *new_status,
+                                    ply_boot_client_response_handler_t  handler,
+                                    ply_boot_client_response_handler_t  failed_handler,
+                                    void                               *user_data);
 
 void ply_boot_client_disconnect (ply_boot_client_t *client);
 void ply_boot_client_attach_to_event_loop (ply_boot_client_t *client,
index f1dc4d26ee11e43d7e0abd5fb35284fd78e8d300..89ba75dcd3f2e01156d8215cb1fc1bcf95a55a88 100644 (file)
@@ -25,6 +25,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <sys/types.h>
@@ -118,20 +119,63 @@ ply_boot_server_stop_listening (ply_boot_server_t *server)
   assert (server != NULL);
 }
 
+static bool
+ply_boot_connection_read_request (ply_boot_connection_t  *connection,
+                                  char                  **command,
+                                  char                  **argument)
+{
+  uint8_t header[2];
+
+  assert (connection != NULL);
+  assert (connection->fd >= 0);
+
+  if (!ply_read (connection->fd, header, sizeof (header)))
+    return false;
+
+  *command = calloc (2, sizeof (char));
+  *command[0] = header[0];
+
+  *argument = NULL;
+  if (header[1] == '\002')
+    {
+      uint8_t argument_size;
+
+      if (!ply_read (connection->fd, &argument_size, sizeof (uint8_t)))
+        return false;
+
+      *argument = calloc (argument_size, sizeof (char));
+
+      if (!ply_read (connection->fd, *argument, argument_size))
+        return false;
+    }
+  return true;
+}
+
 static void
 ply_boot_connection_on_request (ply_boot_connection_t *connection)
 {
-  uint8_t byte;
+  char *command, *argument;
 
   assert (connection != NULL);
   assert (connection->fd >= 0);
 
-  if (read (connection->fd, &byte, sizeof (byte)) != 1)
-    return;
+  if (!ply_boot_connection_read_request (connection,
+                                         &command, &argument))
+    {
+      close (connection->fd);
+      return;
+    }
+
+  if (argument != NULL)
+    printf ("got command '%s' with argument '%s'\n",
+            command, argument);
+  else
+    printf ("got command '%s'\n", command);
 
-  ply_write (connection->fd, 
+  if (!ply_write (connection->fd, 
              PLY_BOOT_PROTOCOL_RESPONSE_TYPE_ACK,
-             strlen (PLY_BOOT_PROTOCOL_RESPONSE_TYPE_ACK));
+             strlen (PLY_BOOT_PROTOCOL_RESPONSE_TYPE_ACK)))
+    close (connection->fd);
 }
 
 static void