can I use shm_open instead of shmget on QNX - qnx

I am new to QNX platform, and we are porting Linux project to QNX. and found code related to shared memory creating in linux using shmget system call. but shmget not present in QNX. and I saw similar call shm_open, I don't know difference b/w both.
My straight question is like, should I use shm_open instead of shmget in QNX platform ? If yes, how ? if not, why not ?

First of all QNX does NOT support the shmget() API.
You will need to use shm_open() instead.
Below is a sample program from online QNX documentation
that demonstrates the proper use of shm_open() on QNX:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
int main( int argc, char** argv )
{
int fd;
unsigned* addr;
/*
* In case the unlink code isn't executed at the end
*/
if( argc != 1 ) {
shm_unlink( "/bolts" );
return EXIT_SUCCESS;
}
/* Create a new memory object */
fd = shm_open( "/bolts", O_RDWR | O_CREAT, 0777 );
if( fd == -1 ) {
fprintf( stderr, "Open failed:%s\n",
strerror( errno ) );
return EXIT_FAILURE;
}
/* Set the memory object's size */
if( ftruncate( fd, sizeof( *addr ) ) == -1 ) {
fprintf( stderr, "ftruncate: %s\n",
strerror( errno ) );
return EXIT_FAILURE;
}
/* Map the memory object */
addr = mmap( 0, sizeof( *addr ),
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0 );
if( addr == MAP_FAILED ) {
fprintf( stderr, "mmap failed: %s\n",
strerror( errno ) );
return EXIT_FAILURE;
}
printf( "Map addr is 0x%08x\n", addr );
/* Write to shared memory */
*addr = 1;
/*
* The memory object remains in
* the system after the close
*/
close( fd );
/*
* To remove a memory object
* you must unlink it like a file.
*
* This may be done by another process.
*/
shm_unlink( "/bolts" );
return EXIT_SUCCESS;
}
Hope this helps.

Related

readlink() error while reading /proc/self/exefile on QNX

I am working on QNX platform, in which I need to get the path of executable which is running.
I have wrote a small peice of code, which is returning always -1:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
extern int errno;
int main( int argc, char** argv )
{
char buf[512] = {0};
const char mypath[100] = "/proc/self/exefile";
errno = 0;
printf("The value readlink:: %d %s\n",readlink(mypath, buf, 512 ), strerror( errno ));
return( 0 );
}
When I ran above code then I get following output:
The value readlink:: -1 No such file or directory
Am I missing anything?
What needs to be done to get my current exe path in QNX?
In QNX /proc/self/exefile is not a symbolic link; It's a regular file.
Try:
#include <fstream>
#include <iostream>
#include <string>
int main(int argc, char **argv) {
std::ifstream file("/proc/self/exefile");
std::string path;
std::getline(file, path);
std::cout << path << "\n";
return 0;
}

How may I use PCAP or other library to parse layer 3 frame received from TUN?

