Pi4J / pi4j-v1

DEPRECATED Java I/O library for Raspberry Pi (GPIO, I2C, SPI, UART)
http://www.pi4j.com
Apache License 2.0
1.31k stars 446 forks source link

Unable to open GPIO export interface: Permission denied #86

Closed hemachowdary closed 10 years ago

hemachowdary commented 10 years ago

hi , i have a simple code for operating LED using raspberry pi. when i am running that using tomcat server i am getting following error.

HTTP Status 500 - java.lang.RuntimeException: Unable to open GPIO export interface: Permission denied

type Exception report

message java.lang.RuntimeException: Unable to open GPIO export interface: Permission denied

description The server encountered an internal error (java.lang.RuntimeException: Unable to open GPIO export interface: Permission denied ) that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.RuntimeException: Unable to open GPIO export interface: Permission denied

com.vaadin.server.VaadinServlet.handleServiceException(VaadinServlet.java:580)
com.vaadin.server.VaadinServlet.service(VaadinServlet.java:343)
com.vaadin.server.VaadinServlet.service(VaadinServlet.java:201)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.RuntimeException: Unable to open GPIO export interface: Permission denied

com.pi4j.wiringpi.GpioUtil.export(Native Method)
com.pi4j.io.gpio.RaspiGpioProvider.export(RaspiGpioProvider.java:67)
com.pi4j.io.gpio.impl.GpioPinImpl.export(GpioPinImpl.java:165)
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionPin(GpioControllerImpl.java:520)
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalOutputPin(GpioControllerImpl.java:645)
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalOutputPin(GpioControllerImpl.java:672)
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalOutputPin(GpioControllerImpl.java:684)
com.example.raspberrypigpioexample01.Raspberrypigpioexample01UI.init(Raspberrypigpioexample01UI.java:19)
com.vaadin.ui.UI.doInit(UI.java:529)
com.vaadin.server.AbstractCommunicationManager.getBrowserDetailsUI(AbstractCommunicationManager.java:2468)
com.vaadin.server.AbstractCommunicationManager.handleBrowserDetailsRequest(AbstractCommunicationManager.java:2362)
com.vaadin.server.VaadinServlet.service(VaadinServlet.java:325)
com.vaadin.server.VaadinServlet.service(VaadinServlet.java:201)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.28 logs.

can any one please help me to solve this issue ?

Thanks in advance

radut commented 10 years ago

try running the java as root first. If that solved then you must give permision to user tomcat/ pi which one is used for running tomcat to access GPIO

radut commented 10 years ago

http://stackoverflow.com/questions/12133860/accessing-the-gpio-of-a-raspberry-pi-without-sudo http://www.raspberrypi.org/forums/viewtopic.php?f=44&t=8999 https://github.com/quick2wire/quick2wire-gpio-admin

hemachowdary commented 10 years ago

hi radut, Thanks for your quick reply. i have tried them but still i am getting same issue.

radut commented 10 years ago

please write the output of : ps -ef |grep java

hemachowdary commented 10 years ago

output is :

tomcat7 2364 1 98 08:36 ? 00:00:58 /usr/lib/jvm/java-7-openjdk-armhf/bin/java -Djava.util.logging.config.file=/var/lib/tomcat7/conf/logging.properties -Djava.awt.headless=true -Xmx128m -XX:+UseConcMarkSweepGC -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/share/tomcat7/endorsed -classpath /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/var/lib/tomcat7 -Dcatalina.home=/usr/share/tomcat7 -Djava.io.tmpdir=/tmp/tomcat7-tomcat7-tmp org.apache.catalina.startup.Bootstrap start pi 2402 2228 0 08:37 pts/0 00:00:00 grep --color=auto java

radut commented 10 years ago

here is user tomcat7. try with root. do you have init.d script ? please write it here (because there are >10 init.d scripts for tomcat :) )

hemachowdary commented 10 years ago

no i doesnt have init.d script.........

radut commented 10 years ago

how do you start tomcat then ?

hemachowdary commented 10 years ago

i use following command : sudo service tomcat7 start

radut commented 10 years ago

did you checked for any tomcat7 file in /etc/init.d/ ? I belive that service is a wrapper over a init script:-?

hemachowdary commented 10 years ago

yes i am having tomcat7 file in that path ........

radut commented 10 years ago

write it here

hemachowdary commented 10 years ago

!/bin/sh

#

/etc/init.d/tomcat7 -- startup script for the Tomcat 6 servlet engine

#

Written by Miquel van Smoorenburg miquels@cistron.nl.

Modified for Debian GNU/Linux by Ian Murdock imurdock@gnu.ai.mit.edu.

Modified for Tomcat by Stefan Gybas sgybas@debian.org.

Modified for Tomcat6 by Thierry Carrez thierry.carrez@ubuntu.com.

