@@ -38,6 +38,10 @@ SOFTWARE.
3838#include "event_gpio.h"
3939#include "common.h"
4040
41+ #define GPIO_NOT_EXPORTED 0
42+ #define GPIO_EXPORTED 1
43+ #define GPIO_ALREADY_EXPORTED 2
44+
4145const char * stredge [4 ] = {"none" , "rising" , "falling" , "both" };
4246
4347// file descriptors
@@ -61,7 +65,7 @@ struct callback
6165struct callback * callbacks = NULL ;
6266
6367pthread_t threads ;
64- int exported_gpios [120 ] = { 0 };
68+ int exported_gpios [120 ] = { GPIO_NOT_EXPORTED };
6569int event_occurred [120 ] = { 0 };
6670int thread_running = 0 ;
6771int epfd = -1 ;
@@ -71,9 +75,19 @@ int gpio_export(unsigned int gpio)
7175 int fd , len ;
7276 char str_gpio [10 ];
7377
74- // already exported
75- if (exported_gpios [gpio ] != 0 )
76- return 1 ;
78+ // already exported by us?
79+ if (exported_gpios [gpio ] != GPIO_NOT_EXPORTED )
80+ return 0 ;
81+
82+ // already exported by someone else?
83+ char gpio_path [64 ];
84+ snprintf (gpio_path , sizeof (gpio_path ), "/sys/class/gpio/gpio%d" , gpio );
85+
86+ if (access (gpio_path , R_OK |W_OK |X_OK ) != -1 )
87+ {
88+ exported_gpios [gpio ] = GPIO_ALREADY_EXPORTED ;
89+ return 0 ;
90+ }
7791
7892 if ((fd = open ("/sys/class/gpio/export" , O_WRONLY )) < 0 )
7993 {
@@ -87,7 +101,7 @@ int gpio_export(unsigned int gpio)
87101 }
88102
89103 // add to list
90- exported_gpios [gpio ] = 1 ;
104+ exported_gpios [gpio ] = GPIO_EXPORTED ;
91105 return 0 ;
92106}
93107
@@ -173,7 +187,7 @@ int gpio_unexport(unsigned int gpio)
173187 int fd , len ;
174188 char str_gpio [10 ];
175189
176- if (exported_gpios [gpio ] == 0 )
190+ if (exported_gpios [gpio ] != GPIO_EXPORTED )
177191 return 0 ;
178192
179193 close_value_fd (gpio );
@@ -189,7 +203,7 @@ int gpio_unexport(unsigned int gpio)
189203 }
190204
191205 // remove from list
192- exported_gpios [gpio ] = 0 ;
206+ exported_gpios [gpio ] = GPIO_NOT_EXPORTED ;
193207 return 0 ;
194208}
195209
0 commit comments