RM Server can be initialized with a group ID for non-root user access to server's...
authorJustin Sobota <jsobota@ti.com>
Tue, 19 May 2015 20:09:06 +0000 (16:09 -0400)
committerJustin Sobota <jsobota@ti.com>
Tue, 19 May 2015 20:09:06 +0000 (16:09 -0400)
test/armv7/linux/rm_dsp_client_test.c
test/armv7/linux/rm_linux_client_test.c
test/armv7/linux/rm_linux_mt_client_test.c
test/armv7/linux/rm_server.c
test/armv7/linux/sockrmmsg.h
test/armv7/linux/sockutils.c
test/armv7/linux/sockutils.h

index 92253d12e37aa794733cb63f2640d28154cb6ced..d45ab906fd099ef7a29b194cafd558a5212e0f4b 100644 (file)
@@ -85,8 +85,8 @@ Int rmServerExchange_execute(UInt16 procId)
     sock_name_t         server_sock;
     sock_name_t         server_sock_addr;
     struct sockaddr_un  server_addr;
-    char                client_ex_sock_name[] = "/tmp/var/run/rm/rm_dsp_client_exchange";
-    char                server_sock_name[] = RM_SERVER_SOCKET_NAME;    
+    char                client_ex_sock_name[] = "/var/run/rm/rm_dsp_client_exchange";
+    char                server_sock_name[] = RM_SERVER_SOCKET_NAME;
 
     printf("Entered rmServerExchange_execute\n");
  
index b3d658867912521ce2e43be5bd4285f3f6bd71e7..d28ef71ec0325e67b4be8c33f5043550b99f22b2 100644 (file)
@@ -255,7 +255,7 @@ uint16_t            testErrors;
 char                rmClientName[RM_NAME_MAX_CHARS] = "RM_Client";
 
 /* Client socket name */
-char                rmClientSockName[] = "/tmp/var/run/rm/rm_client";
+char                rmClientSockName[] = "/var/run/rm/rm_client";
 
 /* Client socket handle */
 sock_h              rmClientSocket;
index 47b34d281132ea8b3f73c7833ad9bb8fa659d4ae..d40e54fa65fde97a985ffcc344e75be0b90b3b27 100644 (file)
@@ -126,7 +126,7 @@ Rm_ServiceHandle  *service_handle = NULL;
 pthread_mutex_t    client_mutex_lock;
 
 /* Client socket name */
-char               client_sock_name[] = "/tmp/var/run/rm/rm_client";
+char               client_sock_name[] = "/var/run/rm/rm_client";
 
 /* Client socket handles */
 sock_h             client_sock;
index e1f1f8a1b649eabaa24e8edccc23e539f2a0e1ca..ccbe08be7fe870c0e936282c55d32368ca50fb50 100644 (file)
 #include <string.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <signal.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <unistd.h>
+#include <grp.h>
 
 #include <libdaemon/daemon.h>
 
@@ -203,7 +205,8 @@ int32_t transportSend (Rm_AppTransportHandle appTransport, Rm_PacketHandle pktHa
     return (0);
 }
 
-int rm_server_run(void *grl, void *policy, void *lin_dtb, int is_daemon)
+int rm_server_run(void *grl, void *policy, void *lin_dtb, int is_daemon,
+                  char *group_name)
 {
     Rm_InitCfg          rm_init_cfg;
     Rm_TransportCfg     rm_trans_cfg;
@@ -214,6 +217,9 @@ int rm_server_run(void *grl, void *policy, void *lin_dtb, int is_daemon)
     int                 retval;
     int                 length = 0;
     sock_name_t         serv_sock_name;
+    struct group       *group_data;
+    int                 group_valid = 0;
+    gid_t               serv_group_id = 0;
     sock_name_t         client_sock_addr;
     Rm_Packet          *rm_pkt = NULL;
     char                pkt_src[RM_NAME_MAX_CHARS];
@@ -226,7 +232,7 @@ int rm_server_run(void *grl, void *policy, void *lin_dtb, int is_daemon)
         printf("Error in opening log file %s (%s)", RMSERVER_DAEMON_LOG_FILE_NAME, strerror(errno));
     }
 
-    debug_msg("Starting RM server");
+    debug_msg("Starting RM server\n");
     
     /* Create the Server instance */
     memset(&rm_init_cfg, 0, sizeof(rm_init_cfg));
