Global message queue id changes on msgrcv function [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
Improve this question
There are 2 message queues and 2 queue ids. When I call msgrcv, the queue id that is not associated with this msgrcv call gets altered. Its declared globally. When I make it static global the value does not change. But I need to know the root cause of this issue. The code snippet is below
int xmlrpcqid;
rtspbuf rpcbuf;
int statusqid;
msqbuf sbuf;
static void *getXmlResponse(void *arg);
printf ("*******msg qid position 1 = %d\n", statusqid);
while(!eventLoopWatchVariable)
{
printf ("*******msg qid position 2 = %d \n", statusqid);
if(msgrcv(xmlrpcqid, &rpcbuf, sizeof(rtspbuf), 0, MSG_NOERROR) < 0)
{
printf("msgrcv Fail \n");
}
else {
printf ("*******msg qid 3 = %d \n", statusqid);
}
}
// not adding remaining code
}
int main {
if ((xmlrpcqid = msgget (1256, IPC_CREAT | 0666 )) < 0)
printf ("login msgget failed\n");
if ((statusqid = msgget (1234, IPC_CREAT | 0666 )) < 0)
printf ("status msgget failed\n");
pthread_t xmlResponse_thr = -1;
if(pthread_create(&xmlResponse_thr, NULL, getXmlResponse, NULL))
{
printf("pthread_create failed for getXmlResponse\n");
return -1;
}
}

Related

