Re: [evlog-dev] Wrong use of _SC_GETPW_R_SIZE_MAX

Hien Nguyen <[email protected]> Wed, 21 Apr 2004 15:23:47 -0700
Newsgroups gmane.linux.kernel.event-logging
Message-ID <[email protected]>
Hello Benoit,

Thanks for your suggestion. How about change it to something like this


case POSIX_LOG_ENTRY_UID:
            {
#ifdef _POSIX_THREAD_SAFE_FUNCTIONS
                struct passwd *pw, passwd;
                char buf[50];
                /* If host is a rmt host - don't resolve the user name
*/
                if ((entry->log_processor >> 16) =3D=3D 0) {
                        (void) getpwuid_r(entry->log_uid, &passwd, buf,
                                sizeof(buf), &pw);
#else
                struct passwd *pw;
                /* If host is a rmt host - don't resolve the user name
*/
                if ((entry->log_processor >> 16) =3D=3D 0) {
                        pw =3D getpwuid(entry->log_uid);
#endif
                        if (pw) {
                                (void) strcpy(s, pw->pw_name);
                        } else {
                                (void) snprintf(s, sizeof(s), "%u",
entry->log_uid);
                        }
                }
            }
                break;



On Tue, 2004-04-20 at 07:15, Benoit Guillon wrote:
> Hello again,=20
>=20
> In posix1.c (I haven't check the other files) _SC_GETPW_R_SIZE_MAX is
> used as buffer size. Actually it is a variable name, not the size
> itself. You should use sysconf() to have the real size.=20
>=20
> It could give something like this:=20
>=20
>  case POSIX_LOG_ENTRY_UID:
>      {
> #ifdef _POSIX_THREAD_SAFE_FUNCTIONS
>   struct passwd *pw, passwd;
>   size_t buflen;
>   char * buf;
>   /* If host is a rmt host - don't resolve the user name */
>   if ((entry->log_processor >> 16) !=3D 0) goto _rmt_user_exit;
>   buflen =3D sysconf(_SC_GETPW_R_SIZE_MAX);
>   /* one should check the buflen value */
>   buf =3D (char *)malloc(buflen);
>   (void) getpwuid_r(entry->log_uid, &passwd, buf,
>                     buflen, &pw);
> #else
>   struct passwd *pw;
>   /* If host is a rmt host - don't resolve the user name */
>   if ((entry->log_processor >> 16) !=3D 0) goto _rmt_user_exit;
>   pw =3D getpwuid(entry->log_uid);
> #endif
>  if (pw) {
>    (void) strcpy(s, pw->pw_name);
>   } else {
>    (void) snprintf(s, sizeof(s), "%u", entry->log_uid);
>   }
> #ifdef _POSIX_THREAD_SAFE_FUNCTIONS
>   free(buf);
> #endif
>  =20
>=20
> Bye,=20
> --
> Beno=C3=AEt Guillon                [email protected]=20
> =20



-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click