비글본블랙 우분투에서 파일 권한 문제

newton98의 이미지

안녕하세요?

제가(리눅스초보라 개념이 없습니다) 4일째 고생하다 이유를 찾았습니다.
시간이 없어서 하드코딩을 하던지 그래야 하는데 방법이 있는지 궁금 합니다.

비글본 블랙에 우분투를 올렸습니다.
웹페이지에서 제가 만들 파일을 실행하려고 합니다.
(제가 만든 프로그램 : 릴레이를 동작시키는 프로그램입니다)
예를 들면 ksw.cpp를 컴파일 해서 ksw를 만들었습니다.
쉘에서 잘 동작합니다.
./ksw
하면 잘 동작

exec 를 사용하면 될것 같아 명령어를 다음과 같이 테스트 했습니다.

<?php
$output = exec('ls -l');
echo "

$output
";
?>

잘 동작 합니다.

hello.c를 작성했습니다.
#include

int main()
{
printf("Hello World\n"
exit(0);
}

<?php
$output = exec('hello');
echo "

$output
";
?>
잘동작합니다.

그런데 제가 만든것만 웹에서 실행하면 릴레이가 동작하지 않습니다.
중간에 COUT등으로 메시지를 집어 넣었는데 메세지는 잘 보여 집니다.

혹시 컴파일에 문제가 있나 싶어 헤더,CPP1, CPP2 로 되어 있는것을
하나로 합쳐도 보았습니다.

콘솔에서 하면 잘되는데 웹에서는 안되네요...
이유가 무었일까요?
이것때문에 별거별거 다 하다가 3-4일째 진도 못나가고 있네요.
혹시 힌트될만한것 있으면 부탁드립니다.
검색을 하니 권한 문제가 제일 많이 나와서 권한 쪽을 살펴 봤는데 chmod 777로 모든(php,실행) 파일을 바꿔도 봤습니다.
====================================================================================================
위의 문제는 GPIO의 export unexport시 권한 문제였습니다.
인터넷으로 할때는 nobody라고 하는군요. w권한이 없어서 실행이 안되었고 shell에서는 sudo로 했었기 때문에 된것입니다.

그런데 access( file_name, W_OK))조회해서 안되면
chmod함수를 사용해서 권한 변경을 하려고 했는데 이것도 쉘은 되는데 웨에서는 안됩니다.

방법이 없을까요?
컴파일한 바이너리를 실행할때도 권한이 문제가 되는것인지 몰랐습니다. Web이니 보안때문인것 같은데
간단할 줄만 알았는데 너무 힘이 드네요.^^

요것도 쉘은 ok 웹에서는 안됨 you Can't mode chagned

#include
#include
#include

int main( void)
{
char *file_name = "/sys/class/gpio/export";

if ( 0 == access( file_name, F_OK)){
printf( "%s file is being.\n", file_name);

if ( 0 == access( file_name, W_OK)){
printf( "you can write it\n");
}
else{
printf( "you Can't write it\n");
//mode change
if(chmod(file_name, 722) == 0){
printf("mode changed 722\n");
}
else{
printf( "you Can't mode chagned\n");
}
}
}
}

GPIO 함수들
/**********************************
* gpio_export
**********************************/
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];

fd = open(SYSFS_GPIO_DIR "/export" SYSCHMOD_GPIO_DIR "/export" , O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}

len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);

return 0;
}

/****************************************************************
* gpio_unexport
****************************************************************/
int gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];

fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}

len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}

/****************************************************************
* gpio_set_dir
****************************************************************/
int gpio_set_dir(unsigned int gpio, PIN_DIRECTION out_flag)
{
int fd;
char buf[MAX_BUF];

snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);

fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/direction");
return fd;
}

if (out_flag == OUTPUT_PIN)
write(fd, "out", 4);
else
write(fd, "in", 3);

close(fd);
return 0;
}

/****************************************************************
* gpio_set_value
****************************************************************/
int gpio_set_value(unsigned int gpio, PIN_VALUE value)
{
int fd;
char buf[MAX_BUF];

snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);

fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/set-value");
return fd;
}

if (value==LOW)
write(fd, "0", 2);
else
write(fd, "1", 2);

close(fd);
return 0;
}

/****************************************************************
* gpio_get_value
****************************************************************/
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd;
char buf[MAX_BUF];
char ch;

snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);

fd = open(buf, O_RDONLY);
if (fd < 0) {
perror("gpio/get-value");
return fd;
}

read(fd, &ch, 1);

if (ch != '0') {
*value = 1;
} else {
*value = 0;
}

close(fd);
return 0;
}

/****************************************************************
* gpio_set_edge
****************************************************************/

int gpio_set_edge(unsigned int gpio, char *edge)
{
int fd;
char buf[MAX_BUF];

snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);

fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/set-edge");
return fd;
}

write(fd, edge, strlen(edge) + 1);
close(fd);
return 0;
}

/****************************************************************
* gpio_fd_open
****************************************************************/

int gpio_fd_open(unsigned int gpio)
{
int fd;
char buf[MAX_BUF];

snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);

fd = open(buf, O_RDONLY | O_NONBLOCK );
if (fd < 0) {
perror("gpio/fd_open");
}
return fd;
}

/****************************************************************
* gpio_fd_close
****************************************************************/

int gpio_fd_close(int fd)
{
return close(fd);
}

====================================================================================================

하고자 하는것은 웹에서 인증(아이디, 비번)받고
각 버튼을 누르면 버튼 명령에 맞게 릴레이가 움직이는 것입니다.
쉘에서는 다 구현 해 놓았고 회로도 간단하게 그렸는데 연동쪽에서 막히고요.

프로그램으로 쓰기권한을 주고 싶어요...
GPIO export, unexport, DIRECTION, Value 값을 핀에 맞게 작동해야 하는데
웹의 권한에서 막힙니다.

어떻게 해야 할까요?

bacon의 이미지

위에 sudo를 언급하셨으니, /etc/sudoers에 웹서버유저(위에 nobody로 예를 들어 놓은듯)가 해당 명령어를 패스워드 없이 실행가능하게 설정해놓고 웹페이지에서 sudo로 실행하는 방법도 있겠고,

해당명령어파일의 owner는 root(혹은 유사한 권한의 유저)로 하고, setuid 비트를 설정해놓을수도 있겠고,

root(혹은 유사한 권한의 유저)로 돌아가는 다른 프로그램에 실행을 의뢰하는 방법등등이 있겠네요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.