Call to ioctl() results in errno indicating "Bad address" - c

I'm getting the following error:
wlan_config_vap_priv_int vap lan0 cmd cpauth val 0(0) error: Bad address
Which is from the print in wlan_config_vap_priv_int. Does anyone know what might be causing the "Bad address" error?
From why does ioctl return "bad address" I suspect the culprit may be the ioctl call, but I don't see why.
int wlan_config_vap_priv(char *vap, char *cmd, char * val) {
int fd, ret;
struct ifreq ifr;
param_t fp;
strncpy(ifr.ifr_name, vap, IFNAMSIZ);
strncpy(fp.cmd, cmd, sizeof(fp.cmd));
strncpy(fp.val, val, sizeof(fp.val));
ifr.ifr_data = (void *) &fp;
printf("%s:%d: config vap %s priv %s=%s\n", __func__, __LINE__, vap, cmd, val);
if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
return -1;
}
ret = ioctl(fd, SIOCSPARAM, &ifr);
close(fd);
return (ret);
}
int wlan_config_vap_priv_int(uint8_t rId, char *vap, char *cmd, int val) {
char buf[32];
int ret;
snprintf(buf, sizeof(buf), "%d", val);
ret = wlan_config_vap_priv(vap, cmd, buf);
if (ret < 0) {
CW_LOG_ERR("%s vap %s cmd %s val %s(%d) error: %s\n",
__FUNCTION__, vap, cmd, buf, val, strerror(errno));
}
return ret;
}

Your information is incomplete because you do not have the Kernel part for SIOCSPARAM.
However according to http://man7.org/linux/man-pages/man2/ioctl.2.html, this error happens when argp references an inaccessible memory area
EFAULT argp references an inaccessible memory area.
Your source strings do not have size attached to them. Using strncpy on this string is unsafe and could be the culprit. You can try using something like snprintf.

Related

Unix system programming: get a network identifier to be passed to getaddrinfo

