errno & libc

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
robif
Posts: 6
Joined: Sat Oct 15, 2005 11:02 pm
Location: Maribor

errno & libc

Post by robif »

Hi everybody,

first i would like to say hello as i'm new here (been lurking for some time).

Recently i started to mess with Python port and noticed that functions in libc do not set
errno when sce* functions fail. I think this is not ok as it may break programs that expect
non zero value in errno when function fails (it also helps to debug).

I implemented a naive approach to solve this problem in libcglue.c.
I implemented this function:

Code: Select all

#define __PSP_FACILITY_ERRNO (0x80010000)

static int psp_set_errno(int code) 
{
        if (code & 0x80000000) 
        {
                if (code & __PSP_FACILITY_ERRNO) 
                {
                        code &= 0x0000FFFF;
                        switch(code)
                        {
                                case 0x24:
                                        code = ENAMETOOLONG;
                                        break; 
                                case 0x62:
                                        code = EADDRINUSE;
                                        break;
                                case 0x67:
                                        code = ECONNABORTED;
                                        break;
                                case 0x6E:
                                        code = ETIMEDOUT;
                                        break;
                                case 0x7B:
                                        code = ENOMEDIUM;
                                        break;
                                        
                        }
                                                
                }
                errno = code;
                return -1;
        }
        else
        {
                return code;
        }
}
then wrapped most sce* calls like that:

Code: Select all

int mkdir(const char *pathname, mode_t mode)
{
        char dest[MAXPATHLEN + 1];

        if&#40;__psp_path_absolute&#40;pathname, dest, MAXPATHLEN&#41; < 0&#41; &#123;
                errno = ENAMETOOLONG;
                return -1;
        &#125;

        return psp_set_errno&#40;sceIoMkdir&#40;dest, mode&#41;&#41;;
&#125;

I tested this with Python and it seems to work fine.
Would that be acceptable approach, or is there another way?

BR,
Robert
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Re: errno & libc

Post by jimparis »

I'd rather see something without a lookup:

Code: Select all

static int psp_set_errno&#40;int code&#41; 
&#123;
                if &#40;code & 0x80010000&#41; &#123;
                        errno = code & 0xFFFF;
                        return -1;
                &#125;
                return code;
&#125;
and then modify our newlib errno.h to use errno codes that match up with Sony's. I think most of the errno codes match up anyway; changing those that don't shouldn't hurt anything.

BTW, the errno variable in pspsdk isn't thread-local. I'm not sure of the best way to fix that.
robif
Posts: 6
Joined: Sat Oct 15, 2005 11:02 pm
Location: Maribor

Re: Lookup

Post by robif »

I agree that it would be better to change newlib's error codes than to perform lookup, as there
are only few values (that i know of) that differ.

Regarding thread safety i'm not sure either. Currently when i compile thread code into Python
PSP seems to freeze. This could be for many reasons...
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

OK, I've cleaned up the errno handling in r1223. It should be more or less correct now, except that the actual errno values still need to get fixed up.
robif
Posts: 6
Joined: Sat Oct 15, 2005 11:02 pm
Location: Maribor

Post by robif »

Thanks Jim,
this looks great.

Robert
Post Reply