]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/sendmsg.c
Update.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / sendmsg.c
1 /* Copyright (C) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
19 #include <sys/socket.h>
20 #include <errno.h>
21 #include <unistd.h>
22
23 #include <asm/posix_types.h>
24
25 /* The kernel expects this structure in SCM_CREDS messages.
26 * Note: sizeof(struct __kernel_ucred) <= sizeof(struct cmsgcred) must hold.
27 */
28 struct __kernel_ucred
29 {
30 __kernel_pid_t pid;
31 __kernel_uid_t uid;
32 __kernel_gid_t gid;
33 };
34
35 extern int __syscall_sendmsg (int, const struct msghdr *, int);
36
37 /* Send a message described by MESSAGE on socket FD.
38 Returns the number of bytes sent, or -1 for errors. */
39 int
40 __libc_sendmsg (int fd, const struct msghdr *message, int flags)
41 {
42 struct cmsghdr *cm;
43 struct cmsgcred *cc;
44 struct __kernel_ucred *u;
45 pid_t pid;
46
47 /* Preprocess the message control block for SCM_CREDS. */
48 cm = CMSG_FIRSTHDR (message);
49 while (cm)
50 {
51 if (cm->cmsg_type == SCM_CREDS)
52 {
53 if (cm->cmsg_len < CMSG_SPACE (sizeof (struct cmsgcred)))
54 {
55 __set_errno (EINVAL);
56 return -1;
57 }
58
59 u = (struct __kernel_ucred *) CMSG_DATA (cm);
60 cc = (struct cmsgcred *) CMSG_DATA (cm);
61 /* Linux expects the calling process to pass in
62 its credentials, and sanity checks them.
63 You can send real, effective, or set- uid and gid.
64 If the user hasn't filled in the buffer, we default to
65 real uid and gid. */
66 pid = __getpid ();
67 if (cc->cmcred_pid != pid)
68 {
69 u->pid = pid;
70 u->uid = __getuid ();
71 u->gid = __getgid ();
72 }
73 else
74 {
75 struct __kernel_ucred v;
76 v.pid = cc->cmcred_pid;
77 v.uid = cc->cmcred_uid;
78 v.gid = cc->cmcred_gid;
79 u->pid = v.pid;
80 u->uid = v.uid;
81 u->gid = v.gid;
82 }
83 }
84 cm = CMSG_NXTHDR ((struct msghdr *) message, cm);
85 }
86
87 return __syscall_sendmsg (fd, message, flags);
88 }
89
90 weak_alias (__libc_sendmsg, __sendmsg)
91 weak_alias (__libc_sendmsg, sendmsg)