Esempio n. 1
0
static void doLoad (int argc, char *argv [])
{
  char *module1, *module2 ;
  char cmd [80] ;
  char *file1, *file2 ;
  char args1 [32], args2 [32] ;

  if (argc < 3)
    _doLoadUsage (argv) ;

  args1 [0] = args2 [0] = 0 ;

  /**/ if (strcasecmp (argv [2], "spi") == 0)
  {
    module1 = "spidev" ;
    module2 = "spi_bcm2708" ;
    file1  = "/dev/spidev0.0" ;
    file2  = "/dev/spidev0.1" ;
    if (argc == 4)
      sprintf (args1, " bufsize=%d", atoi (argv [3]) * 1024) ;
    else if (argc > 4)
      _doLoadUsage (argv) ;
  }
  else if (strcasecmp (argv [2], "i2c") == 0)
  {
    module1 = "i2c_dev" ;
    module2 = "i2c_bcm2708" ;
    file1  = "/dev/i2c-0" ;
    file2  = "/dev/i2c-1" ;
    if (argc == 4)
      sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
    else if (argc > 4)
      _doLoadUsage (argv) ;
  }
  else
    _doLoadUsage (argv) ;

  if (!moduleLoaded (module1))
  {
    sprintf (cmd, "modprobe %s%s", module1, args1) ;
    system (cmd) ;
  }

  if (!moduleLoaded (module2))
  {
    sprintf (cmd, "modprobe %s%s", module2, args2) ;
    system (cmd) ;
  }

  if (!moduleLoaded (module2))
  {
    fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
    exit (1) ;
  }

  sleep (1) ;	// To let things get settled

  changeOwner (argv [0], file1) ;
  changeOwner (argv [0], file2) ;
}
Esempio n. 2
0
void doExport (int argc, char *argv [])
{
  FILE *fd ;
  int pin ;
  char *mode ;
  char fName [128] ;

  if (argc != 4)
  {
    fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
    exit (1) ;
  }

  pin = atoi (argv [2]) ;

  mode = argv [3] ;

  if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
  {
    fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
    exit (1) ;
  }

  fprintf (fd, "%d\n", pin) ;
  fclose (fd) ;

  sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
  if ((fd = fopen (fName, "w")) == NULL)
  {
    fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
    exit (1) ;
  }

  /**/ if ((strcasecmp (mode, "in")   == 0) || (strcasecmp (mode, "input")  == 0))
    fprintf (fd, "in\n") ;
  else if ((strcasecmp (mode, "out")  == 0) || (strcasecmp (mode, "output") == 0))
    fprintf (fd, "out\n") ;
  else if ((strcasecmp (mode, "high") == 0) || (strcasecmp (mode, "up")     == 0))
    fprintf (fd, "high\n") ;
  else if ((strcasecmp (mode, "low")  == 0) || (strcasecmp (mode, "down")   == 0))
    fprintf (fd, "low\n") ;
  else
  {
    fprintf (stderr, "%s: Invalid mode: %s. Should be in, out, high or low\n", argv [1], mode) ;
    exit (1) ;
  }

  fclose (fd) ;

// Change ownership so the current user can actually use it

  sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  changeOwner (argv [0], fName) ;

  sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
  changeOwner (argv [0], fName) ;

}
Esempio n. 3
0
static void doLoad (int argc, char *argv [])
{
  char *module ;
  char cmd [80] ;
  char *file1, *file2 ;

  if (argc != 3)
    _doLoadUsage (argv) ;

  /**/ if (strcasecmp (argv [2], "spi") == 0)
  {
    module = "spi_bcm2708" ;
    file1  = "/dev/spidev0.0" ;
    file2  = "/dev/spidev0.1" ;
  }
  else if (strcasecmp (argv [2], "i2c") == 0)
  {
    module = "i2c_bcm2708" ;
    file1  = "/dev/i2c-0" ;
    file2  = "/dev/i2c-1" ;
  }
  else
    _doLoadUsage (argv) ;

  if (!moduleLoaded (module))
  {
    sprintf (cmd, "modprobe %s", module) ;
    system (cmd) ;
  }

  if (!moduleLoaded (module))
  {
    fprintf (stderr, "%s: Unable to load %s\n", argv [0], module) ;
    exit (1) ;
  }

  sleep (1) ;	// To let things get settled

  changeOwner (argv [0], file1) ;
  changeOwner (argv [0], file2) ;
}
Esempio n. 4
0
void doEdge (int argc, char *argv [])
{
    FILE *fd ;
    int pin ;
    char *mode ;
    char fName [128] ;

    if (argc != 4)
    {
        fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
        exit (1) ;
    }

    pin  = atoi (argv [2]) ;
    mode = argv [3] ;

    /*add for BananaPro by LeMaker team*/
    if (pin == 0)
    {
        printf("%d is invalid pin,please check it over.\n", pin);
        return ;
    }
    /*end 2014.08.19*/

    // Export the pin and set direction to input

    if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
    {
        fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
        exit (1) ;
    }

    fprintf (fd, "%d\n", pin) ;
    fclose (fd) ;

    sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
    if ((fd = fopen (fName, "w")) == NULL)
    {
        fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
        exit (1) ;
    }

    fprintf (fd, "in\n") ;
    fclose (fd) ;

    sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
    if ((fd = fopen (fName, "w")) == NULL)
    {
        fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
        exit (1) ;
    }

    if (strcasecmp (mode, "none")    == 0) fprintf (fd, "none\n") ;
    else if (strcasecmp (mode, "rising")  == 0) fprintf (fd, "rising\n") ;
    else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
    else if (strcasecmp (mode, "both")    == 0) fprintf (fd, "both\n") ;
    else
    {
        fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
        exit (1) ;
    }

    // Change ownership of the value and edge files, so the current user can actually use it!

    sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
    changeOwner (argv [0], fName) ;

    sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
    changeOwner (argv [0], fName) ;

    fclose (fd) ;
}
Esempio n. 5
0
void doExport (int argc, char *argv [])
{
    FILE *fd ;
    int pin ;
    char *mode ;
    char fName [128] ;

    if (argc != 4)
    {
        fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
        exit (1) ;
    }

    pin = atoi (argv [2]) ;

    /*add for BananaPro by LeMaker team*/
    if (pin == 0)
    {
        printf("%d is invalid pin,please check it over.\n", pin);
        return ;
    }
    /*end 2014.08.19*/

    mode = argv [3] ;

    if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
    {
        fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
        exit (1) ;
    }

    fprintf (fd, "%d\n", pin) ;
    fclose (fd) ;

    sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
    if ((fd = fopen (fName, "w")) == NULL)
    {
        fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
        exit (1) ;
    }

    /**/ if ((strcasecmp (mode, "in")  == 0) || (strcasecmp (mode, "input")  == 0))
        fprintf (fd, "in\n") ;
    else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
        fprintf (fd, "out\n") ;
    else
    {
        fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
        exit (1) ;
    }

    fclose (fd) ;

    // Change ownership so the current user can actually use it!

    sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
    changeOwner (argv [0], fName) ;

    sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
    changeOwner (argv [0], fName) ;

}
Esempio n. 6
0
static void doLoad (int argc, char *argv [])
{
    char *module1, *module2 ;
    char cmd [80] ;
    char *file1, *file2 ;
    char args1 [32], args2 [32] ;

    if (argc < 3)
        _doLoadUsage (argv) ;

    args1 [0] = args2 [0] = 0 ;

    //add for S500
    if(S500_REV == piBoardRev())
    {
        if (strcasecmp (argv [2], "spi") == 0)
        {
            module1 = "spidev" ;
            module2 = "spi-owl" ;
            file1  = "/dev/spidev0.0" ;
            file2  = "/dev/spidev0.1" ;
            if (argc == 4)
                sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ;
            else if (argc > 4)
                _doLoadUsage (argv) ;
        }
        else if (strcasecmp (argv [2], "i2c") == 0)
        {
            module1 = "i2c_dev" ;
            module2 = "i2c-owl" ;
            file1  = "/dev/i2c-2" ;
            file2  = "/dev/i2c-2" ;
            if (argc == 4)
                sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
            else if (argc > 4)
                _doLoadUsage (argv) ;
        }
        else
            _doLoadUsage (argv) ;


        if (!moduleLoaded (module1))
        {
            sprintf (cmd, "  %s%s", module1, args1) ;
            system (cmd) ;
        }


        if (!moduleLoaded (module2))
        {
            sprintf (cmd, "modprobe %s%s", module2, args2) ;
            system (cmd) ;
        }

        if (!moduleLoaded (module2))
        {
            fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
            exit (1) ;
        }
    }
    /*add for BananaPro by LeMaker team*/
    else if(BP_REV ==  piBoardRev())
    {
        /**/ if (strcasecmp (argv [2], "spi") == 0)
        {
            module1 = "spidev" ;
            module2 = "spi-sun7i" ;
            file1  = "/dev/spidev0.0" ;
            file2  = "/dev/spidev0.1" ;
            if (argc == 4)
                sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ;
            else if (argc > 4)
                _doLoadUsage (argv) ;
        }
        else if (strcasecmp (argv [2], "i2c") == 0)
        {
            module1 = "i2c_dev" ;
            module2 = "i2c-sunxi" ;
            file1  = "/dev/i2c-2" ;
            file2  = "/dev/i2c-2" ;
            if (argc == 4)
                sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
            else if (argc > 4)
                _doLoadUsage (argv) ;
        }
        else
            _doLoadUsage (argv) ;


        if (!moduleLoaded (module1))
        {
            sprintf (cmd, "  %s%s", module1, args1) ;
            system (cmd) ;
        }


        if (!moduleLoaded (module2))
        {
            sprintf (cmd, "modprobe %s%s", module2, args2) ;
            system (cmd) ;
        }

        if (!moduleLoaded (module2))
        {
            fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
            exit (1) ;
        }
    }
    else
    {

    }
    /*end 2014.08.19*/
    sleep (1) ;	// To let things get settled

    changeOwner (argv [0], file1) ;
    changeOwner (argv [0], file2) ;
}
Esempio n. 7
0
int LshttpdMain::init(int argc, char *argv[])
{
    int ret;
    ServerProcessConfig &procConfig = ServerProcessConfig::getInstance();
    if (argc > 1)
        parseOpt(argc, argv);
    if (getServerRoot(argc, argv) != 0)
    {
        //LS_ERROR("Failed to determine the root directory of server!" ));
        fprintf(stderr,
                "Can't determine the Home of LiteSpeed Web Server, exit!\n");
        return 1;
    }

    mkdir(DEFAULT_TMP_DIR,  0755);

    if (testRunningServer() != 0)
        return 2;


    if (m_pServer->configServerBasics(0, m_pBuilder->getRoot()))
        return 1;

    if (!MainServerConfig::getInstance().getDisableLogRotateAtStartup())
        LOG4CXX_NS::LogRotate::roll(HttpLog::getErrorLogger()->getAppender(),
                                    procConfig.getUid(),
                                    procConfig.getGid(), 1);

    if (procConfig.getUid() <= 10 || procConfig.getGid() < 10)
    {
        MainServerConfig  &MainServerConfigObj =  MainServerConfig::getInstance();
        LS_ERROR("It is not allowed to run LiteSpeed web server on behalf of a "
                 "privileged user/group, user id must not be "
                 "less than 50 and group id must not be less than 10."
                 "UID of user '%s' is %d, GID of group '%s' is %d. "
                 "Please fix above problem first!",
                 MainServerConfigObj.getUser(), procConfig.getUid(),
                 MainServerConfigObj.getGroup(), procConfig.getGid());
        return 1;
    }
    changeOwner();

    plainconf::flushErrorLog();
    LS_NOTICE("Loading %s ...", HttpServerVersion::getVersion());
    LS_NOTICE("Using [%s]", SSLeay_version(SSLEAY_VERSION));

    if (!m_noDaemon)
    {
        if (Daemonize::daemonize(1, 1))
            return 3;
        LS_DBG_L("Daemonized!");
#ifndef RUN_TEST
        Daemonize::close();
#endif
    }

    enableCoreDump();


    if (testRunningServer() != 0)
        return 2;
    m_pid = getpid();
    if (m_pidFile.writePid(m_pid))
        return 2;


    startAdminSocket();
    ret = config();
    if (ret)
    {
        LS_ERROR("Fatal error in configuration, exit!");
        fprintf(stderr, "[ERROR] Fatal error in configuration, shutdown!\n");
        return ret;
    }
    removeOldRtreport();
    {
        char achBuf[8192];

        if (procConfig.getChroot() != NULL)
        {
            PidFile pidfile;
            ConfigCtx::getCurConfigCtx()->getAbsolute(achBuf, PID_FILE, 0);
            pidfile.writePidFile(achBuf, m_pid);
        }
    }

#if defined(__FreeBSD__)
    //setproctitle( "%s", "lshttpd" );
#else
    argv[1] = NULL;
    strcpy(argv[0], "openlitespeed (lshttpd - main)");
#endif
    //if ( !m_noCrashGuard && ( m_pBuilder->getCrashGuard() ))
    s_iCpuCount = PCUtil::getNumProcessors();

    //Server init done
    if (GlobalServerSessionHooks->isEnabled(LSI_HKPT_MAIN_INITED))
        GlobalServerSessionHooks->runCallbackNoParam(LSI_HKPT_MAIN_INITED, NULL);

    if (!m_noCrashGuard && (MainServerConfig::getInstance().getCrashGuard()))
    {
        if (guardCrash())
            return 8;
        m_pidFile.closePidFile();
    }
    else
    {
        HttpServerConfig::getInstance().setProcNo(1);
        allocatePidTracker();
        m_pServer->initAdns();
        m_pServer->enableAioLogging();
        if ((HttpServerConfig::getInstance().getUseSendfile() == 2)
            && (m_pServer->initAioSendFile() != 0))
            return LS_FAIL;
    }
    //if ( fcntl( 5, F_GETFD, 0 ) > 0 )
    //    printf( "find it!\n" );
    if (getuid() == 0)
    {
        if (m_pServer->changeUserChroot() == -1)
            return LS_FAIL;
        if (procConfig.getChroot() != NULL)
            m_pServer->offsetChroot();
    }

    if (1 == HttpServerConfig::getInstance().getProcNo())
        ExtAppRegistry::runOnStartUp();

    if (GlobalServerSessionHooks->isEnabled(LSI_HKPT_WORKER_INIT))
        GlobalServerSessionHooks->runCallbackNoParam(LSI_HKPT_WORKER_INIT,
                NULL);


    return 0;
}
Esempio n. 8
0
static int raspberrypiISR(int pin, int mode) {
	int i = 0, fd = 0, match = 0, count = 0;
	const char *sMode = NULL;
	char path[35], c, line[120];
	FILE *f = NULL;

	if(raspberrypiValidGPIO(pin) != 0) {
		wiringXLog(LOG_ERR, "raspberrypi->isr: Invalid pin number %d", pin);
		return -1;
	}

	pinModes[pin] = SYS;

	if(mode == INT_EDGE_FALLING) {
		sMode = "falling" ;
	} else if(mode == INT_EDGE_RISING) {
		sMode = "rising" ;
	} else if(mode == INT_EDGE_BOTH) {
		sMode = "both";
	} else {
		wiringXLog(LOG_ERR, "raspberrypi->isr: Invalid mode. Should be INT_EDGE_BOTH, INT_EDGE_RISING, or INT_EDGE_FALLING");
		return -1;
	}

	sprintf(path, "/sys/class/gpio/gpio%d/value", pinToGpio[pin]);
	fd = open(path, O_RDWR);

	if(fd < 0) {
		if((f = fopen("/sys/class/gpio/export", "w")) == NULL) {
			wiringXLog(LOG_ERR, "raspberrypi->isr: Unable to open GPIO export interface: %s", strerror(errno));
			return -1;
		}

		fprintf(f, "%d\n", pinToGpio[pin]);
		fclose(f);
	}

	sprintf(path, "/sys/class/gpio/gpio%d/direction", pinToGpio[pin]);
	if((f = fopen(path, "w")) == NULL) {
		wiringXLog(LOG_ERR, "raspberrypi->isr: Unable to open GPIO direction interface for pin %d: %s", pin, strerror(errno));
		return -1;
	}

	fprintf(f, "in\n");
	fclose(f);

	sprintf(path, "/sys/class/gpio/gpio%d/edge", pinToGpio[pin]);
	if((f = fopen(path, "w")) == NULL) {
		wiringXLog(LOG_ERR, "raspberrypi->isr: Unable to open GPIO edge interface for pin %d: %s", pin, strerror(errno));
		return -1;
	}

	if(strcasecmp(sMode, "none") == 0) {
		fprintf(f, "none\n");
	} else if(strcasecmp(sMode, "rising") == 0) {
		fprintf(f, "rising\n");
	} else if(strcasecmp(sMode, "falling") == 0) {
		fprintf(f, "falling\n");
	} else if(strcasecmp (sMode, "both") == 0) {
		fprintf(f, "both\n");
	} else {
		wiringXLog(LOG_ERR, "raspberrypi->isr: Invalid mode: %s. Should be rising, falling or both", sMode);
		return -1;
	}
	fclose(f);

	if((f = fopen(path, "r")) == NULL) {
		wiringXLog(LOG_ERR, "raspberrypi->isr: Unable to open GPIO edge interface for pin %d: %s", pin, strerror(errno));
		return -1;
	}

	match = 0;
	while(fgets(line, 120, f) != NULL) {
		if(strstr(line, sMode) != NULL) {
			match = 1;
			break;
		}
	}
	fclose(f);

	if(match == 0) {
		wiringXLog(LOG_ERR, "raspberrypi->isr: Failed to set interrupt edge to %s", sMode);
		return -1;
	}

	sprintf(path, "/sys/class/gpio/gpio%d/value", pinToGpio[pin]);
	if((sysFds[pin] = open(path, O_RDONLY)) < 0) {
		wiringXLog(LOG_ERR, "raspberrypi->isr: Unable to open GPIO value interface: %s", strerror(errno));
		return -1;
	}
	changeOwner(path);

	sprintf(path, "/sys/class/gpio/gpio%d/edge", pinToGpio[pin]);
	changeOwner(path);

	ioctl(fd, FIONREAD, &count);
	for(i=0; i<count; ++i) {
		read(fd, &c, 1);
	}
	close(fd);

	return 0;
}