changeset 137:133a8266a5ac

8075360: [UART] add restore/drop privileges calls to the open procedure. Summary: Privilege restored for driver open procedure. Reviewed-by: snazarki Contributed-by: alexey.karaksin@oracle.com
author snazarki
date Mon, 23 Mar 2015 13:32:55 +0300
parents 636b32ba06f9
children 29314732135b
files src/share/linux/native/com/oracle/dio/uart/serial.c
diffstat 1 files changed, 13 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/linux/native/com/oracle/dio/uart/serial.c	Wed Mar 18 14:55:33 2015 +0300
+++ b/src/share/linux/native/com/oracle/dio/uart/serial.c	Mon Mar 23 13:32:55 2015 +0300
@@ -25,6 +25,7 @@
 
 #include "serial.h"
 #include "list_helper.h"
+#include "privilege_manager.h"
 
 typedef void *(thread_func)(void*);
 
@@ -730,10 +731,15 @@
     if(get_config_by_name(p->dev_name) != NULL){
         return JAVACALL_BUSY;
     }
-    //opening one of the /dev/ttyUSB devices, no pin busy check
-    if (-1 == (p->fd = open(p->dev_name, O_RDWR | O_NOCTTY | O_NONBLOCK))) {
-        JAVACALL_REPORT_ERROR2(JC_SERIAL, "Can't open %s file errno %d",
-                p->dev_name, errno);
+
+    // restore root privileges temporary
+    restore_privileges();
+    p->fd = open(p->dev_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
+    drop_privileges();
+
+    if (-1 == p->fd) {
+        JAVACALL_REPORT_ERROR2(JC_SERIAL, "Can't open %s file. %s",
+                p->dev_name, strerror(errno));
         return JAVACALL_FAIL;
     }
     /*flock set an advisory lock*/
@@ -744,15 +750,16 @@
     lock.l_pid    = getpid();
 
     if(-1 == fcntl(p->fd, F_SETLK, &lock)){
+        //save errno for printouts, next "close" call may modify it
         error_n = errno;
         close(p->fd);
         p->fd = -1;
 
         if (error_n == EACCES || error_n == EAGAIN) {
-            JAVACALL_REPORT_ERROR1(JC_DIO, "[UART] Can't lock device. errno %d", errno);
+            JAVACALL_REPORT_ERROR1(JC_DIO, "[UART] Can't lock device. %s", strerror(error_n));
             return JAVACALL_BUSY;
         }else{
-            JAVACALL_REPORT_ERROR1(JC_DIO, "[UART] Can't lock device. errno %d", errno);
+            JAVACALL_REPORT_ERROR1(JC_DIO, "[UART] Can't lock device. %s", strerror(error_n));
             return JAVACALL_FAIL;
         }
     }