I am following on a book the code in C to build a server using system calls.
The main function is the following:
int main(int argc, char* argv[])
{
printf("entered main\n");
struct addrinfo *ailist, *aip, hint;
int sockfd, err, n;
char *host;
if (argc != 1)
{
printf("usage: ruptimed\n");
exit(1);
}
if ((n=sysconf(_SC_HOST_NAME_MAX))<0)
{
n = HOST_NAME_MAX;
}
if((host = malloc(n)) == NULL)
{
printf("malloc error\n");
exit(1);
}
if (gethostname(host, n)<0)
{
printf("gethostname error\n");
exit(1);
}
printf("host: %s\n", host);
printf("Daemonizing\n");
int res = daemonize("ruptimed");
printf("%d\n", res);
printf("Daemonized\n");
memset(&hint, 0, sizeof(hint)); //set to 0 all bytes
printf("hint initialized\n");
hint.ai_flags = AI_CANONNAME;
hint.ai_socktype = SOCK_STREAM;
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
printf("getting addresses\n");
if((err = getaddrinfo(host, "ruptime", &hint, &ailist))!=0)
{
printf("error %s\n", gai_strerror(err));
syslog(LOG_ERR, "ruptimed: getaddrinfo error %s", gai_strerror(err));
exit(1);
}
printf("Got addresses\n");
for (aip = ailist; aip!=NULL; aip = aip->ai_next)
{
if ((sockfd = initserver(SOCK_STREAM, aip->ai_addr, aip->ai_addrlen, QLEN))>=0)
{
printf("starting to serve\n");
serve(sockfd);
exit(0);
}
}
exit(1);
}
My problem is when getting the host name with gethostname and then using it with getaddrinfo.
Running the code on OSX I get a name such as pippo's-MacBook-pro.local memorized in the host char pointer variable. Passing this to getaddrinfo results in the error: nodename nor servname provided, or not known.
I was expecting the gethostname to return a local IP or a local network identifier (even localhost would be good for learning). I doubt such name can be used to identify a (local) server without proper settings on the machine (plus I cannot remember the book saying anything about setting the host name).
How can I get a network identifier (such as the local IP) to be passed to getaddrinfo?
If I would like to use gethostname what changes or settings should be performed?
CODE
server.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h> //_SC_HOST_NAME_MAX
#include<string.h>
#include<netdb.h> //Here are defined AF_INET and the others of the family
#include<syslog.h> //LOG_ERR
#include<errno.h> //errno
#include <sys/types.h>
#include"utilities.h"
#include "error.h"
#define BUFLEN 128
#define QLEN 10
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 156
#endif
int initserver(int type, const struct sockaddr *addr, socklen_t alen, int qlen);
void serve(int sockfd);
int main(int argc, char* argv[])
{
printf("entered main\n");
struct addrinfo *ailist, *aip, hint;
int sockfd, err, n;
char *host;
if (argc != 1)
{
printf("usage: ruptimed\n");
exit(1);
}
if ((n=sysconf(_SC_HOST_NAME_MAX))<0)
{
n = HOST_NAME_MAX;
}
if((host = malloc(n)) == NULL)
{
printf("malloc error\n");
exit(1);
}
if (gethostname(host, n)<0)
{
printf("gethostname error\n");
exit(1);
}
printf("host: %s\n", host);
printf("Daemonizing\n");
int res = daemonize("ruptimed");
printf("%d\n", res);
printf("Daemonized\n");
memset(&hint, 0, sizeof(hint)); //set to 0 all bytes
printf("hint initialized\n");
hint.ai_flags = AI_CANONNAME;
hint.ai_socktype = SOCK_STREAM;
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
printf("getting addresses\n");
if((err = getaddrinfo(host, "ruptime", &hint, &ailist))!=0)
{
printf("error %s\n", gai_strerror(err));
syslog(LOG_ERR, "ruptimed: getaddrinfo error %s", gai_strerror(err));
exit(1);
}
printf("Got addresses\n");
for (aip = ailist; aip!=NULL; aip = aip->ai_next)
{
if ((sockfd = initserver(SOCK_STREAM, aip->ai_addr, aip->ai_addrlen, QLEN))>=0)
{
printf("starting to serve\n");
serve(sockfd);
exit(0);
}
}
exit(1);
}
void serve(int sockfd)
{
int clfd;
FILE *fp;
char buf[BUFLEN];
set_cloexec(sockfd);
for(;;)
{
/*After listen, the socket can receive connect requests. accept
retrieves a connect request and converts it into a connection.
The file returned by accept is a socket descriptor connected to the client that
called connect, haing the same coket type and family type. The original
soket remains available to receive otherconneion requests. If we don't care
about client's identity we can set the second (struct sockaddr *addr)
and third parameter (socklen_t *len) to NULL*/
if((clfd = accept(sockfd, NULL, NULL))<0)
{
/*This generates a log mesage.
syslog(int priority, const char *fformat,...)
priority is a combination of facility and level. Levels are ordered from highest to lowest:
LOG_EMERG: emergency system unusable
LOG_ALERT: condiotin that must be fied immediately
LOG_CRIT: critical condition
LOG_ERR: error condition
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG
format and other arguments are passed to vsprintf function forf formatting.*/
syslog(LOG_ERR, "ruptimed: accept error: %s", strerror(errno));
exit(1);
}
/* set the FD_CLOEXEC file descriptor flag */
/*it causes the file descriptor to be automatically and atomically closed
when any of the exec family function is called*/
set_cloexec(clfd);
/**pg. 542 Since a common operation is to create a pipe to another process
to either read its output or write its input Stdio has provided popen and
pclose: popen creates pipe, close the unused ends of the pipe,
forks a child and call exec to execute cmdstr and
returns a file pointer (connected to std output if "r", to stdin if "w").
pclose closes the stream, waits for the command to terminate*/
if ((fp = popen("/usr/bin/uptime", "r")) == NULL)
{
/*sprintf copy the string passed as second parameter inside buf*/
sprintf(buf, "error: %s\n", strerror(errno));
/*pag 610. send is similar to write. send(int sockfd, const void *buf, size_t nbytes, it flags)*/
send(clfd, buf, strlen(buf),0);
}
else
{
/*get data from the pipe that reads created to exec /usr/bin/uptime */
while(fgets(buf, BUFLEN, fp)!=NULL)
{
/* clfd is returned by accept and it is a socket descriptor
connected to the client that called connect*/
send(clfd, buf, strlen(buf), 0);
}
/*see popen pag. 542*/
pclose(fp);
}
close(clfd);
}
}
int initserver(int type, const struct sockaddr *addr, socklen_t alen, int qlen)
{
int fd, err;
int reuse = 1;
if ((fd = socket(addr->sa_family, type, 0))<0)
{
return (-1);
}
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int))<0)
{
goto errout;
}
if(bind(fd, addr, alen)<0)
{
goto errout;
}
if (type == SOCK_STREAM || type == SOCK_SEQPACKET)
{
if(listen(fd, qlen)<0)
{
goto errout;
}
}
return fd;
errout:
err = errno;
close (fd);
errno = err;
return(-1);
}
utilities.c: containing the demonize and setcloexec functions. In daemonize function I did not close file descriptors for debugging.
#include "utilities.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <syslog.h>
#include <sys/time.h>//getrlimit
#include <sys/resource.h>//getrlimit
#include <signal.h> //sigempyset , asigcation (umask?)
#include <sys/resource.h>
#include <fcntl.h> //O_RDWR
#include <stdarg.h>
#include "error.h"
int daemonize(const char *cmd)
{
int fd0, fd1, fd2;
unsigned int i;
pid_t pid;
struct rlimit rl;
struct sigaction sa;
/* *Clear file creation mask.*/
umask(0);
/* *Get maximum number of file descriptors. */
if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
{
err_quit("%s: can’t get file limit", cmd);
}
/* *Become a session leader to lose controlling TTY. */
if ((pid = fork()) < 0)
{
err_quit("%s: can’t fork", cmd);
}
else if (pid != 0) /* parent */
{
exit(0); //the parent will exit
}
setsid();
/* *Ensure future opens won’t allocate controlling TTYs. */
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGHUP, &sa, NULL) < 0)
{
err_quit("%s: can’t ignore SIGHUP", cmd);
}
if ((pid = fork()) < 0)
{
err_quit("%s: can’t fork", cmd);
}
else if (pid != 0) /* parent */
{
exit(0);
}
/*
*Change the current working directory to the root so
* we won’t prevent file systems from being unmounted.
*/
if (chdir("/") < 0)
{
err_quit("%s: can’t change directory to /", cmd);
}
/* Close all open file descriptors. */
if (rl.rlim_max == RLIM_INFINITY)
{
rl.rlim_max = 1024;
}
printf("closing file descriptors\n");
/*for (i = 0; i < rl.rlim_max; i++)
{
close(i);
}*/
/* *Attach file descriptors 0, 1, and 2 to /dev/null.*/
//printf not working
/*printf("closed all file descriptors for daemonizing\n");*/
/*fd0 = open("/dev/null", O_RDWR);
fd1 = dup(0);
fd2 = dup(0);*/
/* *Initialize the log file. Daemons do not have a controlling terminal so
they can't write to stderror. We don't want them to write to the console device
because on many workstations the control device runs a windowing system. They can't
write on separate files either. A central daemon error-logging facility is required.
This is the BSD. 3 ways to generate log messages:
1) kernel routines call the log function. These messages can be read from /dev/klog
2) Most user processes (daemons) call syslog to generate log messages. This causes
messages to be sent to the UNIX domain datagram socket /dev/log
3) A user process on this host or on other host connected to this with TCP/ID
can send log messages to UDP port 514. Explicit network programmin is required
(it is not managed by syslog.
The syslogd daemon reads al three of log messages.
openlog is optional since if not called, syslog calls it. Also closelog is optional
openlog(const char *ident, int option, int facility)
It lets us specify ident that is added to each logmessage. option is a bitmask:
LOG_CONS tells that if the log message can't be sent to syslogd via UNIX
domain datagram, the message is written to the console instead.
facility lets the configuration file specify that messages from different
facilities are to be handled differently. It can be specified also in the 'priority'
argument of syslog. LOG_DAEMON is for system deamons
*/
/*
openlog(cmd, LOG_CONS, LOG_DAEMON);
if (fd0 != 0 || fd1 != 1 || fd2 != 2)
{*/
/*This generates a log mesage.
syslog(int priority, const char *fformat,...)
priority is a combination of facility and level. Levels are ordered from highest to lowest:
LOG_EMERG: emergency system unusable
LOG_ALERT: condiotin that must be fied immediately
LOG_CRIT: critical condition
LOG_ERR: error condition
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG
format and other arguments are passed to vsprintf function forf formatting.*/
/*syslog(LOG_ERR, "unexpected file descriptors %d %d %d", fd0, fd1, fd2);
exit(1);
}*/
return 0;
}
/*The function set the FD_CLOEXEC flag of the file descriptor already open that
is passed to as parameter. FD_CLOEXEC causes the file descriptor to be
automatically and atomically closed when any of the exec family function is
called*/
int set_cloexec(int fd)
{
int val;
/* retrieve the flags of the file descriptor */
if((val = fcntl(fd, F_GETFD, 0))<0)
{
return -1;
}
/* set the FD_CLOEXEC file descriptor flag */
/*it causes the file descriptor to be automatically and atomically closed
when any of the exec family function is called*/
val |= FD_CLOEXEC;
return (fcntl(fd, F_SETFD, val));
}
error functions I used
/* Fatal error unrelated to a system call.
* Print a message and terminate*/
void err_quit (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
err_doit (0, 0, fmt, ap);
va_end (ap);
exit(1);
}
/*Print a message and return to caller.
*Caller specifies "errnoflag"*/
static void err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf [MAXLINE];
vsnprintf (buf, MAXLINE-1, fmt, ap);
if (errnoflag)
{
snprintf (buf+strlen(buf), MAXLINE-strlen(buf)-1, ": %s",
strerror (error));
}
strcat(buf, "\n");
fflush(stdout); /*in case stdout and stderr are the same*/
fputs (buf, stderr);
fflush(NULL); /* flushes all stdio output streams*/
}
There is a relatively simple typo in the getaddrinfo call
if((err = getaddrinfo("host", "ruptime", &hint, &ailist))!=0)
/* ^^^^^^ */
/* Should be the variable host */
{
printf("error %s\n", gai_strerror(err));
syslog(LOG_ERR, "ruptimed: getaddrinfo error %s", gai_strerror(err));
exit(1);
}
The code is looking for the address of "host". I would also replace "ruptime" with NULL since ruptime isn't an entry in /etc/services. See getaddrinfo(3) for more detail.

