How to get task port of SpringBoard in iOS7 (Jailbroken)? - ios

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.

Related

UART Interrupt problem while performing real-time image processing with camera by using opencv in c++ (I worked on Ubuntu 18.04, Linux Kernel 5x)

I write a code to process real time video by using camera. I can run this code within any error by itself but I want to implement a UART interrupt. Basically I aim that when I take a data from UART, other processes will be interrupted and after reading data, processes will be continue.
Here my UART interrupt implemented code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
void signal_handler_IO(int status);
VideoCapture vid("/dev/video0", CAP_V4L2);
int n;
int fd;
int connected;
struct termios termAttr;
struct sigaction saio;
const int fps = 20;
int main(int argc, char *argv[])
{
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
Mat frame;
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyO1\n");
exit(1);
}
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL; //
sigaction(SIGIO,&saio,NULL);
fcntl(fd, F_SETFL, FNDELAY|FASYNC);
fcntl(fd, F_SETOWN, getpid());
tcgetattr(fd,&termAttr);
cfsetispeed(&termAttr,B9600);
cfsetospeed(&termAttr,B9600);
termAttr.c_cflag &= ~PARENB; // --> Parity Enable
termAttr.c_cflag &= ~CSTOPB; // -->two stop bit else one
termAttr.c_cflag &= ~CSIZE; // Character Size (C5, C6...)
termAttr.c_cflag |= CS8; // Charachter size 8 bit
termAttr.c_cflag |= (CLOCAL | CREAD);
//CLOCAL --> Ignore modem status lines. ¦ CREAD --> Enable receiver.
termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
termAttr.c_oflag &= ~OPOST; // OPOST --> Post-process output
tcsetattr(fd, TCSANOW, &termAttr);
printf("UART1 configured....\n");
connected = 1;
if (!vid.isOpened())
{
return -1;
}
vid.set(CAP_PROP_FRAME_WIDTH, 1280);
vid.set(CAP_PROP_FRAME_HEIGHT, 720);
while (vid.read(frame))
{
imshow("webcam", frame);
if (waitKey(1000/fps) >= 0)
break;
}
destroyAllWindows();
vid.release();
close(fd);
exit(0);
}
void signal_handler_IO (int status)
{
printf("received data from UART.\n");
}
Now let's get to the main problem:
When I implement UART interrupt to this code, my captured video was stopped by the core (I takes Core Dumped Error). I guess that after process interrupted and when it will continue again, compiler is try to set again the video properties which I defined as
(vid.set(CAP_PROP_FRAME_WIDTH, 1280);)
(vid.set(CAP_PROP_FRAME_HEIGHT, 720);)
so I think I got such an error.
I think as I mentioned because when I deleted these size setting commands, code run very well with UART interrupt.
I try to define these commands globally but I cant do this. I take "vid does not name a type." error.
Thus, I can't solve this problem. I am not sure about why this problem occur and I don't know how it will be solved.
First, you cannot invoke printf(3) in a signal handler. Printf interacts with stdio, which in turn interacts with malloc(3); neither of these frameworks are signal safe. So, change your printf() to a write(2), which is safe.
That is unlikely to be your problem.
You didn't post the source to the implementation of vid, and in particular, how does vid.read deal with signals (ie EINTR)? Does it handle them correctly? Well? Perhaps return 0 if it is interrupted?
You will likely need to vet vid., so you should have a test candidate that gets a timer based signal over random intervals to gain confidence that it works.

pthread error code 3025 (ENOENT) on the as400/IBM i?

When I have the following C source code, which is running on an IBM i Midrange, then I get a non-zero result from pthread_create, specifically 3025, which is ENOENT (No such path or directory), which doesn't make any sense to me. Anyone have any thoughts on what the error actually means in this context.
#define _MULTI_THREADED
#define _XOPEN_SOURCE 520
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
void* workerThread(void* parm) {
// Do some work here
pthread_exit(NULL);
}
int main(int argc, char* argv[]) {
pthread_t t;
int rc;
rc = pthread_create(&t, NULL, workerThread, NULL);
if (rc != 0) {
char *msg = strerror(errno);
perror("pthread_create failed");
}
// Other code here
return 0;
}
pthread_create doesn't set errno. You should be checking strerror of rc.
http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_create.html
char *msg = strerror(rc);

Set pull up/down resistor on Beaglebone Black

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.

glib compilation with ios

I am trying to compile glib in ios, i have got an error in gio/tests/appinfo-test.h
#include <stdlib.h>
#include <gio/gio.h>
int
main (int argc, char *argv[])
{
const gchar *envvar;
gint pid_from_env;
envvar = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
g_assert (envvar != NULL);
pid_from_env = atoi (envvar);
g_assert_cmpint (pid_from_env, ==, getpid ());
envvar = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
g_assert_cmpstr (envvar, ==, SRCDIR "/appinfo-test.desktop"); //got the error here that "Use of undefined identifier 'SRCDIR' "
return 0;
}
please help me out...Thank you
I can not figure out with given information how you tried to compile the sample code in your ios, but you can add
#define SRCDIR
before main().
The sample code seems to be glib/gio/tests/appinfo-test.c in the source repository. SRCDIR is defined as -DSRCDIR=\""$(srcdir)"\" in the Makefile.am.

BIND ERROR : Address already in use

I am learning socket programming in c, I wrote this simple program to accept connections on port 5072. i connect to it using telnet. This works fine the first time but when i try to run it again immediately it fails showing BIND : Address already in use, but then again starts to work after a minute or so. What am i doing wromg?
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>
int main(){
//variables
int listenfd, clientfd;
socklen_t clilen;
struct sockaddr_in cliaddr,servaddr;
//getsocket
if((listenfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("SOCKET");
exit(0);
}
//prep the servaddr
bzero(&servaddr,sizeof servaddr);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr ("127.0.0.1");
servaddr.sin_port = htons(5072);
//bind
if(bind(listenfd, (struct sockaddr *) &servaddr,sizeof servaddr)<0){
perror("BIND");
exit(0);
}
//listen
if(listen(listenfd,20)<0){
perror("LISTEN");
exit(0);
}
//accept
int counter = 1;
clilen = sizeof cliaddr;
while(counter<3){
clientfd = accept(listenfd,(struct sockaddr *) &cliaddr,&clilen);
if(clientfd == -1){
perror("ACCEPT");
exit(0);
}
if(fork()==0){
close(listenfd);
printf("CONNECTION NO. %d\n",counter);
close(clientfd);
exit(0);
}
counter++;
close(clientfd);
}
close(listenfd);
}
Thanks
You must setsockopt(SO_REUSEADDR)
See this faq for explanation why.
Or simply wait a couple minutes. The TCP/IP stack holds onto the socket for twice the maximum segment lifetime after close,to prevent any stray packets from the first connection from showing up after a second one's established, which will make it all confused.

Resources