@@ -238,15 +244,35 @@ int rm_server_run(void *grl, void *policy, void *lin_dtb, int is_daemon)
     server_h = Rm_init(&rm_init_cfg, &rm_result);
     ERROR_CHECK(RM_OK, rm_result, server_name, "Initialization failed\n");
 
-    debug_msg("RM Server initialized with name: %s", server_name);
+    debug_msg("RM Server initialized with name: %s\n", server_name);
 
     Rm_resourceStatus(server_h, 1);
-    
+
+    if (group_name) {
+        /* Get the group ID */
+        errno = 0;
+        group_data = getgrnam(group_name);
+        if (group_data) {
+            group_valid = 1;
+            serv_group_id = group_data->gr_gid;
+        } else {
+            if (errno) {
+                error_msg("Received error: \"%s\" when attempting to retrieve "
+                          "group database information for group: %s\n",
+                          strerror(errno), group_name);
+                return(-1);
+            } else {
+                error_msg("Group database information does not exist for "
+                          "group with name %s - "
+                          "Not setting group permissions\n", group_name);
+            }
+        }
+    }
     serv_sock_name.type = sock_name_e;
     serv_sock_name.s.name = rm_socket_name;
-    server_sock = sock_open (&serv_sock_name);
+    server_sock = sock_open(&serv_sock_name, group_valid, serv_group_id);
     if (!server_sock) {
-        error_msg("Error when opening socket %s", rm_socket_name);
+        error_msg("Error when opening socket %s\n", rm_socket_name);
         return -1;
     }
 
@@ -347,9 +373,8 @@ loop_continue:
 }
 
 char *get_pid_file_name(void) {
-       static char pid_file_name[] = RMSERVER_DAEMON_PID_FILE_NAME;
-
-       return pid_file_name;
+    static char pid_file_name[] = RMSERVER_DAEMON_PID_FILE_NAME;
+    return pid_file_name;
 }
 
 static void print_usage(char *appname)
@@ -371,6 +396,8 @@ static void print_usage(char *appname)
             "                            for Linux.  The GRL must have the\n"
             "                            proper Linux DTB resource mappings\n"
             "                            for this feature to work\n"
+            "  -g, --group [GROUP_NAME]  Group permission to assign to RM\n"
+            "                            Server socket\n"
             "Miscellaneous:\n"
             "  -n, --nodaemon            do not daemonize, run in foreground\n"
             "  -k, --kill                kill the existing daemon\n"
@@ -392,6 +419,7 @@ int main(int argc, char *argv[])
     void        *grl = NULL;
     void        *policy = NULL;
     void        *lin_dtb = NULL;
+    char        *group_name = NULL;
     
     const struct option longopts[] =
     {
@@ -400,12 +428,13 @@ int main(int argc, char *argv[])
         {"help",     no_argument,       0, 'h'},
         {"lindtb",   required_argument, 0, 'l'},
         {"logsize",  required_argument, 0, 's'},
+        {"group",    required_argument, 0, 'g'},
         {0,          0,                 0,   0},
     };
 
     rmsrv_cfg.logfile_max_len = DEFAULT_LOG_LEN;
 