Warning: assignment makes integer from pointer without a cast

I'm learning C almost one year and it's my first time that I got that warnings.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <windows.h>
#include <string.h>
int socket_creation(FILE* fp){
int s;
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET){
printf("Error occurred while creating socket: %d\n", WSAGetLastError());
fprintf(fp, "Error occurred while creating socket: %d\n", WSAGetLastError());
}
else{
printf("Socket creation was successful.\n");
fprintf(fp, "Socket creation was successful.\n");
}
return s;
}
void connect_to_server(int s, struct sockaddr_in ClientService, FILE* fp){
int cResult;
cResult = connect(s, (struct sockaddr*) &ClientService, sizeof(ClientService));
if (cResult == SOCKET_ERROR){
printf("connection to the server has been failed: %d\n", WSAGetLastError());
fprintf(fp, "connection to the server has been failed: %d\n", WSAGetLastError());
cResult = closesocket(s);
if (cResult == SOCKET_ERROR){
printf("error occurred while trying to close socket. \n");
fprintf(fp, "error occurred while trying to close socket. \n");
}
WSACleanup();
}
else{
printf("Connection to serevr has been made successfully. \n");
fprintf(fp, "Connection to serevr has been made successfully. \n");
}
}
int send_to_serv(char buffer[], int s){
int sendto;
sendto = send(s, buffer, 1024, 0);
if (sendto == -1)
printf("\nError: couldn't send the Code.\n", buffer);
else printf("\nCode: <%s> SENT.\n", buffer);
return sendto;
}
int recv_from_serv(int s, int* numberLines, FILE *fp){
int recvfrom;
char buffer[1024] = "";
recvfrom = recv(s, buffer, 1024, 0);
if (recvfrom == -1)
printf("\nError: couldn't receive Code. !\n");
else printf("\nRespond: <%s>, RECEIVED. \n", buffer);
fprintf(fp, "\n");
fprintf(fp, buffer);
*numberLines = atoi(buffer + 3);
return recvfrom;
}
int main() {
WSADATA info;
int error, s;
int sResults, sendError, recvError,convert2;
char buffer[1024] = "";
char recvbuf[1024] = "";
int numberLines, i, temp, convert;
char converted_num[1024] = "";
struct sockaddr_in ClientService;
FILE *fp = fopen("stored_data.txt", "w");
char* lines_array;
error = WSAStartup(MAKEWORD(2, 0), &info);
if (error != 0){
printf("WSAstartup failed with error: %d\n", error);
exit(1);
}
for (i=0; i <=numberLines; i++) {
lines_array[i]=NULL;
}
s = socket_creation(fp);
ClientService.sin_family = AF_INET;
ClientService.sin_addr.s_addr = inet_addr("54.209.143.42");
ClientService.sin_port = htons(6714);
connect_to_server(s, ClientService, fp);
strcpy(buffer, "100");
sendError = send_to_serv(buffer, s);
recvError = recv_from_serv(s, &numberLines, fp);
strcpy(buffer, "400");
sendError = send_to_serv(buffer, s);
recvError = recv_from_serv(s, &numberLines, fp);
printf("\nNumber of Lines are: %d\n", numberLines);
lines_array = malloc(sizeof(char*)*numberLines);
temp = numberLines;
for (i = 0; i < temp; i++){
convert = 5000001 + i;
_itoa(convert, converted_num, 10);
sendError = send_to_serv(converted_num, s);
convert2=atoi(convert);
convert=convert%10000;
if(convert2==0) {
for(i=0; i<=1024; i++) {
buffer[i]=0;
}
lines_array= (convert);
recv_from_serv(s, &numberLines, fp);
}
else{
for(i=0; i<=1024; i++) {
buffer[i]=0;
}
}
}
close(fp);
//system("PkAUSE>nul");
system("PAUSE");
return 0;
}
This is that output that my compiler (MinGW) giving to me:
finalproject1.c: In function main': finalproject1.c:87: warning:
assignment makes integer from pointer without a cast
finalproject1.c:113: warning: passing arg 1 ofatoi' makes pointer
from integer without a cast finalproject1.c:119: warning: assignment
makes pointer from integer without a cast finalproject1.c:133:3:
warning: no newline at end of file
Please help me I don't know what to do and my guid told me to "Google it" when I asked him to help me.
The line that triggers the warning is:
lines_array[i]=NULL;
and this variable is declared like so:
char* lines_array;
so you're cramming a pointer (the NULL macro expands to a pointer-type value on your system) into a single char, which won't fit and is a very strange thing to be doing.
Probably you wanted an array of character pointers, i.e.
char **lines_array;
but then you must of course allocate the array once you know how many lines you're going to be dealing with.
First of all, i can see you are using an integer numberLines as a condition in your for loop without initializing it first:
int numberLines;
for (i=0; i <=numberLines; i++) {
lines_array[i]=NULL;
}
Simply if you want to initialize your array to null you can initialize it as:
char* lines_array = null;
Try this and let me know if the warnings still exist.