Modified for Tomcat7 by Ernesto Hernandez-Novich emhn@itverx.com.ve.

Additional improvements by Jason Brittain jason.brittain@mulesoft.com.

#

BEGIN INIT INFO

Provides: tomcat7

Required-Start: $local_fs $remote_fs $network

Required-Stop: $local_fs $remote_fs $network

Should-Start: $named

Should-Stop: $named

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: Start Tomcat.

Description: Start the Tomcat servlet engine.

END INIT INFO

set -e

PATH=/bin:/usr/bin:/sbin:/usr/sbin NAME=tomcat7 DESC="Tomcat servlet engine" DEFAULT=/etc/default/$NAME JVM_TMP=/tmp/tomcat7-$NAME-tmp

if [ id -u -ne 0 ]; then echo "You need root privileges to run this script" exit 1 fi

Make sure tomcat is started with system locale

if [ -r /etc/default/locale ]; then . /etc/default/locale export LANG fi

. /lib/lsb/init-functions

if [ -r /etc/default/rcS ]; then . /etc/default/rcS fi

The following variables can be overwritten in $DEFAULT

Run Tomcat 7 as this user ID and group ID

TOMCAT7_USER=tomcat7 TOMCAT7_GROUP=tomcat7

this is a work-around until there is a suitable runtime replacement

for dpkg-architecture for arch:all packages

this function sets the variable OPENJDKS

find_openjdks() { for jvmdir in /usr/lib/jvm/java-7-openjdk- do if [ -d "${jvmdir}" -a "${jvmdir}" != "/usr/lib/jvm/java-7-openjdk-common" ] then OPENJDKS=$jvmdir fi done for jvmdir in /usr/lib/jvm/java-6-openjdk- do if [ -d "${jvmdir}" -a "${jvmdir}" != "/usr/lib/jvm/java-6-openjdk-common" ] then OPENJDKS="${OPENJDKS} ${jvmdir}" fi done }

OPENJDKS="" find_openjdks

The first existing directory is used for JAVA_HOME (if JAVA_HOME is not

defined in $DEFAULT)

JDK_DIRS="/usr/lib/jvm/default-java ${OPENJDKS} /usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-sun"

Look for the right JVM to use

for jdir in $JDK_DIRS; do if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then JAVA_HOME="$jdir" fi done export JAVA_HOME

Directory where the Tomcat 6 binary distribution resides

CATALINA_HOME=/usr/share/$NAME

Directory for per-instance configuration files and webapps

CATALINA_BASE=/var/lib/$NAME

Use the Java security manager? (yes/no)

TOMCAT7_SECURITY=no

Default Java options

Set java.awt.headless=true if JAVA_OPTS is not set so the

Xalan XSL transformer can work without X11 display on JDK 1.4+

It also looks like the default heap size of 64M is not enough for most cases

so the maximum heap size is set to 128M

if [ -z "$JAVA_OPTS" ]; then JAVA_OPTS="-Djava.awt.headless=true -Xmx128M" fi

End of variables that can be overwritten in $DEFAULT

overwrite settings from default file

if [ -f "$DEFAULT" ]; then . "$DEFAULT" fi

if [ ! -f "$CATALINA_HOME/bin/bootstrap.jar" ]; then log_failure_msg "$NAME is not installed" exit 1 fi

POLICY_CACHE="$CATALINA_BASE/work/catalina.policy"

if [ -z "$CATALINA_TMPDIR" ]; then CATALINA_TMPDIR="$JVM_TMP" fi

Set the JSP compiler if set in the tomcat7.default file

if [ -n "$JSP_COMPILER" ]; then JAVA_OPTS="$JAVA_OPTS -Dbuild.compiler=\"$JSP_COMPILER\"" fi

SECURITY="" if [ "$TOMCAT7_SECURITY" = "yes" ]; then SECURITY="-security" fi

Define other required variables

CATALINA_PID="/var/run/$NAME.pid" CATALINA_SH="$CATALINA_HOME/bin/catalina.sh"

Look for Java Secure Sockets Extension (JSSE) JARs

if [ -z "${JSSE_HOME}" -a -r "${JAVA_HOME}/jre/lib/jsse.jar" ]; then JSSE_HOME="${JAVA_HOME}/jre/" fi

