Userspace program cannot get data from ctl_enqueudata()
Userspace program cannot get data from ctl_enqueudata()
- Subject: Userspace program cannot get data from ctl_enqueudata()
- From: NAHieu <email@hidden>
- Date: Sat, 14 Oct 2006 18:20:28 +0900
Hello,
I am writing a small kext that sends data out to userspace via kernel
control. However, the userspace program cannot receive the expected
data. Anybody plese tell me what I did wrong?
My code is very simple: it sends a variable length text to userspace.
Todo that, I have a structure named MyLog, like below:
--
struct MyLog {
int length; /* text length */
uid_t uid; /* uid that generated this action */
unsigned int action; /* in kernel space: kauth_action_t action */
};
--
Then the ctrl_connect() method of kernel control is defined so
whenever a userspace program connect to kernel control, ctl_connect()
sends out few text. Please see below:
--
static int ctl_connect(kern_ctl_ref ctl_ref, struct sockaddr_ctl *sac,
void **unitinfo)
{
struct MyLog log;
char *text = "Greeting from kernelspace!";
log.uid = 2006;
log.action = 1010;
log.length = strlen(text);
/* put log information into queue */
ctl_enqueuedata(ctl_ref, sac->sc_unit, &log, sizeof(log), 0);
/* then put the variable text into queue */
ctl_enqueuedata(ctl_ref, sac->sc_unit, text, strlen(text), 0);
return 0;
}
--
My program in userspace connect to kernel, and trying to read data
out, like this:
---
struct MyLog log;
n = recv(gSocket, &log, sizeof(log), MSG_WAITALL);
if (n == -1) {
printf("Failed on getting data from kernel\n");
return -1;
}
printf("Got packet of %d byte, variable length: %d\n", n, log.length);
n = recv(gSocket, buffer, length, MSG_WAITALL);
printf("Got exactly %d bytes\n", n);
buffer[n] = '\0';
printf("Action %d on %s (%d)\n", log.action, buffer, log.uid);
---
However, while I expect that my userspace program will get the text
"Greeting from kernelspace", it does not. In fact, I only got data
from the first recv() function, and my program printed out:
---
Got packet of 16 byte, variable length: 16
---
So far, it is correct. But then it hangs up there, instead of
succesfully execute the next recv()? Why that happens? In my kernel
code, I already put all the neccessary data in the control buffer, no?
(???)
Then I tried to copy another data into the kernel buffer (use another
command ctl_enqueudata() command). Then the userspace program quit the
hangup status, and continue to print out data. But the data is not
what I expected, like below:
--
Got exactly 26 bytes
Action 1010 on (2006)
--
As you can see, the text "Greeting from kernelspace" is no where to be
seen, although I got 26 bytes as I expected. The only problem is that
the data is incorrect.
It seems that the way I put the kernel data into the buffer is
incorrect, or what else should be fixed?
Thank you a lot, and have a nice weekend (I don't :-)
H
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden