ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I am a newbie in Module programming. I wanted to know
whether i can return a specific exit code other than
predefined ones while writing to a proc entry.
For example if there is an error while writing to a proc entry
can i have an exit code like e.g. 1234.
This will help me in more specifically pointing out the reason
for the error.
For example i do
$bash] echo "This echo whill fail" > /proc/dummy/waste
Suppose the write failed because of Error "Some weired Error",
which i have defined to be error number 1234 in my error file,
i should get 1234 in $? on the shell prompt.
But all the friends i asked for help told me that they dont
know of any such way, and they can return only whether
command suceeded or command failed. I cannot pin point reason
behind the error
But i think there has to be atleast some work around to
get specific exit code, so that i can parse an error file for that
exit code, and echo the specific reason.
Thanks in advance.....
Hi,
By 1234 i meant any code other than -1, 0 and 1.
That is when i echo something to proc, i should be
able to return a specific code which represents a specific
error; rather than just telling that there was an error
or not.
Hi,
By 1234 i meant any code other than -1, 0 and 1.
That is when i echo something to proc, i should be
able to return a specific code which represents a specific
error; rather than just telling that there was an error
or not.
I think what kaz is saying is that you can’t dictate the return code of echo (a userspace program) from the kernel. What you can do is read the appropriate errno value and interpret it as you like in a C program. In fact, on my system, echo will do just that. Instead of returning the error number, however, it will write some diagnostic output and have an exit code of 1. So basically, you can chose any of the “specific error” codes available as system error numbers. That’s as close as you’re going to get.
To show this, consider the module made of a file waste.c:
Code:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("osor");
static struct proc_dir_entry *dummy, *waste;
char dir_name [] = "dummy";
char entry_name [] = "waste";
static int write_waste(struct file *file,
const char *buffer,
unsigned long count,
void *data)
{
if(!strncmp("This should say \"Bad address\"\n", buffer, count))
return -EFAULT;
if(!strncmp("This should say \"Resource temporarily unavailable\"\n",
buffer, count))
return -EAGAIN;
return -EINVAL; /* Default is "Invalid argument" */
}
int init_module()
{
dummy = proc_mkdir(dir_name, NULL);
if(dummy == NULL) {
printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
dir_name);
return -ENOMEM;
}
waste = create_proc_entry(entry_name, 0222, dummy);
waste->write_proc = write_waste;
if (waste == NULL) {
remove_proc_entry(entry_name, dummy);
remove_proc_entry(dir_name, NULL);
printk(KERN_ALERT "Error: Could not initialize /proc/%s/%s\n",
dir_name, entry_name);
return -ENOMEM;
}
return 0;
}
void cleanup_module()
{
remove_proc_entry(entry_name, dummy);
remove_proc_entry(dir_name, NULL);
}
Hi syg00,
The problem with having another proc entry e.g. /proc/dummy/code for error code comes,when i have almost simultaneous writes on the proc/dummy/waste.
The first command fails, and the /proc/dummy/code has the error code e.g. 12,
but before i read /proc/dummy/code another write to dummy/waste is successfull, and i wont see specific reason for the error as /proc/dummy/code shows 0.
So i think osor's way seems to be the best solution , but my bash does
not give those messages when i tried running osor's code. my bash version is 3.00.15. Do i need to enable something in bash for that?
my bash does
not give those messages when i tried running osor's code. my bash version is 3.00.15. Do i need to enable something in bash for that?
For me, it works on bash-3.2 and 3.1, but not on 3.0. Regardless of your shell, you could always use the non-builtin version of echo (usually it’s just “/bin/echo”). For me, it gives similar output with coreutils-6.9:
In any case, you should not depend on the specific implementation of a userspace program when looking for errors. The best thing to do is to establish (i.e., document in an API) what each errno means for a write to your particular proc_entry. Let specialized programs look for such errno values upon the write() system call and handle them accordingly.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.