catalina_sh() {

Escape any double quotes in the value of JAVA_OPTS

JAVA_OPTS="$(echo $JAVA_OPTS | sed 's/\"/\\"/g')"
AUTHBIND_COMMAND=""
if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then
    JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true"
    AUTHBIND_COMMAND="/usr/bin/authbind --deep /bin/bash -c "
fi

# Define the command to run Tomcat's catalina.sh as a daemon
# set -a tells sh to export assigned variables to spawned shells.
TOMCAT_SH="set -a; JAVA_HOME=\"$JAVA_HOME\"; source \"$DEFAULT\"; \
    CATALINA_HOME=\"$CATALINA_HOME\"; \
    CATALINA_BASE=\"$CATALINA_BASE\"; \
    JAVA_OPTS=\"$JAVA_OPTS\"; \
    CATALINA_PID=\"$CATALINA_PID\"; \
    CATALINA_TMPDIR=\"$CATALINA_TMPDIR\"; \
    LANG=\"$LANG\"; JSSE_HOME=\"$JSSE_HOME\"; \
    cd \"$CATALINA_BASE\"; \
    \"$CATALINA_SH\" $@"

if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then
    TOMCAT_SH="'$TOMCAT_SH'"
fi

# Run the catalina.sh script as a daemon
set +e
touch "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out
chown $TOMCAT7_USER "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out
start-stop-daemon --start -b -u "$TOMCAT7_USER" -g "$TOMCAT7_GROUP" \
    -c "$TOMCAT7_USER" -d "$CATALINA_TMPDIR" -p "$CATALINA_PID" \
    -x /bin/bash -- -c "$AUTHBIND_COMMAND $TOMCAT_SH"
status="$?"
set +a -e
return $status

}

case "$1" in start) if [ -z "$JAVA_HOME" ]; then log_failure_msg "no JDK found - please set JAVA_HOME" exit 1 fi

if [ ! -d "$CATALINA_BASE/conf" ]; then
    log_failure_msg "invalid CATALINA_BASE: $CATALINA_BASE"
    exit 1
fi