This is how I receive layer 3 frame from TUN.
Written based on docs:
https://www.kernel.org/doc/Documentation/networking/tuntap.txt
http://backreference.org/2010/03/26/tuntap-interface-tutorial/
Unpacking from http://www.saminiir.com/lets-code-tcp-ip-stack-1-ethernet-arp/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <stdarg.h>
/* buffer for reading from tun/tap interface, must be >= 1500 */
#define BUFSIZE 2000
int tun_alloc(char *dev)
{
struct ifreq ifr;
int fd, err;
if (!dev) {
return -1;
}
memset(&ifr, 0, sizeof(ifr));
/* Flags: IFF_TUN - TUN device (no Ethernet headers)
* IFF_TAP - TAP device
*
* IFF_NO_PI - Do not provide packet information
* IFF_MULTI_QUEUE - Create a queue of multiqueue device
*/
ifr.ifr_flags = IFF_TUN;
strcpy(ifr.ifr_name, dev);
if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
return fd;
err = ioctl(fd, TUNSETIFF, (void *)&ifr);
if (err) {
close(fd);
goto err;
}
strcpy(dev, ifr.ifr_name);
return fd;
err:
close(fd);
return err;
}
int main() {
char *tun_name;
tun_name = malloc(IFNAMSIZ);
tun_name[0] = '\0';
int tun_fd = tun_alloc(tun_name);
if (tun_fd < 0) {
puts("Try as root");
exit(1);
}
if (ioctl(tun_fd, TUNSETPERSIST, 0) < 0) {
perror("disabling TUNSETPERSIST");
exit(1);
}
printf("Set interface '%s' nonpersistent\n", tun_name);
struct layer3_frame
{
uint16_t flags;
uint16_t proto;
uint8_t version;
unsigned char payload[];
} __attribute__((packed));
int nread;
char buffer[BUFSIZE];
while(1) {
nread = read(tun_fd, buffer, sizeof(buffer));
if(nread < 0) {
perror("Reading from interface");
close(tun_fd);
exit(1);
}
/* Do whatever with the data */
printf("Read %d bytes from device %s\n", nread, tun_name);
struct layer3_frame* l3f = (struct layer3_frame*)(buffer);
printf("FLAGS %d, PROTO %d, VER %d", l3f->flags, l3f->proto, l3f->version);
// E.g. FLAGS 0, PROTO 56710, VER 96
// Why PROTO is not 4 or 6, why VER is not 4 or 6?
// MAIN: HOW TO USE PCAP TO PARSE l3f FURTHER
// AND GET INFO UP TO SNI (server name indication), e.g.
}
return 0;
}
To play:
gcc index.c
sudo ./a.out
sudo ip link set tun0 up
PCAP usually is not used for parsing packets.
You may use however:
#include <netinet/ip.h>
#include <netinet/ip6.h>
//...
struct layer3_frame
{
uint16_t flags; // FLAGS from TUN
uint16_t proto; // PRPTO from TUN
unsigned char payload[]; // FRAME/PACKET
} __attribute__((packed));
const struct ip* ippacket = (struct ip*)(l3p->payload);
printf("Version is %d", ippacket->ip_v)
3. About strange values for PROTO like 56710 try printf("FFF: %x", ntohs(56710)) you will get 86dd which you may look up at https://en.wikipedia.org/wiki/EtherType

capture’ was not declared in this scope capture = cvCaptureFromCAM( 0 ); how to fix it

I was following a tutorial on web for an OpenCV library to detect eyes
when i compile it this error appears I tried to fix it but didn't find a
solution.
main.cpp:59:1: error: ‘capture’ was not declared in this scope
capture = cvCaptureFromCAM( 0 );
The code is long I put only the part that I think causing this error .
: Full code
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <math.h>
#include "constants.h"
#include "findEyeCenter.h"
#include "findEyeCorner.h"
/** Function Headers */
void detectAndDisplay( cv::Mat frame );
int main( )
{
cv::Mat frame;
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade, please change face_cascade_name in source code.\n"); return -1; };
createCornerKernels();
ellipse(skinCrCbHist, cv::Point(113, 155.6), cv::Size(23.4, 15.2),
43.0, 0.0, 360.0, cv::Scalar(255, 255, 255), -1);
capture = cvCaptureFromCAM( 0 );
if( capture)
{
while( true )
{
frame = cvQueryFrame( capture );
// mirror it
imshow("Video",frame);
cv::flip(frame, frame, 1);
frame.copyTo(debugImage);
// Apply the classifier to the frame
if( !frame.empty() ) {
detectAndDisplay( frame );
}
else {
printf(" --(!) No captured frame -- Break!");
break;
}
imshow(main_window_name,debugImage);
int c = cv::waitKey(10);
if( (char)c == 'c' ) { break; }
if( (char)c == 'f' ) {
imwrite("frame.png",frame);
}
}
}
releaseCornerKernels();
return 0;
}
}
try to use VideoCapture capture(0);

obtaining scsi(including SAS and FC) hardisk model and serial number

