[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


About <linux/ucred.h> .............

Posted on 2004-08-06
Medium Priority
Last Modified: 2013-11-15
#include        "unp.h"
#include        <sys/param.h>
#include        <sys/ucred.h>

ssize_t recv_cred(int, void *, size_t, struct fcred *);

        int                             fd[2], on, n;
        char                    buf[100];
        struct fcred    cred;

        if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fd) < 0)
                err_sys("socketpair error");

                /* must set the socket option on the *receiving* socket */
        on = 1;
        Setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));

        Write(fd[0], "hello, world\n", 13);

        if ( (n = recv_cred(fd[1], buf, sizeof(buf), &cred)) < 0)
                err_sys("recv_cred error");
        else if (n == 0)
                err_quit("recv_cred, unexpected EOF");

        buf[n] = 0;                     /* null terminate */
        printf("data: %s", buf);

        if (cred.fc_ngroups == 0)
                printf("(no credentials returned)\n");
        else {
                printf("real user ID = %d\n", cred.fc_ruid);
                printf("real group ID = %d\n", cred.fc_rgid);
                printf("login name = %-*s\n", MAXLOGNAME, cred.fc_login);
                printf("effective user ID = %d\n", cred.fc_uid);
                printf("effective group ID = %d\n", cred.fc_gid);
                printf("%d supplementary groups:", cred.fc_ngroups - 1);
                for (n = 1; n < cred.fc_ngroups; n++)   /* [0] is the egid */
                        printf(" %d", cred.fc_groups[n]);


#define CONTROL_LEN     (sizeof(struct cmsghdr) + sizeof(struct fcred))

recv_cred(int fd, void *ptr, size_t nbytes, struct fcred *fcredptr)
        struct msghdr   msg;
        struct iovec    iov[1];
        char                    control[CONTROL_LEN + 20];
int                             n;

        msg.msg_name = NULL;
        msg.msg_namelen = 0;
        iov[0].iov_base = ptr;
        iov[0].iov_len = nbytes;
        msg.msg_iov = iov;
        msg.msg_iovlen = 1;
        msg.msg_control = control;
        msg.msg_controllen = sizeof(control);
        msg.msg_flags = 0;

        if ( (n = recvmsg(fd, &msg, 0)) < 0)

        fcredptr->fc_ngroups = 0;       /* indicates no credentials returned */
        if (fcredptr && msg.msg_controllen > 0) {
                struct cmsghdr  *cmptr = (struct cmsghdr *) control;

                if (cmptr->cmsg_len != sizeof(struct cmsghdr) + sizeof(struct fcred))
                        err_quit("control length = %d", cmptr->cmsg_len);
                if (cmptr->cmsg_level != SOL_SOCKET)
                        err_quit("control level != SOL_SOCKET");
                if (cmptr->cmsg_type != SCM_CREDS)
                        err_quit("control type != SCM_CREDS");
                memcpy(fcredptr, CMSG_DATA(cmptr), sizeof(struct fcred));


unp.h file

/* include unph */
/* Our own header.  Tabs are set for 4 spaces, not 8 */

#ifndef      __unp_h
#define      __unp_h

#include      "../config.h"      /* configuration options for current OS */
                                          /* "../config.h" is generated by configure */

/* If anything changes in the following list of #includes, must change
   acsite.m4 also, for configure's tests. */

#include      <sys/types.h>      /* basic system data types */
#include      <sys/socket.h>      /* basic socket definitions */
#include      <sys/time.h>      /* timeval{} for select() */
#include      <time.h>            /* timespec{} for pselect() */
#include      <netinet/in.h>      /* sockaddr_in{} and other Internet defns */
#include      <arpa/inet.h>      /* inet(3) functions */
#include      <errno.h>
#include      <fcntl.h>            /* for nonblocking */
#include      <netdb.h>
#include      <signal.h>
#include      <stdio.h>
#include      <stdlib.h>
#include      <string.h>
#include      <sys/stat.h>      /* for S_xxx file mode constants */
#include      <sys/uio.h>            /* for iovec{} and readv/writev */
#include      <unistd.h>
#include      <sys/wait.h>
#include      <sys/un.h>            /* for Unix domain sockets */

#ifdef      HAVE_SYS_SELECT_H
# include      <sys/select.h>      /* for convenience */

#ifdef      HAVE_POLL_H
# include      <poll.h>            /* for convenience */

#ifdef      HAVE_STRINGS_H
# include      <strings.h>            /* for convenience */

/* Three headers are normally needed for socket/file ioctl's:
 * <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>.
#ifdef      HAVE_SYS_IOCTL_H
# include      <sys/ioctl.h>
#ifdef      HAVE_SYS_FILIO_H
# include      <sys/filio.h>
#ifdef      HAVE_SYS_SOCKIO_H
# include      <sys/sockio.h>

#ifdef      HAVE_PTHREAD_H
# include      <pthread.h>

/* OSF/1 actually disables recv() and send() in <sys/socket.h> */
#ifdef      __osf__
#undef      recv
#undef      send
#define      recv(a,b,c,d)      recvfrom(a,b,c,d,0,0)
#define      send(a,b,c,d)      sendto(a,b,c,d,0,0)

#ifndef      INADDR_NONE
/* $$.Ic INADDR_NONE$$ */
#define      INADDR_NONE      0xffffffff      /* should have been in <netinet/in.h> */

#ifndef      SHUT_RD                        /* these three Posix.1g names are quite new */
#define      SHUT_RD            0      /* shutdown for reading */
#define      SHUT_WR            1      /* shutdown for writing */
#define      SHUT_RDWR      2      /* shutdown for reading and writing */
/* $$.Ic SHUT_RD$$ */
/* $$.Ic SHUT_WR$$ */
/* $$.Ic SHUT_RDWR$$ */

/* *INDENT-OFF* */
#define      INET_ADDRSTRLEN            16      /* "ddd.ddd.ddd.ddd\0"
                                                    1234567890123456 */

/* Define following even if IPv6 not supported, so we can always allocate
   an adequately-sized buffer, without #ifdefs in the code. */
/* $$.Ic INET6_ADDRSTRLEN$$ */
#define      INET6_ADDRSTRLEN      46      /* max size of IPv6 address string:
                           "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" or
                            1234567890123456789012345678901234567890123456 */
/* *INDENT-ON* */

/* Define bzero() as a macro if it's not in standard C library. */
#ifndef      HAVE_BZERO
#define      bzero(ptr,n)            memset(ptr, 0, n)
/* $$.If bzero$$ */
/* $$.If memset$$ */

/* Older resolvers do not have gethostbyname2() */
#define      gethostbyname2(host,family)            gethostbyname((host))

/* The structure returned by recvfrom_flags() */
struct in_pktinfo {
  struct in_addr      ipi_addr;      /* dst IPv4 address */
  int                        ipi_ifindex;/* received interface index */
/* $$.It in_pktinfo$$ */
/* $$.Ib ipi_addr$$ */
/* $$.Ib ipi_ifindex$$ */

/* We need the newer CMSG_LEN() and CMSG_SPACE() macros, but few
   implementations support them today.  These two macros really need
    an ALIGN() macro, but each implementation does this differently. */
#ifndef      CMSG_LEN
/* $$.Ic CMSG_LEN$$ */
#define      CMSG_LEN(size)            (sizeof(struct cmsghdr) + (size))
#ifndef      CMSG_SPACE
/* $$.Ic CMSG_SPACE$$ */
#define      CMSG_SPACE(size)      (sizeof(struct cmsghdr) + (size))

/* Posix.1g requires the SUN_LEN() macro but not all implementations DefinE
   it (yet).  Note that this 4.4BSD macro works regardless whether there is
   a length field or not. */
#ifndef      SUN_LEN
/* $$.Im SUN_LEN$$ */
# define      SUN_LEN(su) \
      (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))

/* Posix.1g renames "Unix domain" as "local IPC".
   But not all systems DefinE AF_LOCAL and AF_LOCAL (yet). */
#ifndef      AF_LOCAL
#define AF_LOCAL      AF_UNIX
#ifndef      PF_LOCAL
#define PF_LOCAL      PF_UNIX

/* Posix.1g requires that an #include of <poll.h> DefinE INFTIM, but many
   systems still DefinE it in <sys/stropts.h>.  We don't want to include
   all the streams stuff if it's not needed, so we just DefinE INFTIM here.
   This is the standard value, but there's no guarantee it is -1. */
#ifndef INFTIM
#define INFTIM          (-1)    /* infinite poll timeout */
/* $$.Ic INFTIM$$ */
#ifdef      HAVE_POLL_H
#define      INFTIM_UNPH                        /* tell unpxti.h we defined it */

/* Following could be derived from SOMAXCONN in <sys/socket.h>, but many
   kernels still #define it as 5, while actually supporting many more */
#define      LISTENQ            1024      /* 2nd argument to listen() */

/* Miscellaneous constants */
#define      MAXLINE            4096      /* max text line length */
#define      MAXSOCKADDR  128      /* max socket address structure size */
#define      BUFFSIZE      8192      /* buffer size for reads and writes */

/* Define some port number that can be used for client-servers */
#define      SERV_PORT             9877                  /* TCP and UDP client-servers */
#define      SERV_PORT_STR      "9877"                  /* TCP and UDP client-servers */
#define      UNIXSTR_PATH      "/tmp/unix.str"      /* Unix domain stream cli-serv */
#define      UNIXDG_PATH            "/tmp/unix.dg"      /* Unix domain datagram cli-serv */
/* $$.ix [LISTENQ]~constant,~definition~of$$ */
/* $$.ix [MAXLINE]~constant,~definition~of$$ */
/* $$.ix [MAXSOCKADDR]~constant,~definition~of$$ */
/* $$.ix [BUFFSIZE]~constant,~definition~of$$ */
/* $$.ix [SERV_PORT]~constant,~definition~of$$ */
/* $$.ix [UNIXSTR_PATH]~constant,~definition~of$$ */
/* $$.ix [UNIXDG_PATH]~constant,~definition~of$$ */

/* Following shortens all the type casts of pointer arguments */
#define      SA      struct sockaddr

#define      FILE_MODE      (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
                              /* default file access permissions for new files */
#define      DIR_MODE      (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
                              /* default permissions for new directories */

typedef      void      Sigfunc(int);      /* for signal handlers */

#define      min(a,b)      ((a) < (b) ? (a) : (b))
#define      max(a,b)      ((a) > (b) ? (a) : (b))

# include      "../lib/addrinfo.h"

struct if_nameindex {
  unsigned int   if_index;  /* 1, 2, ... */
  char          *if_name;   /* null terminated name: "le0", ... */
/* $$.It if_nameindex$$ */
/* $$.Ib if_index$$ */
/* $$.Ib if_name$$ */

struct timespec {
  time_t      tv_sec;            /* seconds */
  long            tv_nsec;      /* and nanoseconds */
/* $$.It timespec$$ */
/* $$.Ib tv_sec$$ */
/* $$.Ib tv_nsec$$ */
/* end unph */

                  /* prototypes for our own library functions */
int             connect_nonb(int, const SA *, socklen_t, int);
int             connect_timeo(int, const SA *, socklen_t, int);
void       daemon_init(const char *, int);
void       daemon_inetd(const char *, int);
void       dg_cli(FILE *, int, const SA *, socklen_t);
void       dg_echo(int, SA *, socklen_t);
char      *gf_time(void);
void       heartbeat_cli(int, int, int);
void       heartbeat_serv(int, int, int);
struct addrinfo *host_serv(const char *, const char *, int, int);
int             inet_srcrt_add(char *, int);
u_char  *inet_srcrt_init(void);
void       inet_srcrt_print(u_char *, int);
char   **my_addrs(int *);
int             readable_timeo(int, int);
ssize_t       readline(int, void *, size_t);
ssize_t       readn(int, void *, size_t);
ssize_t       read_fd(int, void *, size_t, int *);
ssize_t       recvfrom_flags(int, void *, size_t, int *, SA *, socklen_t *,
             struct in_pktinfo *);
Sigfunc *signal_intr(int, Sigfunc *);
int             sock_bind_wild(int, int);
int             sock_cmp_addr(const SA *, const SA *, socklen_t);
int             sock_cmp_port(const SA *, const SA *, socklen_t);
int             sock_get_port(const SA *, socklen_t);
void       sock_set_addr(SA *, socklen_t, const void *);
void       sock_set_port(SA *, socklen_t, int);
void       sock_set_wild(SA *, socklen_t);
char      *sock_ntop(const SA *, socklen_t);
char      *sock_ntop_host(const SA *, socklen_t);
int             sockfd_to_family(int);
void       str_echo(int);
void       str_cli(FILE *, int);
int             tcp_connect(const char *, const char *);
int             tcp_listen(const char *, const char *, socklen_t *);
void       tv_sub(struct timeval *, struct timeval *);
int             udp_client(const char *, const char *, void **, socklen_t *);
int             udp_connect(const char *, const char *);
int             udp_server(const char *, const char *, socklen_t *);
int             writable_timeo(int, int);
ssize_t       writen(int, const void *, size_t);
ssize_t       write_fd(int, void *, size_t, int);

#ifdef      MCAST
int             mcast_leave(int, const SA *, socklen_t);
int             mcast_join(int, const SA *, socklen_t, const char *, u_int);
int             mcast_get_if(int);
int             mcast_get_loop(int);
int             mcast_get_ttl(int);
int             mcast_set_if(int, const char *, u_int);
int             mcast_set_loop(int, int);
int             mcast_set_ttl(int, int);

void       Mcast_leave(int, const SA *, socklen_t);
void       Mcast_join(int, const SA *, socklen_t, const char *, u_int);
int             Mcast_get_if(int);
int             Mcast_get_loop(int);
int             Mcast_get_ttl(int);
void       Mcast_set_if(int, const char *, u_int);
void       Mcast_set_loop(int, int);
void       Mcast_set_ttl(int, int);

unsigned short      in_cksum(unsigned short *, int);

int             getaddrinfo(const char *, const char *, const struct addrinfo *,
                               struct addrinfo **);
void       freeaddrinfo(struct addrinfo *);
char      *gai_strerror(int);

int             getnameinfo(const SA *, socklen_t, char *, size_t, char *, size_t, int);

int             gethostname(char *, int);

const char      *hstrerror(int);

unsigned int       if_nametoindex(const char *);
char                  *if_indextoname(unsigned int, char *);
void                   if_freenameindex(struct if_nameindex *);
struct if_nameindex *if_nameindex(void);

int                   inet_pton(int, const char *, void *);
const char      *inet_ntop(int, const void *, char *, size_t);

int             inet_aton(const char *, struct in_addr *);

int             isfdtype(int, int);

int             pselect(int, fd_set *, fd_set *, fd_set *,
                         const struct timespec *, const sigset_t *);

int             sockatmark(int);

int             snprintf(char *, size_t, const char *, ...);

                  /* prototypes for our own library wrapper functions */
void       Connect_timeo(int, const SA *, socklen_t, int);
struct addrinfo *Host_serv(const char *, const char *, int, int);
const char            *Inet_ntop(int, const void *, char *, size_t);
void                   Inet_pton(int, const char *, void *);
char                  *If_indextoname(unsigned int, char *);
unsigned int             If_nametoindex(const char *);
struct if_nameindex      *If_nameindex(void);
char   **My_addrs(int *);
ssize_t       Read_fd(int, void *, size_t, int *);
int             Readable_timeo(int, int);
ssize_t       Recvfrom_flags(int, void *, size_t, int *, SA *, socklen_t *,
             struct in_pktinfo *);
Sigfunc *Signal(int, Sigfunc *);
Sigfunc *Signal_intr(int, Sigfunc *);
int             Sock_bind_wild(int, int);
char      *Sock_ntop(const SA *, socklen_t);
char      *Sock_ntop_host(const SA *, socklen_t);
int             Sockfd_to_family(int);
int             Tcp_connect(const char *, const char *);
int             Tcp_listen(const char *, const char *, socklen_t *);
int             Udp_client(const char *, const char *, void **, socklen_t *);
int             Udp_connect(const char *, const char *);
int             Udp_server(const char *, const char *, socklen_t *);
ssize_t       Write_fd(int, void *, size_t, int);
int             Writable_timeo(int, int);

                  /* prototypes for our Unix wrapper functions: see {Sec errors} */
void      *Calloc(size_t, size_t);
void       Close(int);
void       Dup2(int, int);
int             Fcntl(int, int, int);
void       Gettimeofday(struct timeval *, void *);
int             Ioctl(int, int, void *);
pid_t       Fork(void);
void      *Malloc(size_t);
void       Mktemp(char *);
void      *Mmap(void *, size_t, int, int, int, off_t);
int             Open(const char *, int, mode_t);
void       Pipe(int *fds);
ssize_t       Read(int, void *, size_t);
void       Sigaddset(sigset_t *, int);
void       Sigdelset(sigset_t *, int);
void       Sigemptyset(sigset_t *);
void       Sigfillset(sigset_t *);
int             Sigismember(const sigset_t *, int);
void       Sigpending(sigset_t *);
void       Sigprocmask(int, const sigset_t *, sigset_t *);
char      *Strdup(const char *);
long       Sysconf(int);
void       Sysctl(int *, u_int, void *, size_t *, void *, size_t);
void       Unlink(const char *);
pid_t       Wait(int *);
pid_t       Waitpid(pid_t, int *, int);
void       Write(int, void *, size_t);

                  /* prototypes for our stdio wrapper functions: see {Sec errors} */
void       Fclose(FILE *);
FILE      *Fdopen(int, const char *);
char      *Fgets(char *, int, FILE *);
FILE      *Fopen(const char *, const char *);
void       Fputs(const char *, FILE *);

                  /* prototypes for our socket wrapper functions: see {Sec errors} */
int             Accept(int, SA *, socklen_t *);
void       Bind(int, const SA *, socklen_t);
void       Connect(int, const SA *, socklen_t);
void       Getpeername(int, SA *, socklen_t *);
void       Getsockname(int, SA *, socklen_t *);
void       Getsockopt(int, int, int, void *, socklen_t *);
int             Isfdtype(int, int);
void       Listen(int, int);
#ifdef      HAVE_POLL
int             Poll(struct pollfd *, unsigned long, int);
ssize_t       Readline(int, void *, size_t);
ssize_t       Readn(int, void *, size_t);
ssize_t       Recv(int, void *, size_t, int);
ssize_t       Recvfrom(int, void *, size_t, int, SA *, socklen_t *);
ssize_t       Recvmsg(int, struct msghdr *, int);
int             Select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
void       Send(int, const void *, size_t, int);
void       Sendto(int, const void *, size_t, int, const SA *, socklen_t);
void       Sendmsg(int, const struct msghdr *, int);
void       Setsockopt(int, int, int, const void *, socklen_t);
void       Shutdown(int, int);
int             Sockatmark(int);
int             Socket(int, int, int);
void       Socketpair(int, int, int, int *);
void       Writen(int, void *, size_t);

void       err_dump(const char *, ...);
void       err_msg(const char *, ...);
void       err_quit(const char *, ...);
void       err_ret(const char *, ...);
void       err_sys(const char *, ...);

#endif      /* __unp_h */



HW CAN I DO ....

Question by:amankhan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

255x4 earned 1500 total points
ID: 11739751
ucred.h is from the BSD world, not the GNU/Linux world.  It appears that this file exists in the various BSD kernel sources.

I would first check by going to the root (/) directory and searching for the file.

cd /
find -name ucred.h

If that did not work, I would ckeck on the Solaris CD's for any header packages, especially any kernel header packages.  Finally, I would go to one of the BSD sites and try to download that particaluar file.  I have been looking, but I believe it will be in one of the packages here:


especially the Linux_base or Linux_devtools.  These file are pretty large, so I will try and download them and find which one has the file.



Author Comment

ID: 11739875
Thank you sir 255x4

but my problem is that will i be able to use cred.h that is on solaris to make it work...


Expert Comment

ID: 13475059
where can i get header files
and sys/sockio.h
pls help quick
i m using linux
and want to make threaded server

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question