Parameter passed to pthread changes [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i don't know why parameters passed to pthread changes automatically.
i'll show you my code
the debug result is this :
begin of while while() =begin= 0 ===
params: a84a5310
->socket: a84a4e70
->connection: a84a52d0(4)
->webserver: a84a4ea0(0)
===== begin of while while() =end====
===== end of while() =begin= 1709692672 ===
params: a84a5310
->socket: a84a4e70
->connection: a84a52d0(4)
->webserver: a84a4ea0(0)
===== end of while() =end====
Accepting...
===== in thread() =begin= 1709692672 ===
params: aacf4240//wrong expected a84a5310
->socket: a84a5310 //wrong expected a84a4e70
->connection: a84a4ea0(0)//wrong expected a84a52d0(4)
->webserver: 65e7d700(0) //wrong expected a84a4ea0(0)
===== in thread() =end====
this is struct that i pass to the thread :
typedef struct InfoServer{
ServerTCP* server;
WebServer* webserver;
ConnServer* serverConn;
}ServerInfo;
this is debug function :
void* show_params(ServerInfo* params, char* where, int n) {
printf("===== %s =begin= %d ===\n",where, n);
printf("params: %x\n",
params);
printf("->socket: %x\n",
params->server);
printf("->connection: %x(%d)\n",
params->serverConn, params->serverConn->getConn());
printf("->webserver: %x(%d)\n",
params->webserver);
printf("===== %s =end====\n",where);
fflush(stdout);
}
this is main function :
void *manageConnection(void *serverConn);
void fineRichiesta(ServerInfo * serverinformation);
int main (int argc , char* argv[]){
if(argc!=2){
printf("USAGE: %S , PORT \n",argv[0]);
fflush(stdout);
exit(1);
}
int port = atoi(argv[1]);
ServerTCP* tserver= new ServerTCP(port);
WebServer* webserv= new WebServer();
bool exit = false;
while( true ){
pthread_t manageConnection_thread;
ConnServer* serverC= tserver->acceptConn();
ServerInfo* serverinfo = (ServerInfo*) malloc(sizeof(ServerInfo));
serverinfo->server = tserver;
serverinfo->webserver = webserv;
serverinfo->serverConn =serverC;
show_params(serverinfo,"starting in while()",0);
if(pthread_create(&manageConnection_thread,
NULL,
manageConnection,
(void*) &serverinfo)) {
error("Error Creating server");
}
show_params(serverinfo,"end of while()", manageConnection_thread);
}
delete( tserver);
delete( webserv);
return 0;
}
here is thread code :
void *manageConnection(void * serverinformation){
ServerInfo *serverinfo = (ServerInfo*)serverinformation;
show_params(serverinfo,"in thread()", pthread_self());
ConnServer * srvConn = serverinfo->serverConn;
char* answ;
if(srvConn){
answ = (char*)srvConn->riceviServer();
}
else {
printf("im in else\n");
fflush(stdout);
}
if(!answ){
printf("finerichiesta\n");
fflush(stdout);
fineRichiesta(serverinfo);
}
printf("answ: %s\n", answ);
fflush(stdout);
char* file = serverinfo->webserver->getFile(answ);
srvConn->inviaServer(file);
fineRichiesta( serverinfo);
free(answ);
return NULL;
}
function :
void fineRichiesta(ServerInfo * serverinformation){
printf("im in fine richiesta\n");
fflush(stdout);
serverinformation->server->disconnect(serverinformation->serverConn);
free(serverinformation);
pthread_exit(NULL);
}
Any help would be amazing , im getting crazy -
i dont know if my question is best formulated -
thanks in advance ...
Your pthread_create call is passing the wrong void * value to the thread start routine manageConnection.
It is passing the address of the serverInfo variable:
pthread_create(&manageConnection_thread,
NULL,
manageConnection,
(void*) &serverinfo)
It should be passing value of the serverInfo variable, which points to a filled in ServerInfo:
pthread_create(&manageConnection_thread,
NULL,
manageConnection,
(void*) serverinfo) // <-- note: ampersand removed here

Redirecting console I/O: SetHandleInformation fails with invalid handle [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to figure out how to adapt this sample in order to redirect the output of a console window to a text box inside another process.
Unfortunately, it seems that the reader will never receive any input.
Further debugging shows that the call to SetHandleInformation always aborts with Error 6: Invalid Handle. The value of hPipeOutRd does not look bad, it is something like 0x00000244.
This reproduces the matter:
int main(int argc, char *argv[])
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
int result = 0;
HANDLE hPipeOutRd = INVALID_HANDLE_VALUE; // This end is passed to the pipe reader
HANDLE hPipeOutWr = INVALID_HANDLE_VALUE; // This end is passed to the child process
if ( result == 0 && !::CreatePipe( &hPipeOutRd, &hPipeOutWr, &sa, 4096 ) )
{
result = -1;
printf("Error: %u\r\n", GetLastError() );
}
if ( result == 0 && !::SetHandleInformation( &hPipeOutRd, HANDLE_FLAG_INHERIT, 0 ) ) // This fails with invalid handle
{
result = -1;
printf("Error: %u\r\n", GetLastError() );
}
return result;
}
Any ideas why?
A HANDLE is already a pointer. You don't take its address unless it is an out parameter.
Just take the & out of your SetHandleInformation call.

MD5 function not functioning properly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This is my current MD5 function. When used on windows 8.1 it always returns a value, but when run on windows 7 it only returns a value about 50% of the time. That is pretty peculiar to me. Any ideas?
It turns out that it is dodgy on both win7 and win8. Apparently the call to CryptGetHashParam sometimes fails with ERROR_MORE_DATA.
std::string MD5(std::string input)
{
HCRYPTPROV CryptProv;
HCRYPTHASH CryptHash;
BYTE BytesHash[33];//!
DWORD dwHashLen;
std::string final;
if (CryptAcquireContext(&CryptProv,NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
{
if (CryptCreateHash(CryptProv, CALG_MD5, 0, 0, &CryptHash))
{
if (CryptHashData(CryptHash, (BYTE*)input.c_str(), input.length(), 0))
{
if (CryptGetHashParam(CryptHash, HP_HASHVAL, BytesHash, &dwHashLen, 0))
{
final.clear();
std::string hexcharset = "0123456789ABCDEF";
for (int j = 0; j < 16; j++)
{
final += hexcharset.substr(((BytesHash[j] >> 4) & 0xF), 1);
final += hexcharset.substr(((BytesHash[j]) & 0x0F), 1);
}
}
}
}
}
CryptDestroyHash(CryptHash);
CryptReleaseContext(CryptProv, 0);
return final;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *pp = "derp";
std::string derp = MD5(std::string(pp));
printf("%s\n", derp.c_str());
system("pause");
return 0;
}
I have a fantastic idea: add else clauses to those conditions so that you know which one is failing. Put output in those clauses so that you can see detail on the error conditions. Then you will know what is wrong with your function.

USB Mass Storage linux [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new in linux device driver. I wan to write a C/C++ code to perform file transfer from raspberry pi to usb flash drive. I having difficult for the starting point, so i try on libusb for HID device sample code from signal11 and the code works fine for detecting my optical mouse with its device ID. Then i try to obtain usb flash drive vendor id somehow it give me very wired number. Finally i come out with a very silly try out by writing a bash script for cp a file to usb flash drive and activate the script in C++ and it works but i feel it is not a proper way to do it. Then i start with SCSI protocol and i very hard to understand how it works.Any guideline is appreciated.
int scsi_get_serial(int fd, void *buf, size_t buf_len) {
// we shall retrieve page 0x80 as per http://en.wikipedia.org/wiki/SCSI_Inquiry_Command
unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, buf_len, 0};
unsigned char sense[32];
struct sg_io_hdr io_hdr;
int result;
memset(&io_hdr, 0, sizeof (io_hdr));
io_hdr.interface_id = 'S';
io_hdr.cmdp = inq_cmd;
io_hdr.cmd_len = sizeof (inq_cmd);
io_hdr.dxferp = buf;
io_hdr.dxfer_len = buf_len;
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
io_hdr.sbp = sense;
io_hdr.mx_sb_len = sizeof (sense);
io_hdr.timeout = 5000;
result = ioctl(fd, SG_IO, &io_hdr);
if (result < 0)
return result;
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
return 1;
return 0;
}
int main(int argc, char** argv) {
//char *dev = "/dev/sda";
char *dev = "/dev/sg2";
char scsi_serial[255];
int rc;
int fd;
fd = open(dev, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
perror(dev);
}
memset(scsi_serial, 0, sizeof (scsi_serial));
rc = scsi_get_serial(fd, scsi_serial, 255);
// scsi_serial[3] is the length of the serial number
// scsi_serial[4] is serial number (raw, NOT null terminated)
if (rc < 0) {
printf("FAIL, rc=%d, errno=%d\n", rc, errno);
} else
if (rc == 1) {
printf("FAIL, rc=%d, drive doesn't report serial number\n", rc);
} else {
if (!scsi_serial[3]) {
printf("Failed to retrieve serial for %s\n", dev);
return -1;
}
printf("Serial Number: %.*s\n", (size_t) scsi_serial[3], (char *) & scsi_serial[4]);
}
close(fd);
return (EXIT_SUCCESS);
}
I get this serial number: 00/1F
Then i try write this in test.sh
cp /home/Desktop/stl4.pdf /media/mini_flash
and run system("./test.sh") in C++
The question seems contradictory, at first you say you want to copy a file using a kernel driver, which seems strange to say the least. Then you say you use libusb, which is an userspace library. Then you say that you try to execute a shell script with cp.
Maybe what you want is simply a code snippet that copies a file form an userspace C/C++ program? Try one of these snippets.
In detail, if all you want to do is a C++ equivalent of cp /home/Desktop/stl4.pdf /media/mini_flash, then this is enough:
ifstream in("/home/Desktop/stl4.pdf",ios::binary);
ofstream out("/media/mini_flash/stl4.pdf",ios::binary);
out<<in.rdbuf();
in.close();
out.close();

C++ code to find BSSID OF associated network [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Want to know the ESSID of wireless network via C++ in UBUNTU
Hello I have written the following code which is a part of a project. It is used to find the ESSID of the current associated network. But it has a flaw that it also the displays the ESSID of the network with which I am not associated i.e. if I try to associate myself with a wireless n/w and if it is unsuccessfull i.e. NO DHCP OFFERS ARE RECEIVED, then also it will display the that ESSID with which I have made my attempt.
Is it possible to find the BSSID of current associated wireless network as it is the only way with which I can mark b/w associated and non associated, e.g. with an ioctl call?
int main (void)
{
int errno;
struct iwreq wreq;
CStdString result = "None";
int sockfd;
char * id;
char ESSID[20];
memset(&wreq, 0, sizeof(struct iwreq));
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
fprintf(stderr, "Cannot open socket \n");
fprintf(stderr, "errno = %d \n", errno);
fprintf(stderr, "Error description is : %s\n",strerror(errno));
return result ;
}
CLog::Log(LOGINFO,"Socket opened successfully");
FILE* fp = fopen("/proc/net/dev", "r");
if (!fp)
{
// TBD: Error
return result;
}
char* line = NULL;
size_t linel = 0;
int n;
char* p;
int linenum = 0;
while (getdelim(&line, &linel, '\n', fp) > 0)
{
// skip first two lines
if (linenum++ < 2)
continue;
p = line;
while (isspace(*p))
++p;
n = strcspn(p, ": \t");
p[n] = 0;
strcpy(wreq.ifr_name, p);
id = new char[IW_ESSID_MAX_SIZE+100];
wreq.u.essid.pointer = id;
wreq.u.essid.length = 100;
if ( ioctl(sockfd,SIOCGIWESSID, &wreq) == -1 ) {
continue;
}
else
{
strcpy(ESSID,id);
return ESSID;
}
free(id);
}
free(line);
fclose(fp);
return result;
}
Note: Since this question seems to be duplicated in two places, I'm repeating my answer here as well.
You didn't mention whether you were using an independent basic service set or not (i.e., an ad-hoc network with no controlling access point), so if you're not trying to create an ad-hoc network, then the BSSID should be the MAC address of the local access point. The ioctl() constant you can use to access that information is SIOCGIWAP. The ioctl payload information will be stored inside of your iwreq structure at u.ap_addr.sa_data.