I have recently been playing around with some hard drive stuff. Now what I want to do is print out the model and serial number of harddisk. Sata drives are very easy with ioctl. scsi on the other hand I have to send an inquiry command. I found a very helpful site which explains everything and even has a example program: http://tldp.org/HOWTO/archived/SCSI-Programming-HOWTO/SCSI-Programming-HOWTO-24.html
but I only get nothing or gibberish as a result if I print it out. I even had to fix the program as stdlib wasn't included and the function Inquiry returned a local variable. But I have no idea how to fix it...
#define DEVICE "/dev/sdb"
/* Example program to demonstrate the generic SCSI interface */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <scsi/sg.h>
#define SCSI_OFF sizeof(struct sg_header)
static unsigned char cmd[SCSI_OFF + 18]; /* SCSI command buffer */
int fd; /* SCSI device/file descriptor */
/* process a complete scsi cmd. Use the generic scsi interface. */
static int handle_scsi_cmd(unsigned cmd_len, /* command length */
unsigned in_size, /* input data size */
unsigned char *i_buff, /* input buffer */
unsigned out_size, /* output data size */
unsigned char *o_buff /* output buffer */
)
{
int status = 0;
struct sg_header *sg_hd;
/* safety checks */
if (!cmd_len) return -1; /* need a cmd_len != 0 */
if (!i_buff) return -1; /* need an input buffer != NULL */
#ifdef SG_BIG_BUFF
if (SCSI_OFF + cmd_len + in_size > SG_BIG_BUFF) return -1;
if (SCSI_OFF + out_size > SG_BIG_BUFF) return -1;
#else
if (SCSI_OFF + cmd_len + in_size > 4096) return -1;
if (SCSI_OFF + out_size > 4096) return -1;
#endif
if (!o_buff) out_size = 0;
/* generic scsi device header construction */
sg_hd = (struct sg_header *) i_buff;
sg_hd->reply_len = SCSI_OFF + out_size;
sg_hd->twelve_byte = cmd_len == 12;
sg_hd->result = 0;
#if 0
sg_hd->pack_len = SCSI_OFF + cmd_len + in_size; /* not necessary */
sg_hd->pack_id; /* not used */
sg_hd->other_flags; /* not used */
#endif
/* send command */
status = write( fd, i_buff, SCSI_OFF + cmd_len + in_size );
if ( status < 0 || status != SCSI_OFF + cmd_len + in_size ||
sg_hd->result ) {
/* some error happened */
fprintf( stderr, "write(generic) result = 0x%x cmd = 0x%x\n",
sg_hd->result, i_buff[SCSI_OFF] );
perror("");
return status;
}
if (!o_buff) o_buff = i_buff; /* buffer pointer check */
/* retrieve result */
status = read( fd, o_buff, SCSI_OFF + out_size);
if ( status < 0 || status != SCSI_OFF + out_size || sg_hd->result ) {
/* some error happened */
fprintf( stderr, "read(generic) result = 0x%x cmd = 0x%x\n",
sg_hd->result, o_buff[SCSI_OFF] );
fprintf( stderr, "read(generic) sense "
"%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",
sg_hd->sense_buffer[0], sg_hd->sense_buffer[1],
sg_hd->sense_buffer[2], sg_hd->sense_buffer[3],
sg_hd->sense_buffer[4], sg_hd->sense_buffer[5],
sg_hd->sense_buffer[6], sg_hd->sense_buffer[7],
sg_hd->sense_buffer[8], sg_hd->sense_buffer[9],
sg_hd->sense_buffer[10], sg_hd->sense_buffer[11],
sg_hd->sense_buffer[12], sg_hd->sense_buffer[13],
sg_hd->sense_buffer[14], sg_hd->sense_buffer[15]);
if (status < 0)
perror("");
}
/* Look if we got what we expected to get */
if (status == SCSI_OFF + out_size) status = 0; /* got them all */
return status; /* 0 means no error */
}
#define INQUIRY_CMD 0x12
#define INQUIRY_CMDLEN 6
#define INQUIRY_REPLY_LEN 96
#define INQUIRY_VENDOR 8 /* Offset in reply data to vendor name */
/* request vendor brand and model */
static unsigned char *Inquiry ( void )
{
unsigned char Inqbuffer[ SCSI_OFF + INQUIRY_REPLY_LEN ];
unsigned char cmdblk [ INQUIRY_CMDLEN ] =
{ INQUIRY_CMD, /* command */
0, /* lun/reserved */
0, /* page code */
0, /* reserved */
INQUIRY_REPLY_LEN, /* allocation length */
0 };/* reserved/flag/link */
memcpy( cmd + SCSI_OFF, cmdblk, sizeof(cmdblk) );
/*
* +------------------+
* | struct sg_header | <- cmd
* +------------------+
* | copy of cmdblk | <- cmd + SCSI_OFF
* +------------------+
*/
if (handle_scsi_cmd(sizeof(cmdblk), 0, cmd,
sizeof(Inqbuffer) - SCSI_OFF, Inqbuffer )) {
fprintf( stderr, "Inquiry failed\n" );
exit(2);
}
return (Inqbuffer + SCSI_OFF);
}
void main( void )
{
fd = open(DEVICE, O_RDWR);
if (fd < 0) {
fprintf( stderr, "Need read/write permissions for "DEVICE".\n" );
exit(1);
}
/* print some fields of the Inquiry result */
printf( "||%s||", Inquiry() + INQUIRY_VENDOR );
}