Why do I get a Segmentation fault? I'm using stat, mmap, nftw, and memcmp, among other things

Here is my code. I'm assuming this has something to do with improper use of pointers or maybe I'm not mapping and unmapping my memory correctly.
Could anyone please provide me with some insight into the issue?
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <ftw.h>
#include <sys/stat.h>
#include <string.h>
int size;
int map1, map2;
void *tar, *temp;
int callback(const char *filename,
const struct stat *sb2,
int filetype,
struct FTW *ftw)
{
printf("test");
if(sb2->st_size == sb1->st_size){
temp = mmap(NULL, sb2->st_size, PROT_NONE, 0, map2, 0);
int cmp = memcmp(tar, temp, sb2->st_size);
printf("%d\n", cmp);
if(cmp == 0){
printf("%s\n", filename);
}
if(munmap(temp,sb2->st_size) == -1){
fprintf(stderr, "Error in unmapping in callback function");
exit(EXIT_FAILURE);
}
}
return 0; //continue to walk the tree
}
int main(int argc, char *argv[])
{
//check for correct arguments
if (argc == 1 || argc > 3) {
fprintf(stderr, "Syntax: %s filename dirname\n", argv[0]);
exit(EXIT_FAILURE);
}
//use stat to get size of filename
struct stat sb1;
if(stat(argv[1],&sb1) != 0){
fprintf(stderr, "Error in stat().");
exit(EXIT_FAILURE);
}
size = sb1.st_size;
//fd = mmap filename
tar = mmap(NULL,sb1->st_size, PROT_WRITE, MAP_SHARED, map1, 0);
if(tar == 0){
fprintf(stderr, "Main() mmap failed");
exit(EXIT_FAILURE);
}
//walk through the directory with callback function
nftw(argv[2], callback, 20, 0);
// use munmap to clear fd
if (munmap(tar,sb1->st_size) == -1) {
fprintf(stderr, "Error in unmapping");
exit(EXIT_FAILURE);
}
}
EDIT
I now declare my struct stat sb1 right before I use the stat function. After doing that I receieved a segmentation error again. I then commented out my nftw() call and and printed out the size variable (which has a reasonable number so I believe that's working). The new error is:
Error in unmapping.
You declare:
struct stat *sb1;
You use:
stat(argv[1],sb1);
You crash and burn because sb1 is a null pointer (since the variable is defined at file scope, it is initialized with 0).
You need to declare (at file scope):
struct stat sb1;
And then in main() you can use:
if (stat(argv[1], &sb1) != 0)
...oops...
You'll have to review all uses of sb1 to fix the status change from pointer to object, adding an & where necessary, and changing -> to . where necessary.
mmap() by example
This is a mildly edited version of a function I wrote that uses mmap() to map a file into memory:
/* Map named file into memory and validate that it is a MSG file */
static int msg_mapfile(const char *file)
{
int fd;
void *vp;
struct stat sb;
if (file == 0)
return(MSG_NOMSGFILE);
if ((fd = open(file, O_RDONLY, 0)) < 0)
return(MSG_OPENFAIL);
if (fstat(fd, &sb) != 0)
{
close(fd);
return(MSG_STATFAIL);
}
vp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (vp == MAP_FAILED)
return(MSG_MMAPFAIL);
The MSG_xxxx constants are distinct error numbers applicable to the program it came from. It was only needing to read the file, hence the PROT_READ; I think you may be OK with that too.
if (argc == 1 || argc > 3) {
fprintf(stderr, "Syntax: %s filename dirname\n", argv[0]);
exit(EXIT_FAILURE);
}
/* ... */
nftw(argv[2], callback, 20, 0);
I see a possibility for argv[2] to be NULL. Perhaps you meant:
if (argc != 3) {
fprintf(stderr, "Syntax: %s filename dirname\n", argv[0]);
exit(EXIT_FAILURE);
}
Which book are you reading?

copy_to_user not working - where I am on mistake?

I am trying to get some message from kernel space to userspace, when a condition fails!
Here's my kernel code:
#define MESSAGTOUSER 1
int ret_val;
struct siginfo sinfo;
pid_t id;
struct task_struct *task;
unsigned char msgBuffer[20];
unsigned char buf1[20]= "HI";
static int major_no;
static struct class *safe_class;
static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
static int device_open(struct inode *inode, struct file *file);
static int device_write(struct file *file, const char *gdata, size_t len, loff_t *off);
static int device_read(struct file *file, char *buf, size_t len, loff_t *off);
static int device_release(struct inode *inode, struct file *file);
int failureDetection (char* faultMsg) {
strcpy (msgBuffer, faultMsg);
printk(KERN_ALERT"\nMessage from HBM %s\n", msgBuffer);
printk(KERN_ALERT".......... RETURN VALUE ...... : %d", ret_val);
int Reg_Dev(void);
memset (&sinfo, 0, sizeof(struct siginfo));
sinfo.si_signo = SIGUSR1;
sinfo.si_code = SI_USER;
if (id == 0) {
printk("\ncan't find User PID: %d\n", id);
}else {
//task = pid_task(find_vpid(pid), PIDTYPE_PID);
task = find_task_by_vpid(id);
send_sig_info(SIGUSR1, &sinfo, task);
}
return 0;
}
static int device_open(struct inode *inode, struct file *file){
/*sucess*/
return 0;
}
void strPrint(void) {
printk("value of msgBuffer: %s", msgBuffer);
}
static int device_write(struct file *file, const char *gdata, size_t len, loff_t *off){
get_user (id,(int *)gdata);
if(id <0)
printk(KERN_ALERT"Cann't find PID from userspace its : %i", id);
else
printk(KERN_ALERT"Successfully received the PID of userspace %i", id);
return len;
}
static int
device_read(struct file *file, char *buf, size_t len, loff_t *off){
/*success*/
return 0;
}
static int device_release(struct inode *inode, struct file *file){
/*success*/
return 0;
}
static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
switch (cmd) {
case MESSAGTOUSER:
ret_val = copy_to_user((char *)arg, msgBuffer, sizeof(arg));
printk("Msg of Kernel %s", msgBuffer);
break;
default:
break;
}
return 0;
}
static struct file_operations fops = {
.open = device_open,
.write = device_write,
.read = device_read,
.release = device_release,
.unlocked_ioctl = device_ioctl
};
int Reg_Dev(void) {
major_no = register_chrdev(0, "safe_dev", &fops);
safe_class = class_create(THIS_MODULE, "safe_dev");
device_create(safe_class,NULL, MKDEV(major_no, 0), "safe_dev");
printk("\n Device Registered and Created \n");
return 0;
}
void UnReg_dev (void) {
printk("\nUser PID : %d\n", id);
unregister_chrdev(major_no, "safe_dev");
device_destroy(safe_class, MKDEV(major_no,0));
class_unregister(safe_class);
class_destroy(safe_class);
printk("\n Device Un-Registered and Destroyed \n");
}
extern int Reg_Dev(void);
for he userspace i have this code:
#define PORT 9930
#define G_IP "192.168.10.71"
#define BUFLEN 512
#define MESSAGTOUSER 0
unsigned char *str[20];
char b1[BUFLEN], b2[BUFLEN];
struct sockaddr_in me,client;
int s, i, n=sizeof(me);
int fd;
void error_handler(char *s) {
perror(s);
exit(1);
}
void signal_handler (int signum) {
if(signum == SIGUSR1)
{
printf("\n%s\n",str);
if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
error_handler("\nERROR: in Socket\n");
memset((char *) &me, 0, sizeof(me));
me.sin_family = AF_INET;
me.sin_port = PORT;
if (inet_aton(G_IP, &me.sin_addr)==0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
printf("Message from Kernel : %s", &str);
//strcpy (str, newStr);
int cntr =0; sprintf(b2, "\nFailure Message: %s\n",str);
printf("\nsending Fault to PMN Group : Tick - %d\n", cntr++);
if(sendto(s, str, sizeof(str),0,(struct sockaddr *) &me,n)==-1)
error_handler("\nERROR: in sendto()\n");
close (s);
// counter ++;
// sendAndReceiveOverUDP();
return;
}
}
int main() {
pid_t u_id;
u_id = getpid();
int i = 1;
fd = open("/dev/safe_dev",O_RDWR);
write(fd, &u_id, 4);
ioctl (fd, MESSAGTOUSER, &str);
printf("\n PID sent to device successfully: %d \n", u_id);
close(fd);
signal(SIGUSR1, signal_handler);
printf("\nMy PID is: %d\n",u_id);
//printf("Subnet 1 working fine.. Tick - %d", tv.tv_sec);
while (1)
sleep(1);
return 0;
}
Now what I am expecting to receive on Userspace:
Message from Kernel: A<->B
Sending Fault o PMN Group : tick - 0
Message from Kernel: B<->B
Sending Fault o PMN Group : tick - 1
....
...
but what is the output:
Message from Kernel:
Sending Fault o PMN Group : tick - 0
Message from Kernel:
Sending Fault o PMN Group : tick - 1
....
...
It seems that copy_to_user is not working, while in simple program just copying a string from kernel to user is working fine, but while i am using in this scenario then its not working, its compiling without any warning,
Some other Details:
failureDetection() is getting a string like A<->B mentioned in output from rest of the programs..
the same message from failureDetection is printing on kernel level but not transferring at the user level.
I have also tried to create an own string in this and tried to transfer that, but it is also not working! suppose msgBuffer = HI, then I should receive HI on to the userspace. but its not happening! can anyone please please make me correct whats wrong with this code? how can i get updates onto the userspace!!??
Sindhu..
The copy_to_user() only happens in response to the ioctl(), which only happens once, very early on in your code. Presumably at that point the kernel buffer msgBuffer is empty, because the failureDetection() function has not yet run at that point. It doesn't matter if failureDetection() runs later and sets msgBuffer then, because your userspace program never calls the ioctl() again so it doesn't see the new contents of msgBuffer.
You also have a bug in your copy_to_user() call - instead of sizeof(args) (which is a constant 4) you should probably use sizeof msgBuffer.
#caf: Thank you so much..
void signal_handler (int signum) {
if(signum == SIGUSR1)
{
fd = open ("/dev/safe_dev",O_RDWR);
ioctl (fd, MESSAGTOUSER, &str);
close (fd);
if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
error_handler("\nERROR: in Socket\n");
memset((char *) &me, 0, sizeof(me));
me.sin_family = AF_INET;
me.sin_port = PORT;
if (inet_aton(G_IP, &me.sin_addr)==0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
printf("Failure Detected on Eth Cards as : %s are non reachable.", str);
printf("\nsending Fault to PMN Group : Tick - %d\n", cntr++);
sprintf(b2, "\nFailure Message: %s\n",str);
if(sendto(s, str, sizeof(str),0,(struct sockaddr *) &me,n)==-1)
error_handler("\nERROR: in sendto()\n");
close (s);
return;
}
}
I was just making a stupid mistake.. hehehe.. i was not adding it in between file open and close block.. your suggestion resolved my issue...
Thank you so much for your response..
Rahee..

EXC_BAD_ACCESS While calling malloc function

I have the following function but sometimes it fails on the
malloc function call and I don't know the reason,I thought that it may be due to lack of heap size but I have monitored the heap and I understood that I have enough space available for memory allocation when malloc fails ,can any one suggest anything to me
char *substr(const char *pstr, int start, int numchars)
{
char *pnew;
pnew=malloc(numchars+1); //this line fails
if (pnew==0)
{
free(pnew);
pnew=malloc(numchars+1);
}
strncpy(pnew, pstr + start, numchars);
pnew[numchars] = '\0';
return pnew;
}
int32 SendData(char * dataBuffer, int CommandType)
{
struct sockaddr_in remoteServerAddr;
int tcpSocket;
int errorCode;
int counter;
int PacketsToSend;
int32 ret;
char msgbuf[16];
char *packet;
char * cmdIRes;
char RecPacket[BUF_SIZE];
div_t divresult;
counter = 0;
/* Specify struct sock address */
memset(&remoteServerAddr, 0, sizeof(remoteServerAddr));
remoteServerAddr.sin_len = sizeof(remoteServerAddr);
remoteServerAddr.sin_family = AF_INET;
remoteServerAddr.sin_port = htons(11000); // Net byte order required
remoteServerAddr.sin_addr.s_addr = inet_addr("10.252.85.26");
/* Create an TCP socket */
tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (tcpSocket != -1)
{
/* Connect to server */
errorCode = connect(tcpSocket, (struct sockaddr*) &remoteServerAddr,
sizeof(remoteServerAddr));
if (errorCode == -1)
{
/* Connection failed */
errorCode = socketerror();
sprintf(msgbuf, "Error %d", errorCode);
displayMsg("connect:", "Failed!!!", msgbuf, NULL, 0x0100FFFF);
}
else
{
/* Send packets */
divresult=div(sizeof(dataBuffer), BUF_SIZE);
PacketsToSend=divresult.quot;
if (divresult.rem>0)
{
PacketsToSend=PacketsToSend+1;
}
while (counter < PacketsToSend)
{
packet= substr(dataBuffer, counter*BUF_SIZE, BUF_SIZE);
errorCode = send(tcpSocket, packet,strlen(packet) , 0);
if (errorCode == -1)
{
errorCode = socketerror();
sprintf(msgbuf, "Error %d", errorCode);
displayMsg("send:", "Failed!!!", msgbuf, NULL, 0x0100FFFF);
break;
}
counter++;
}
memset(RecPacket, 0, BUF_SIZE);
errorCode = recv(tcpSocket, RecPacket, BUF_SIZE,0);
if (errorCode == -1)
{
errorCode = socketerror();
}
switch (CommandType)
{
case CommandType_SendOneTrans:
case CommandType_SendOfflineData:
cmdIRes=substr(RecPacket, 14, 10);
ret= atoi(cmdIRes);
break;
case CommandType_TransConfirm:
cmdIRes=substr(RecPacket, 11, 2);
if (strcmp(cmdIRes, "ok")==0)
{
ret= 1;
}
else
{
ret= 0;
}
break;
case CommandType_VoucherList:
SaveVoucherList(RecPacket);
ret= 1;
break;
case CommandType_Identify:
cmdIRes= substr(RecPacket, 7, 2);
if (strcmp(cmdIRes, "ok")==0)
{
ret=1;
}
else
{
ret= 0;
}
break;
default:
break;
}
}
/* Close the socket */
close(tcpSocket);
free(RecPacket);
free(cmdIRes);
free(packet);
free(msgbuf);
return ret;
}
else
{
errorCode = socketerror();
sprintf(msgbuf, "Error %d", errorCode);
displayMsg("socket:", "Failed!!!", msgbuf, NULL, 0x0100FFFF);
}
return (errorCode);
}
uint32 SendOneTrans(fin trans)
{
int retVal=0;
int ret=0;
int retValCon=0;
char msg[100];
char * voucherId;
char * Amount;
char * TerminalNo;
char * isOnline;
char * ReturnedId;
TerminalNo=malloc(12);
voucherId=malloc(4);
Amount=malloc(7);
isOnline=malloc(1);
ReturnedId=malloc(3);
memset(TerminalNo, 0, sizeof(TerminalNo));
strcpy(TerminalNo, (char *)getTerminalNo());
memset(msg, 0, sizeof(msg));
if (trans.success==0)
{
memset(msg, 0, sizeof(msg));
memset(voucherId, 0, sizeof(voucherId));
sprintf(voucherId, "%d", trans.voucherId);
memset(Amount, 0, sizeof(Amount));
sprintf(Amount, "%d", trans.Amount);
memset(isOnline, 0, sizeof(isOnline));
sprintf(isOnline, "%d", trans.isOnline);
strcpy(msg, "<Req_fin>");
strcat(msg, TerminalNo);
strcat(msg, ",");
strcat(msg, voucherId);
strcat(msg, ",");
strcat(msg, trans.cardNo);
strcat(msg, ",");
strcat(msg, Amount);
strcat(msg, ",");
strcat(msg, trans.dateOf);
strcat(msg, ",");
strcat(msg, trans.TimeOf);
strcat(msg, ",1");
strcat(msg, "<EOF>");
retVal= SendData(msg, CommandType_SendOneTrans);
if (retVal>=1)
{
sprintf(ReturnedId, "%i", retVal);
memset(msg, 0, sizeof(msg));
strcpy(msg, "<Req_fin_c>");
strcat(msg, TerminalNo);
strcat(msg, ",");
strcat(msg, ReturnedId);
strcat(msg, "<EOF>");
trans.success=1;
retValCon= SendData(msg, CommandType_TransConfirm);
if (retValCon!=0)
{
trans.success=1;
ret=1;
}
}
free(msg);
free(TerminalNo);
free(Amount);
free(voucherId);
return ret;
//free(ReturnedId);
}
}
I'm no Apple dev, but I've never seen EXC_BAD_ACCESS on malloc so I had to Google it. This entry from the Apple technical FAQ looks relevant:
This kind of problem is usually the result of over-releasing an object. It can be very confusing, since the failure tends to occur well after the mistake is made. The crash can also occur while the program is deep in framework code, often with none of your own code visible in the stack.
Your problem is deeper: EXC_BAD_ACCESS basically means that you are over-freeing zones of memory. In a debugger, you'd see something like this
*** malloc[705]: error for object 0×8c5b00:
Incorrect checksum for freed object - object was probably modified after being freed; break at func_name.
What's your platform? Is Guard Malloc available to you? If not, here's what you can do, besides scrutinizing your source code, of course:
Write a wrapper for malloc() which will allocate a single vm page for every request and place the requested buffer at its end. That way, reads or writes past it will cause a bus error. Also, when memory is free()'d, deallocate your vm page(s), so that whenever you read or write to a free()'d are you get an immediate bus error. It's going to take a loooong time, so be prepared!
char *substr(const char *pstr, int start, int numchars)
{
char *pnew;
pnew=malloc(numchars+1); //this line fails
the following makes no sense, what is it you are trying to do? if malloc failed why try again and above all why free? You should exit instead and return null
if (pnew==0)
{
free(pnew);
pnew=malloc(numchars+1);
}
I think the error is somewhere else, maybe you should check
the in-parameters, make sure pstr is not NULL and numchars > 0
You probably corrupted your malloc heap somewhere earlier in the code but the problem does not show up until you call malloc or free - you should run the code under valgrind or similar to narrow down the root cause.
This mite help clear things:
You will get EXC_BAD_ACCESS in 3 cases:
1. An object is not initialized
2. An object is already released
3. Something else that is not very likely to happen
So please evaluate the state of other variables in the function or you can paste the calling function here to get better solutions.
EDIT: code continued after getting information from the comments.
NULL check avoided intentionally.
char a[][100] = {"<Req_fin>1","<Req_fin>1","<Req_fin>1<EOF>","<Req_fin>1<EOF>","<Req_fin>1","<Req_fin>1<EOF>","<Req_fin>1<EOF>","<Req_fin>1<EOF>","<Req_fin>1","<Req_fin>1<EOF>"};
char *b= "<EOF>";
char *substr(char *buff,int start,int bytes)
{
char *ptr;
ptr = malloc(bytes+1);
strncpy(ptr,buff+start,bytes);
ptr[bytes]='\0';
return ptr;
}
int main()
{
char buff[100];
int i;
char *ptr;
strcpy(buff,"Abcdef");
for(i=0;i<10;i++)
{
ptr = substr(buff,0,512);
printf("String is %s \n",ptr);
memset(buff,0,sizeof(buff));
strcpy(buff,a[i]);
strcat(buff,b);
free(ptr);
}
return 0;
}
The above code works fine. So please use this as reference if you cant paste your function here. I can't just guess the error.

Resources