log_daemon_msg "Starting $DESC" "$NAME"
if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
    --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
    >/dev/null; then

    # Regenerate POLICY_CACHE file
    umask 022
    echo "// AUTO-GENERATED FILE from /etc/tomcat7/policy.d/" \
        > "$POLICY_CACHE"
    echo ""  >> "$POLICY_CACHE"
    cat $CATALINA_BASE/conf/policy.d/*.policy \
        >> "$POLICY_CACHE"

    # Remove / recreate JVM_TMP directory
    rm -rf "$JVM_TMP"
    mkdir -p "$JVM_TMP" || {
        log_failure_msg "could not create JVM temporary directory"
        exit 1
    }
    chown $TOMCAT7_USER "$JVM_TMP"

    catalina_sh start $SECURITY
    sleep 5
        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
        --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
        >/dev/null; then
        if [ -f "$CATALINA_PID" ]; then
            rm -f "$CATALINA_PID"
        fi
        log_end_msg 1
    else
        log_end_msg 0
    fi
else
        log_progress_msg "(already running)"
    log_end_msg 0
fi
;;

stop) log_daemon_msg "Stopping $DESC" "$NAME"

set +e
if [ -f "$CATALINA_PID" ]; then 
    start-stop-daemon --stop --pidfile "$CATALINA_PID" \
        --user "$TOMCAT7_USER" \
        --retry=TERM/20/KILL/5 >/dev/null
    if [ $? -eq 1 ]; then
        log_progress_msg "$DESC is not running but pid file exists, cleaning up"
    elif [ $? -eq 3 ]; then
        PID="`cat $CATALINA_PID`"
        log_failure_msg "Failed to stop $NAME (pid $PID)"
        exit 1
    fi
    rm -f "$CATALINA_PID"
    rm -rf "$JVM_TMP"
else
    log_progress_msg "(not running)"
fi
log_end_msg 0
set -e
;;

status) set +e start-stop-daemon --test --start --pidfile "$CATALINA_PID" \ --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \

/dev/null 2>&1 if [ "$?" = "0" ]; then

    if [ -f "$CATALINA_PID" ]; then
        log_success_msg "$DESC is not running, but pid file exists."
        exit 1
    else
        log_success_msg "$DESC is not running."
        exit 3
    fi
else
    log_success_msg "$DESC is running with pid `cat $CATALINA_PID`"
fi
set -e
    ;;

restart|force-reload) if [ -f "$CATALINA_PID" ]; then $0 stop sleep 1 fi $0 start ;; try-restart) if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \ --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \

/dev/null; then $0 start fi ;; *) log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}" exit 1 ;; esac

exit 0

radut commented 10 years ago

search for : TOMCAT7_USER=tomcat7 TOMCAT7_GROUP=tomcat7 and change to TOMCAT7_USER=root TOMCAT7_GROUP=root

hemachowdary commented 10 years ago

i changed TOMCAT7_USER=root TOMCAT7_GROUP=root

but still i am getting same issue

radut commented 10 years ago

ps -ef | grep java has 'root' in first column ?

hemachowdary commented 10 years ago

no it is having pi pi 2635 2228 0 09:01 pts/0 00:00:00 grep --color=auto java

hemachowdary commented 10 years ago

complete output of that command tomcat7 2588 1 42 08:54 ? 00:02:46 /usr/lib/jvm/java-7-openjdk-armhf/bin/java -Djava.util.logging.config.file=/var/lib/tomcat7/conf/logging.properties -Djava.awt.headless=true -Xmx128m -XX:+UseConcMarkSweepGC -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/share/tomcat7/endorsed -classpath /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/var/lib/tomcat7 -Dcatalina.home=/usr/share/tomcat7 -Djava.io.tmpdir=/tmp/tomcat7-tomcat7-tmp org.apache.catalina.startup.Bootstrap start pi 2635 2228 0 09:01 pts/0 00:00:00 grep --color=auto java

radut commented 10 years ago

no, it's stil tomcat7, the line with /usr/lib/jvm... must be followed can we move this discussion to somewhere else, like gtalk ?

hemachowdary commented 10 years ago

sorry to say , i am at office i cant come to gtalk.

radut commented 10 years ago

ok stop tomcat

cd /usr/share/tomcat7/bin sudo su (check if user is root) id ./catalina.sh then again ps -ef | grep java

hemachowdary commented 10 years ago

[ ok ] Stopping Tomcat servlet engine: tomcat7. pi@raspberrypi ~ $ cd /usr/share/tomcat7/bin pi@raspberrypi /usr/share/tomcat7/bin $ sudo su root@raspberrypi:/usr/share/tomcat7/bin# id uid=0(root) gid=0(root) groups=0(root),1001(indiecity),1003(gpio) root@raspberrypi:/usr/share/tomcat7/bin# ./catailna.sh bash: ./catailna.sh: No such file or directory

i think user is root only pl find the above lines for your reference

radut commented 10 years ago

ok you are root but you don't have catalina.sh in bin directory what's the listing of that directory ? ls

hemachowdary commented 10 years ago

bootstrap.jar configtest.sh setclasspath.sh tomcat-juli.jar catalina.sh daemon.sh shutdown.sh tool-wrapper.sh catalina-tasks.xml digest.sh startup.sh version.sh

radut commented 10 years ago

you wrote catailna.sh when it's catalina.sh , use TAB for auto complete

by the way, I see that root is in group gpio, maybe it is easyer to add tomcat7 to group gpio run as sudo usermod -a -G gpio tomcat7 and start the service again

hemachowdary commented 10 years ago

i follwed above steps now there is some change in error

Unable to open GPIO direction interface for pin [0]: Permission denied

radut commented 10 years ago

and now you are running tomcat as user ...tomcat7 ? still permission.. stop tomcat, and try again running ./catalina.sh check what user is running java as root should work, and have no permission problems.

hemachowdary commented 10 years ago

this is the output i am getting

pi@raspberrypi ~ $ cd /usr/share/tomcat7/bin pi@raspberrypi /usr/share/tomcat7/bin $ sudo su root@raspberrypi:/usr/share/tomcat7/bin# ./catalina.sh Using CATALINA_BASE: /usr/share/tomcat7 Using CATALINA_HOME: /usr/share/tomcat7 Using CATALINA_TMPDIR: /usr/share/tomcat7/temp Using JRE_HOME: /usr/lib/jvm/java-1.7.0-openjdk-armhf Using CLASSPATH: /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar Usage: catalina.sh ( commands ... ) commands: debug Start Catalina in a debugger debug -security Debug Catalina with a security manager jpda start Start Catalina under JPDA debugger run Start Catalina in the current window run -security Start in the current window with security manager start Start Catalina in a separate window start -security Start in a separate window with security manager stop Stop Catalina, waiting up to 5 seconds for the process to end stop n Stop Catalina, waiting up to n seconds for the process to end stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running configtest Run a basic syntax check on server.xml - check exit code for result version What version of tomcat are you running? Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined

hemachowdary commented 10 years ago

should i changed this TOMCAT7_USER=root TOMCAT7_GROUP=root

back to

TOMCAT7_USER=tomcat7 TOMCAT7_GROUP=tomcat7

radut commented 10 years ago

./catalina.sh start

hemachowdary commented 10 years ago

Using CATALINA_BASE: /usr/share/tomcat7 Using CATALINA_HOME: /usr/share/tomcat7 Using CATALINA_TMPDIR: /usr/share/tomcat7/temp Using JRE_HOME: /usr/lib/jvm/java-1.7.0-openjdk-armhf Using CLASSPATH: /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar touch: cannot touch `/usr/share/tomcat7/logs/catalina.out': No such file or directory ./catalina.sh: 389: ./catalina.sh: cannot create /usr/share/tomcat7/logs/catalina.out: Directory nonexistent

i am getting directory not existent find the above output for your reference..