shared memory father and child in c

David Shwartz helped me alot and now it kinda works...
do you have any idea for more elgant way to parse the input, if the input consists more than 2 numbers to add which need to be processed by the child ? I want the child to get only two integers so that's why I created the shared memory so the father will send the child the result(shared memory) + another integer.
Thank you all.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
volatile int *shared=0;
int shmid;
int main()
{
char line[256];
int readByte;
int fd[2]; //pipe to son, who processes addition
int pid;
shmid=shmget ( IPC_PRIVATE, sizeof(int) , 0600 );
shared=shmat ( shmid, 0 , 0);
if ( pipe(fd) )
{
perror("pipe");
exit(-1);
}
pid=fork();
if (pid!=0) // father
{
close (fd[0]);
readByte=read(0, line, 256);
line[readByte-1]='\0';
printf("%d",readByte);
int arr[2];
int i=0;
int j=0;
int flag=0;
char num[10];
while (i<readByte)
{
if (line[i]=='+' )
{
i++;
j=0;
flag=1;
}
while (line[i]!='+' && line[i]!='\0')
{
num[j]=line[i];
i++;
j++;
}
num[j]='\0';
if (flag==0)
arr[0]=atoi(num);
else
{
arr[1]=atoi(num);
i++;
}
}
printf("first %d\n",arr[0]);
printf("sec %d\n",arr[1]);
write(fd[1], &arr, sizeof(arr));
wait(NULL);
printf ( "%d\n" , *shared );
}
else
// son
{
int arr[2];
int sum;
readByte = read(fd[0], &arr, sizeof(arr));
printf("son printing: %d\n",arr[0]);
printf("son printing: %d\n",arr[1]);
sum =arr[0]+arr[1];
*shared=sum;
close (fd[0]);
shmdt ( (const void *) shared );
}
shmdt ( (const void *) shared );
shmctl ( shmid , IPC_RMID , 0 );
close(fd[1]);
return 0;
}
You throw away the return value of shmat. And you expect shared to be shared, but it's just a regular variable. Also, you need to prevent the compiler from optimizing away accesses to the shared memory. Here it is with all the fatal bugs fixed:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
volatile int *shared;
int shmid;
int main()
{
int s,i;
shmid=shmget ( IPC_PRIVATE, sizeof(int), 0600 );
shared=shmat ( shmid, 0 , 0);
*shared=100;
printf ( "%d\n" , *shared);
if ( fork()==0 ) // son
{
*shared=1000;
shmdt ( (const void *) shared );
}
else // father
{
wait ( &s );
printf ( "%d\n" , *shared);
shmdt ( (const void *) shared );
shmctl ( shmid , IPC_RMID , 0 );
}
return 0;
}

Resources