-    while((opt = getopt_long(argc, argv, "nkhl:s:", longopts, NULL)) != -1) {
+    while((opt = getopt_long(argc, argv, "nkhl:s:g:", longopts, NULL)) != -1) {
         switch (opt) {
             case 'n':
                 daemonize = 0;
@@ -419,6 +448,9 @@ int main(int argc, char *argv[])
             case 's':
                 rmsrv_cfg.logfile_max_len = strtol(optarg, NULL, 0);
                 break;
+            case 'g':
+                group_name = optarg;
+                break;
             case 'h':
             default:
                 print_usage(argv[0]);
@@ -514,7 +546,7 @@ int main(int argc, char *argv[])
             exit(EXIT_FAILURE);
         }
 
-        if (check_and_create_path (get_pid_file_name()) < 0) {
+        if (check_and_create_path(get_pid_file_name(), 0, 0) < 0) {
             printf("Failed to create pid file path: %s\n", get_pid_file_name());
             exit(EXIT_FAILURE);
         }
@@ -595,7 +627,7 @@ int main(int argc, char *argv[])
         }
     }
     
-    rm_server_run(grl, policy, lin_dtb, daemonize);
+    rm_server_run(grl, policy, lin_dtb, daemonize, group_name);
 
 close_n_exit:
     printf("Exiting %s daemon\n", argv[0]);
index d3dbbecc3960083ccaa15be56419ae9d33eabc6a..ca23258dacdbe8fdace18cf3097c8733d1362b75 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
+ * Copyright (C) 2013-2015 Texas Instruments Incorporated - http://www.ti.com/
  *
  *
  *  Redistribution and use in source and binary forms, with or without
@@ -37,7 +37,7 @@
 
 #include <stdint.h>
 
-#define RM_SERVER_SOCKET_NAME "/tmp/var/run/rm/rm_server"
+#define RM_SERVER_SOCKET_NAME "/var/run/rm/rm_server"
 
 #define msg_alloc(p) \
        do { \
index 57cdacb60f7462c41422369610d5d03c5ef82628..455d5213d27ee41f60fc5afc285a28f520d17fe7 100644 (file)
@@ -50,9 +50,10 @@ typedef struct sock_data {
        int fd;
 } sock_data_t;
 
-int check_and_create_path (char *path)
+int check_and_create_path(char *path, int group_valid, gid_t group_id)
 {
-       char *d = path;
+       char   *d = path;
+       
        if (!d)
                return -1;
 
@@ -61,31 +62,45 @@ int check_and_create_path (char *path)
                if (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
                        if (errno != EEXIST) {
                                *d = '/';
-                               error_msg("can't create path %s (error: %s)",
+                               error_msg("can't create path %s (error: %s)\n",
                                                path, strerror(errno));
                                return -1;
                        }
+               } else if (group_valid) {
+                       /* Set group ownership if dir did not previously exist */
+                       if (chown(path, -1, group_id) < 0) {
+                               error_msg("couldn't set group ownership for path %s "
+                                                 "(error: %s)\n", path, strerror(errno));
+                       }
                }
                *d = '/';
        }
+
        return 0;
 }
 
-sock_h sock_open (sock_name_t *sock_name)
+sock_h sock_open(sock_name_t *sock_name, int group_valid, gid_t group_id)
 {
        sock_data_t *sd = 0;
-       int retval = 0;
+       int          retval = 0;
+       mode_t       old_mask;
 
        if (!sock_name) {
                return 0;
        }
 
+       if (group_valid) {
+               /* Allow group permissions to be set */
+               old_mask = umask(0002);
+       }
+
        sd = calloc (1, sizeof(sock_data_t));
 
        if (sock_name->type == sock_addr_e) {
                memcpy (&sd->addr, sock_name->s.addr, sizeof(struct sockaddr_un));
        } else {
-               if (check_and_create_path(sock_name->s.name) < 0) {
+               if (check_and_create_path(sock_name->s.name, group_valid,
+                                                                 group_id) < 0) {
                        goto check_n_return;
                }
                sd->addr.sun_family = AF_UNIX;
@@ -109,6 +124,14 @@ sock_h sock_open (sock_name_t *sock_name)
        FD_ZERO(&sd->readfds);
        FD_SET(sd->fd, &sd->readfds);
 
+       if (group_valid) {
+               if (chown(sock_name->s.name, -1, group_id) < 0) {
+                       error_msg("Can't assign socket %s to specified group (error: %s)",
+                                         sock_name->s.name, strerror(errno));
+                       goto check_n_return;
+               }
+       }
+
        retval = (int) sd;
 
 check_n_return:
@@ -116,6 +139,10 @@ check_n_return:
                sock_close ((sock_h) &sd);
        }
 
+       if (group_valid) {
+               umask(old_mask);
+       }
+
        return ((sock_h) retval);
 }
 
index dc5d811eef40d85db4807a53fc44cae968a273dc..3b6a3c985a73b0ff28fb6f66a6a812a8936dafe5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
+ * Copyright (C) 2013-2015 Texas Instruments Incorporated - http://www.ti.com/
  *
  *
  *  Redistribution and use in source and binary forms, with or without
@@ -58,7 +58,9 @@ typedef struct {
 
 #define sock_h void *
 
-sock_h sock_open (sock_name_t *sock_name);
+int check_and_create_path(char *path, int group_valid, gid_t group_id);
+
+sock_h sock_open(sock_name_t *sock_name, int group_valid, gid_t group_id);
 
 int sock_close (sock_h handle);