2012-07-17 07:00:18 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
2012-08-14 14:50:48 +04:00
|
|
|
#include <arpa/inet.h>
|
2012-08-21 16:27:07 +04:00
|
|
|
#include <ctype.h>
|
2012-07-17 07:00:18 +04:00
|
|
|
|
2012-07-18 13:04:00 +04:00
|
|
|
#include <google/protobuf-c/protobuf-c.h>
|
|
|
|
|
2012-08-07 02:00:39 +04:00
|
|
|
#include "crtools.h"
|
2012-07-17 07:00:18 +04:00
|
|
|
#include "compiler.h"
|
2013-01-09 17:02:47 +04:00
|
|
|
#include "asm/types.h"
|
2012-07-17 07:00:18 +04:00
|
|
|
#include "log.h"
|
|
|
|
#include "util.h"
|
2012-08-14 14:49:21 +04:00
|
|
|
#include "string.h"
|
2012-08-14 14:50:48 +04:00
|
|
|
#include "sockets.h"
|
2012-07-17 07:00:18 +04:00
|
|
|
|
|
|
|
#include "protobuf.h"
|
2012-08-07 02:00:39 +04:00
|
|
|
#include "protobuf/inventory.pb-c.h"
|
|
|
|
#include "protobuf/regfile.pb-c.h"
|
|
|
|
#include "protobuf/eventfd.pb-c.h"
|
|
|
|
#include "protobuf/eventpoll.pb-c.h"
|
|
|
|
#include "protobuf/signalfd.pb-c.h"
|
2013-01-14 20:47:51 +04:00
|
|
|
#include "protobuf/fsnotify.pb-c.h"
|
2012-08-07 02:00:39 +04:00
|
|
|
#include "protobuf/core.pb-c.h"
|
|
|
|
#include "protobuf/mm.pb-c.h"
|
|
|
|
#include "protobuf/pipe.pb-c.h"
|
|
|
|
#include "protobuf/fifo.pb-c.h"
|
|
|
|
#include "protobuf/fdinfo.pb-c.h"
|
|
|
|
#include "protobuf/pipe-data.pb-c.h"
|
|
|
|
#include "protobuf/pstree.pb-c.h"
|
|
|
|
#include "protobuf/sa.pb-c.h"
|
|
|
|
#include "protobuf/sk-unix.pb-c.h"
|
|
|
|
#include "protobuf/sk-inet.pb-c.h"
|
2012-08-09 16:17:41 +04:00
|
|
|
#include "protobuf/packet-sock.pb-c.h"
|
2012-08-07 02:00:39 +04:00
|
|
|
#include "protobuf/sk-packet.pb-c.h"
|
|
|
|
#include "protobuf/creds.pb-c.h"
|
|
|
|
#include "protobuf/itimer.pb-c.h"
|
|
|
|
#include "protobuf/utsns.pb-c.h"
|
|
|
|
#include "protobuf/ipc-var.pb-c.h"
|
|
|
|
#include "protobuf/ipc-shm.pb-c.h"
|
|
|
|
#include "protobuf/ipc-msg.pb-c.h"
|
|
|
|
#include "protobuf/ipc-sem.pb-c.h"
|
|
|
|
#include "protobuf/fs.pb-c.h"
|
|
|
|
#include "protobuf/remap-file-path.pb-c.h"
|
|
|
|
#include "protobuf/ghost-file.pb-c.h"
|
|
|
|
#include "protobuf/mnt.pb-c.h"
|
|
|
|
#include "protobuf/netdev.pb-c.h"
|
|
|
|
#include "protobuf/tcp-stream.pb-c.h"
|
2012-09-12 20:00:54 +04:00
|
|
|
#include "protobuf/tty.pb-c.h"
|
2013-01-17 16:09:28 +08:00
|
|
|
#include "protobuf/file-lock.pb-c.h"
|
2013-01-10 20:08:38 +04:00
|
|
|
#include "protobuf/rlimit.pb-c.h"
|
2013-03-12 21:00:05 +04:00
|
|
|
#include "protobuf/pagemap.pb-c.h"
|
2013-03-25 23:39:45 +04:00
|
|
|
#include "protobuf/siginfo.pb-c.h"
|
2013-03-25 19:28:42 +04:00
|
|
|
#include "protobuf/sk-netlink.pb-c.h"
|
2012-08-07 02:00:39 +04:00
|
|
|
|
2012-08-07 19:25:09 +04:00
|
|
|
typedef size_t (*pb_getpksize_t)(void *obj);
|
|
|
|
typedef size_t (*pb_pack_t)(void *obj, void *where);
|
|
|
|
typedef void *(*pb_unpack_t)(void *allocator, size_t size, void *from);
|
|
|
|
typedef void (*pb_free_t)(void *obj, void *allocator);
|
|
|
|
|
2012-08-07 02:00:39 +04:00
|
|
|
struct cr_pb_message_desc {
|
|
|
|
pb_getpksize_t getpksize;
|
|
|
|
pb_pack_t pack;
|
|
|
|
pb_unpack_t unpack;
|
|
|
|
pb_free_t free;
|
|
|
|
const ProtobufCMessageDescriptor *pb_desc;
|
|
|
|
};
|
|
|
|
|
2012-08-07 02:26:50 +04:00
|
|
|
#define PB_PACK_TYPECHECK(__o, __fn) ({ if (0) __fn##__pack(__o, NULL); (pb_pack_t)&__fn##__pack; })
|
|
|
|
#define PB_GPS_TYPECHECK(__o, __fn) ({ if (0) __fn##__get_packed_size(__o); (pb_getpksize_t)&__fn##__get_packed_size; })
|
2012-08-07 02:42:58 +04:00
|
|
|
#define PB_UNPACK_TYPECHECK(__op, __fn) ({ if (0) *__op = __fn##__unpack(NULL, 0, NULL); (pb_unpack_t)&__fn##__unpack; })
|
|
|
|
#define PB_FREE_TYPECHECK(__o, __fn) ({ if (0) __fn##__free_unpacked(__o, NULL); (pb_free_t)&__fn##__free_unpacked; })
|
2012-08-07 02:26:50 +04:00
|
|
|
|
2012-08-07 02:00:39 +04:00
|
|
|
/*
|
|
|
|
* This should be explicitly "called" to do type-checking
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CR_PB_MDESC_INIT(__var, __type, __name) do { \
|
|
|
|
__var.getpksize = PB_GPS_TYPECHECK((__type *)NULL, __name); \
|
|
|
|
__var.pack = PB_PACK_TYPECHECK((__type *)NULL, __name); \
|
|
|
|
__var.unpack = PB_UNPACK_TYPECHECK((__type **)NULL, __name); \
|
|
|
|
__var.free = PB_FREE_TYPECHECK((__type *)NULL, __name); \
|
|
|
|
__var.pb_desc = &__name##__descriptor; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static struct cr_pb_message_desc cr_pb_descs[PB_MAX];
|
|
|
|
|
|
|
|
#define CR_PB_DESC(__type, __vtype, __ftype) \
|
|
|
|
CR_PB_MDESC_INIT(cr_pb_descs[PB_##__type], __vtype##Entry, __ftype##_entry)
|
|
|
|
|
|
|
|
void cr_pb_init(void)
|
|
|
|
{
|
|
|
|
CR_PB_DESC(INVENTORY, Inventory, inventory);
|
|
|
|
CR_PB_DESC(FDINFO, Fdinfo, fdinfo);
|
|
|
|
CR_PB_DESC(REG_FILES, RegFile, reg_file);
|
|
|
|
CR_PB_DESC(EVENTFD, EventfdFile, eventfd_file);
|
|
|
|
CR_PB_DESC(EVENTPOLL, EventpollFile, eventpoll_file);
|
|
|
|
CR_PB_DESC(EVENTPOLL_TFD, EventpollTfd, eventpoll_tfd);
|
|
|
|
CR_PB_DESC(SIGNALFD, Signalfd, signalfd);
|
|
|
|
CR_PB_DESC(INOTIFY, InotifyFile, inotify_file);
|
|
|
|
CR_PB_DESC(INOTIFY_WD, InotifyWd, inotify_wd);
|
2013-01-14 20:47:56 +04:00
|
|
|
CR_PB_DESC(FANOTIFY, FanotifyFile, fanotify_file);
|
|
|
|
CR_PB_DESC(FANOTIFY_MARK, FanotifyMark, fanotify_mark);
|
2012-08-07 02:00:39 +04:00
|
|
|
CR_PB_DESC(CORE, Core, core);
|
2013-01-11 18:16:21 +04:00
|
|
|
CR_PB_DESC(IDS, TaskKobjIds, task_kobj_ids);
|
2012-08-07 02:00:39 +04:00
|
|
|
CR_PB_DESC(MM, Mm, mm);
|
|
|
|
CR_PB_DESC(VMAS, Vma, vma);
|
|
|
|
CR_PB_DESC(PIPES, Pipe, pipe);
|
|
|
|
CR_PB_DESC(PIPES_DATA, PipeData, pipe_data);
|
|
|
|
CR_PB_DESC(FIFO, Fifo, fifo);
|
|
|
|
CR_PB_DESC(PSTREE, Pstree, pstree);
|
|
|
|
CR_PB_DESC(SIGACT, Sa, sa);
|
|
|
|
CR_PB_DESC(UNIXSK, UnixSk, unix_sk);
|
|
|
|
CR_PB_DESC(INETSK, InetSk, inet_sk);
|
|
|
|
CR_PB_DESC(SK_QUEUES, SkPacket, sk_packet);
|
|
|
|
CR_PB_DESC(ITIMERS, Itimer, itimer);
|
|
|
|
CR_PB_DESC(CREDS, Creds, creds);
|
|
|
|
CR_PB_DESC(UTSNS, Utsns, utsns);
|
|
|
|
CR_PB_DESC(IPCNS_VAR, IpcVar, ipc_var);
|
|
|
|
CR_PB_DESC(IPCNS_SHM, IpcShm, ipc_shm);
|
|
|
|
/* There's no _entry suffix in this one :( */
|
2012-11-11 13:28:59 +04:00
|
|
|
CR_PB_MDESC_INIT(cr_pb_descs[PB_IPCNS_MSG], IpcMsg, ipc_msg);
|
2012-08-07 02:00:39 +04:00
|
|
|
CR_PB_DESC(IPCNS_MSG_ENT, IpcMsg, ipc_msg);
|
|
|
|
CR_PB_DESC(IPCNS_SEM, IpcSem, ipc_sem);
|
|
|
|
CR_PB_DESC(FS, Fs, fs);
|
|
|
|
CR_PB_DESC(REMAP_FPATH, RemapFilePath, remap_file_path);
|
|
|
|
CR_PB_DESC(GHOST_FILE, GhostFile, ghost_file);
|
|
|
|
CR_PB_DESC(TCP_STREAM, TcpStream, tcp_stream);
|
|
|
|
CR_PB_DESC(MOUNTPOINTS, Mnt, mnt);
|
|
|
|
CR_PB_DESC(NETDEV, NetDevice, net_device);
|
2012-08-09 16:17:41 +04:00
|
|
|
CR_PB_DESC(PACKETSK, PacketSock, packet_sock);
|
2012-09-12 20:00:54 +04:00
|
|
|
CR_PB_DESC(TTY, TtyFile, tty_file);
|
|
|
|
CR_PB_DESC(TTY_INFO, TtyInfo, tty_info);
|
2013-01-17 16:09:28 +08:00
|
|
|
CR_PB_DESC(FILE_LOCK, FileLock, file_lock);
|
2013-01-10 20:08:38 +04:00
|
|
|
CR_PB_DESC(RLIMIT, Rlimit, rlimit);
|
2013-03-12 21:00:05 +04:00
|
|
|
CR_PB_MDESC_INIT(cr_pb_descs[PB_PAGEMAP_HEAD], PagemapHead, pagemap_head);
|
|
|
|
CR_PB_DESC(PAGEMAP, Pagemap, pagemap);
|
2013-03-25 23:39:45 +04:00
|
|
|
CR_PB_DESC(SIGINFO, Siginfo, siginfo);
|
2013-03-25 19:28:42 +04:00
|
|
|
CR_PB_DESC(NETLINKSK, NetlinkSk, netlink_sk);
|
2012-08-07 02:00:39 +04:00
|
|
|
}
|
2012-07-17 07:00:18 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* To speed up reading of packed objects
|
|
|
|
* by providing space on stack, this should
|
|
|
|
* be more than enough for most objects.
|
|
|
|
*/
|
|
|
|
#define PB_PKOBJ_LOCAL_SIZE 1024
|
|
|
|
|
2012-08-14 14:50:48 +04:00
|
|
|
#define INET_ADDR_LEN 40
|
|
|
|
|
2012-08-14 14:45:29 +04:00
|
|
|
struct pb_pr_field_s {
|
2012-08-14 14:45:47 +04:00
|
|
|
void *data;
|
2012-08-14 14:49:21 +04:00
|
|
|
int number;
|
2012-08-14 14:45:29 +04:00
|
|
|
int depth;
|
2012-08-14 14:49:21 +04:00
|
|
|
int count;
|
|
|
|
char fmt[32];
|
2012-08-14 14:45:29 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct pb_pr_field_s pb_pr_field_t;
|
|
|
|
|
2012-07-25 18:28:12 +04:00
|
|
|
struct pb_pr_ctrl_s {
|
|
|
|
void *arg;
|
2012-07-25 18:28:14 +04:00
|
|
|
int single_entry;
|
2012-08-14 14:49:21 +04:00
|
|
|
const char *pretty_fmt;
|
2012-08-14 14:45:29 +04:00
|
|
|
pb_pr_field_t cur;
|
2012-07-25 18:28:12 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct pb_pr_ctrl_s pb_pr_ctl_t;
|
2012-08-14 14:49:21 +04:00
|
|
|
typedef int (*pb_pr_show_t)(pb_pr_field_t *field);
|
2012-07-18 13:04:00 +04:00
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
static int pb_msg_int32x(pb_pr_field_t *field)
|
2012-07-18 13:04:00 +04:00
|
|
|
{
|
2012-08-14 14:47:40 +04:00
|
|
|
pr_msg("%#x", *(int *)field->data);
|
2012-08-14 14:49:21 +04:00
|
|
|
return 0;
|
2012-07-18 13:04:00 +04:00
|
|
|
}
|
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
static int pb_msg_int64x(pb_pr_field_t *field)
|
2012-07-18 13:04:00 +04:00
|
|
|
{
|
2012-08-14 14:47:40 +04:00
|
|
|
pr_msg("%#016lx", *(long *)field->data);
|
2012-08-14 14:49:21 +04:00
|
|
|
return 0;
|
2012-07-18 13:04:00 +04:00
|
|
|
}
|
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
static int pb_msg_string(pb_pr_field_t *field)
|
2012-07-18 13:04:00 +04:00
|
|
|
{
|
2012-08-14 14:47:40 +04:00
|
|
|
pr_msg("\"%s\"", *(char **)field->data);
|
2012-08-14 14:49:21 +04:00
|
|
|
return 0;
|
2012-07-18 13:04:00 +04:00
|
|
|
}
|
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
static int pb_msg_unk(pb_pr_field_t *field)
|
2012-07-18 13:04:00 +04:00
|
|
|
{
|
2012-08-14 14:47:40 +04:00
|
|
|
pr_msg("unknown object %p", field->data);
|
2012-08-14 14:49:21 +04:00
|
|
|
return 0;
|
2012-07-18 13:04:00 +04:00
|
|
|
}
|
|
|
|
|
2012-07-25 18:28:15 +04:00
|
|
|
static inline void print_tabs(pb_pr_ctl_t *ctl)
|
2012-07-25 18:28:14 +04:00
|
|
|
{
|
2012-08-14 14:45:29 +04:00
|
|
|
int counter = ctl->cur.depth;
|
2012-07-25 18:28:15 +04:00
|
|
|
|
|
|
|
if (!ctl->single_entry)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (counter--)
|
|
|
|
pr_msg("\t");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_nested_message_braces(pb_pr_ctl_t *ctl, int right_brace)
|
|
|
|
{
|
|
|
|
if (right_brace)
|
|
|
|
print_tabs(ctl);
|
2012-07-25 18:28:17 +04:00
|
|
|
pr_msg("%s%s", (right_brace) ? "}" : "{", (ctl->single_entry) ? "\n" : " ");
|
2012-07-25 18:28:14 +04:00
|
|
|
}
|
|
|
|
|
2012-07-25 18:28:12 +04:00
|
|
|
static void pb_show_msg(const void *msg, pb_pr_ctl_t *ctl);
|
2012-07-18 13:04:00 +04:00
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
static int show_nested_message(pb_pr_field_t *field)
|
2012-07-18 13:04:00 +04:00
|
|
|
{
|
2012-08-14 14:47:40 +04:00
|
|
|
pb_pr_ctl_t *ctl = container_of(field, pb_pr_ctl_t, cur);
|
|
|
|
|
2012-07-25 18:28:14 +04:00
|
|
|
print_nested_message_braces(ctl, 0);
|
2012-08-14 14:47:40 +04:00
|
|
|
field->depth++;
|
|
|
|
pb_show_msg(field->data, ctl);
|
|
|
|
field->depth--;
|
2012-07-25 18:28:14 +04:00
|
|
|
print_nested_message_braces(ctl, 1);
|
2012-08-14 14:49:21 +04:00
|
|
|
return 0;
|
2012-07-18 13:04:00 +04:00
|
|
|
}
|
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
static int show_enum(pb_pr_field_t *field)
|
2012-07-19 10:17:40 +04:00
|
|
|
{
|
2012-08-14 14:47:40 +04:00
|
|
|
pb_pr_ctl_t *ctl = container_of(field, pb_pr_ctl_t, cur);
|
2012-07-25 18:28:12 +04:00
|
|
|
ProtobufCEnumDescriptor *d = ctl->arg;
|
2012-07-19 10:17:40 +04:00
|
|
|
const char *val_name = NULL;
|
|
|
|
int val, i;
|
|
|
|
|
2012-08-14 14:47:40 +04:00
|
|
|
val = *(int *)field->data;
|
2012-07-19 10:17:40 +04:00
|
|
|
for (i = 0; i < d->n_values; i++)
|
|
|
|
if (d->values[i].value == val) {
|
|
|
|
val_name = d->values[i].name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val_name != NULL)
|
|
|
|
pr_msg("%s", val_name);
|
|
|
|
else
|
|
|
|
pr_msg("%d", val);
|
2012-08-14 14:49:21 +04:00
|
|
|
return 0;
|
2012-07-19 10:17:40 +04:00
|
|
|
}
|
2012-07-18 13:04:00 +04:00
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
static int show_bool(pb_pr_field_t *field)
|
2012-08-09 18:12:22 +04:00
|
|
|
{
|
2012-08-14 14:47:40 +04:00
|
|
|
protobuf_c_boolean val = *(protobuf_c_boolean *)field->data;
|
2012-08-09 18:12:22 +04:00
|
|
|
|
|
|
|
if (val)
|
|
|
|
pr_msg("True");
|
|
|
|
else
|
|
|
|
pr_msg("False");
|
2012-08-14 14:49:21 +04:00
|
|
|
return 0;
|
2012-08-09 18:12:22 +04:00
|
|
|
}
|
|
|
|
|
2012-08-21 16:27:14 +04:00
|
|
|
static int show_bytes(pb_pr_field_t *field)
|
|
|
|
{
|
|
|
|
ProtobufCBinaryData *bytes = (ProtobufCBinaryData *)field->data;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (i < bytes->len)
|
|
|
|
pr_msg("%02x ", bytes->data[i++]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-14 14:46:47 +04:00
|
|
|
static size_t pb_show_prepare_field_context(const ProtobufCFieldDescriptor *fd,
|
|
|
|
pb_pr_ctl_t *ctl)
|
|
|
|
{
|
|
|
|
pb_pr_field_t *field = &ctl->cur;
|
2012-08-14 14:52:21 +04:00
|
|
|
size_t fsize = 0;
|
2012-08-14 14:46:47 +04:00
|
|
|
|
|
|
|
switch (fd->type) {
|
|
|
|
case PROTOBUF_C_TYPE_ENUM:
|
|
|
|
ctl->arg = (void *)fd->descriptor;
|
|
|
|
case PROTOBUF_C_TYPE_INT32:
|
|
|
|
case PROTOBUF_C_TYPE_SINT32:
|
|
|
|
case PROTOBUF_C_TYPE_UINT32:
|
|
|
|
case PROTOBUF_C_TYPE_SFIXED32:
|
|
|
|
case PROTOBUF_C_TYPE_FLOAT:
|
|
|
|
fsize = 4;
|
|
|
|
break;
|
|
|
|
case PROTOBUF_C_TYPE_INT64:
|
|
|
|
case PROTOBUF_C_TYPE_SINT64:
|
|
|
|
case PROTOBUF_C_TYPE_SFIXED64:
|
|
|
|
case PROTOBUF_C_TYPE_FIXED32:
|
|
|
|
case PROTOBUF_C_TYPE_UINT64:
|
|
|
|
case PROTOBUF_C_TYPE_FIXED64:
|
|
|
|
case PROTOBUF_C_TYPE_DOUBLE:
|
|
|
|
fsize = 8;
|
|
|
|
break;
|
|
|
|
case PROTOBUF_C_TYPE_MESSAGE:
|
|
|
|
ctl->arg = (void *)fd->descriptor;
|
|
|
|
field->data = (void *)(*(long *)field->data);
|
|
|
|
case PROTOBUF_C_TYPE_STRING:
|
|
|
|
fsize = sizeof (void *);
|
|
|
|
break;
|
|
|
|
case PROTOBUF_C_TYPE_BOOL:
|
|
|
|
fsize = sizeof (protobuf_c_boolean);
|
|
|
|
break;
|
|
|
|
case PROTOBUF_C_TYPE_BYTES:
|
|
|
|
fsize = sizeof (ProtobufCBinaryData);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
return fsize;
|
|
|
|
}
|
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
static int pb_show_pretty(pb_pr_field_t *field)
|
|
|
|
{
|
2012-08-14 14:50:48 +04:00
|
|
|
switch (field->fmt[0]) {
|
|
|
|
case '%':
|
|
|
|
pr_msg(field->fmt, *(long *)field->data);
|
|
|
|
break;
|
2012-08-21 16:27:07 +04:00
|
|
|
case 'S':
|
|
|
|
{
|
|
|
|
ProtobufCBinaryData *name = (ProtobufCBinaryData *)field->data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < name->len; i++) {
|
|
|
|
char c = (char)name->data[i];
|
|
|
|
|
|
|
|
if (isprint(c))
|
|
|
|
pr_msg("%c", c);
|
|
|
|
else if (c != 0)
|
|
|
|
pr_msg(".");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-08-14 14:50:48 +04:00
|
|
|
case 'A':
|
|
|
|
{
|
|
|
|
char addr[INET_ADDR_LEN] = "<unknown>";
|
|
|
|
int family = (field->count == 1) ? AF_INET : AF_INET6;
|
|
|
|
|
|
|
|
if (inet_ntop(family, (void *)field->data, addr,
|
|
|
|
INET_ADDR_LEN) == NULL)
|
|
|
|
pr_msg("failed to translate");
|
|
|
|
else
|
|
|
|
pr_msg("%s", addr);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2012-08-14 14:49:21 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pb_field_show_pretty(pb_pr_ctl_t *ctl)
|
|
|
|
{
|
|
|
|
pb_pr_field_t *field = &ctl->cur;
|
|
|
|
int found;
|
|
|
|
char cookie[32];
|
|
|
|
const char *ptr;
|
|
|
|
|
2012-10-12 20:48:32 +04:00
|
|
|
if (!ctl->pretty_fmt || field->depth)
|
2012-08-14 14:49:21 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
sprintf(cookie, " %d:", field->number);
|
|
|
|
if (!strncmp(ctl->pretty_fmt, &cookie[1], strlen(&cookie[1])))
|
|
|
|
ptr = ctl->pretty_fmt;
|
|
|
|
else {
|
|
|
|
ptr = strstr(ctl->pretty_fmt, cookie);
|
|
|
|
if (!ptr)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
found = sscanf(ptr, "%*[ 1-9:]%s", field->fmt);
|
|
|
|
BUG_ON(found > 1);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2012-08-14 14:46:49 +04:00
|
|
|
static pb_pr_show_t get_pb_show_function(int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case PROTOBUF_C_TYPE_INT32:
|
|
|
|
case PROTOBUF_C_TYPE_SINT32:
|
|
|
|
case PROTOBUF_C_TYPE_UINT32:
|
|
|
|
case PROTOBUF_C_TYPE_SFIXED32:
|
|
|
|
return pb_msg_int32x;
|
|
|
|
case PROTOBUF_C_TYPE_INT64:
|
|
|
|
case PROTOBUF_C_TYPE_SINT64:
|
|
|
|
case PROTOBUF_C_TYPE_SFIXED64:
|
|
|
|
case PROTOBUF_C_TYPE_FIXED32:
|
|
|
|
case PROTOBUF_C_TYPE_UINT64:
|
|
|
|
case PROTOBUF_C_TYPE_FIXED64:
|
|
|
|
return pb_msg_int64x;
|
|
|
|
case PROTOBUF_C_TYPE_STRING:
|
|
|
|
return pb_msg_string;
|
|
|
|
case PROTOBUF_C_TYPE_MESSAGE:
|
|
|
|
return show_nested_message;
|
|
|
|
case PROTOBUF_C_TYPE_ENUM:
|
|
|
|
return show_enum;
|
2012-08-15 20:53:35 +04:00
|
|
|
case PROTOBUF_C_TYPE_BOOL:
|
|
|
|
return show_bool;
|
2012-08-21 16:27:14 +04:00
|
|
|
case PROTOBUF_C_TYPE_BYTES:
|
|
|
|
return show_bytes;
|
2012-08-14 14:46:49 +04:00
|
|
|
case PROTOBUF_C_TYPE_FLOAT:
|
|
|
|
case PROTOBUF_C_TYPE_DOUBLE:
|
2012-08-15 20:53:35 +04:00
|
|
|
break;
|
2012-08-14 14:46:49 +04:00
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
return pb_msg_unk;
|
|
|
|
}
|
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
static pb_pr_show_t get_show_function(int type, pb_pr_ctl_t *ctl)
|
|
|
|
{
|
|
|
|
if (pb_field_show_pretty(ctl))
|
|
|
|
return pb_show_pretty;
|
|
|
|
return get_pb_show_function(type);
|
|
|
|
}
|
|
|
|
|
2012-08-14 14:47:03 +04:00
|
|
|
static void pb_show_repeated(pb_pr_ctl_t *ctl, int nr_fields, pb_pr_show_t show,
|
2012-08-14 14:49:21 +04:00
|
|
|
size_t fsize)
|
2012-07-18 13:04:00 +04:00
|
|
|
{
|
2012-08-14 14:45:47 +04:00
|
|
|
pb_pr_field_t *field = &ctl->cur;
|
2012-07-18 13:04:00 +04:00
|
|
|
unsigned long counter;
|
2012-08-14 14:49:21 +04:00
|
|
|
int done;
|
|
|
|
|
2012-11-06 16:46:42 +04:00
|
|
|
if (nr_fields == 0) {
|
|
|
|
pr_msg("<empty>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
field->count = nr_fields;
|
|
|
|
done = show(field);
|
|
|
|
if (done)
|
|
|
|
return;
|
2012-07-18 13:04:00 +04:00
|
|
|
|
2012-08-14 14:45:47 +04:00
|
|
|
field->data += fsize;
|
2012-07-18 13:04:00 +04:00
|
|
|
|
2012-08-14 14:45:47 +04:00
|
|
|
for (counter = 0; counter < nr_fields - 1; counter++, field->data += fsize) {
|
2012-07-18 13:04:00 +04:00
|
|
|
pr_msg(":");
|
2012-08-14 14:47:40 +04:00
|
|
|
show(field);
|
2012-07-18 13:04:00 +04:00
|
|
|
}
|
2012-08-14 14:47:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pb_show_field(const ProtobufCFieldDescriptor *fd,
|
|
|
|
int nr_fields, pb_pr_ctl_t *ctl)
|
|
|
|
{
|
|
|
|
pb_pr_show_t show;
|
|
|
|
|
|
|
|
print_tabs(ctl);
|
|
|
|
pr_msg("%s: ", fd->name);
|
|
|
|
|
2012-08-14 14:49:21 +04:00
|
|
|
show = get_show_function(fd->type, ctl);
|
2012-08-14 14:47:03 +04:00
|
|
|
|
|
|
|
pb_show_repeated(ctl, nr_fields, show, pb_show_prepare_field_context(fd, ctl));
|
2012-07-18 13:04:00 +04:00
|
|
|
|
2012-07-25 18:28:14 +04:00
|
|
|
if (ctl->single_entry)
|
|
|
|
pr_msg("\n");
|
|
|
|
else
|
|
|
|
pr_msg(" ");
|
2012-07-18 13:04:00 +04:00
|
|
|
}
|
|
|
|
|
2012-07-25 18:28:15 +04:00
|
|
|
static int pb_optional_field_present(const ProtobufCFieldDescriptor *field,
|
2012-08-09 22:40:18 +04:00
|
|
|
const void *msg)
|
2012-07-25 18:28:15 +04:00
|
|
|
{
|
|
|
|
if ((field->type == PROTOBUF_C_TYPE_MESSAGE) ||
|
|
|
|
(field->type == PROTOBUF_C_TYPE_STRING)) {
|
|
|
|
const void *opt_flag = * (const void * const *)(msg + field->offset);
|
|
|
|
|
|
|
|
if ((opt_flag == NULL) || (opt_flag == field->default_value))
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
const protobuf_c_boolean *has = msg + field->quantifier_offset;
|
|
|
|
|
|
|
|
if (!*has)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-25 18:28:12 +04:00
|
|
|
static void pb_show_msg(const void *msg, pb_pr_ctl_t *ctl)
|
2012-07-18 13:04:00 +04:00
|
|
|
{
|
|
|
|
int i;
|
2012-07-25 18:28:12 +04:00
|
|
|
const ProtobufCMessageDescriptor *md = ctl->arg;
|
2012-07-18 13:04:00 +04:00
|
|
|
|
|
|
|
BUG_ON(md == NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < md->n_fields; i++) {
|
|
|
|
const ProtobufCFieldDescriptor fd = md->fields[i];
|
|
|
|
unsigned long *data;
|
|
|
|
size_t nr_fields;
|
|
|
|
|
|
|
|
nr_fields = 1;
|
|
|
|
data = (unsigned long *)(msg + fd.offset);
|
|
|
|
|
2012-07-25 18:28:15 +04:00
|
|
|
if (fd.label == PROTOBUF_C_LABEL_OPTIONAL) {
|
2012-08-09 22:40:18 +04:00
|
|
|
if (!pb_optional_field_present(&fd, msg))
|
2012-07-25 18:28:15 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-18 13:04:00 +04:00
|
|
|
if (fd.label == PROTOBUF_C_LABEL_REPEATED) {
|
|
|
|
nr_fields = *(size_t *)(msg + fd.quantifier_offset);
|
|
|
|
data = (unsigned long *)*data;
|
|
|
|
}
|
|
|
|
|
2012-08-14 14:45:47 +04:00
|
|
|
ctl->cur.data = data;
|
2012-08-14 14:49:21 +04:00
|
|
|
ctl->cur.number = i + 1;
|
2012-08-14 14:45:47 +04:00
|
|
|
|
|
|
|
pb_show_field(&fd, nr_fields, ctl);
|
2012-07-18 13:04:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 12:43:00 +04:00
|
|
|
static inline void pb_no_payload(int fd, void *obj, int flags) { }
|
2012-07-25 18:32:41 +04:00
|
|
|
|
2012-08-07 02:51:34 +04:00
|
|
|
void do_pb_show_plain(int fd, int type, int single_entry,
|
2012-07-26 12:43:00 +04:00
|
|
|
void (*payload_hadler)(int fd, void *obj, int flags),
|
2012-08-14 14:49:21 +04:00
|
|
|
int flags, const char *pretty_fmt)
|
2012-07-18 19:09:51 +04:00
|
|
|
{
|
2012-08-14 14:49:21 +04:00
|
|
|
pb_pr_ctl_t ctl = {NULL, single_entry, pretty_fmt};
|
2012-07-26 12:43:00 +04:00
|
|
|
void (*handle_payload)(int fd, void *obj, int flags);
|
|
|
|
|
2012-08-07 02:51:34 +04:00
|
|
|
if (!cr_pb_descs[type].pb_desc) {
|
|
|
|
pr_err("Wrong object requested %d\n", type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-26 12:43:00 +04:00
|
|
|
handle_payload = (payload_hadler) ? : pb_no_payload;
|
2012-07-25 18:28:12 +04:00
|
|
|
|
2012-07-18 19:09:51 +04:00
|
|
|
while (1) {
|
|
|
|
void *obj;
|
|
|
|
|
2012-08-07 02:51:34 +04:00
|
|
|
if (pb_read_one_eof(fd, &obj, type) <= 0)
|
2012-07-18 19:09:51 +04:00
|
|
|
break;
|
|
|
|
|
2012-08-07 02:51:34 +04:00
|
|
|
ctl.arg = (void *)cr_pb_descs[type].pb_desc;
|
2012-07-25 18:28:12 +04:00
|
|
|
pb_show_msg(obj, &ctl);
|
2012-07-26 12:43:00 +04:00
|
|
|
handle_payload(fd, obj, flags);
|
2012-08-07 02:51:34 +04:00
|
|
|
cr_pb_descs[type].free(obj, NULL);
|
2012-07-25 18:28:14 +04:00
|
|
|
if (single_entry)
|
|
|
|
break;
|
|
|
|
pr_msg("\n");
|
2012-07-18 19:09:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-15 15:03:17 +04:00
|
|
|
static char *image_name(int fd)
|
|
|
|
{
|
|
|
|
static char image_path[PATH_MAX];
|
|
|
|
|
2013-05-07 19:01:24 +04:00
|
|
|
if (read_fd_link(fd, image_path, sizeof(image_path)) > 0)
|
2013-03-15 15:03:17 +04:00
|
|
|
return image_path;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-17 07:00:18 +04:00
|
|
|
/*
|
|
|
|
* Reads PB record (header + packed object) from file @fd and unpack
|
|
|
|
* it with @unpack procedure to the pointer @pobj
|
|
|
|
*
|
|
|
|
* 1 on success
|
|
|
|
* -1 on error (or EOF met and @eof set to false)
|
|
|
|
* 0 on EOF and @eof set to true
|
|
|
|
*
|
|
|
|
* Don't forget to free memory granted to unpacked object in calling code if needed
|
|
|
|
*/
|
2012-08-07 02:51:34 +04:00
|
|
|
|
|
|
|
int do_pb_read_one(int fd, void **pobj, int type, bool eof)
|
2012-07-17 07:00:18 +04:00
|
|
|
{
|
|
|
|
u8 local[PB_PKOBJ_LOCAL_SIZE];
|
|
|
|
void *buf = (void *)&local;
|
|
|
|
u32 size;
|
|
|
|
int ret;
|
|
|
|
|
2012-08-07 02:51:34 +04:00
|
|
|
if (!cr_pb_descs[type].pb_desc) {
|
2013-03-15 15:03:17 +04:00
|
|
|
pr_err("Wrong object requested %d on %s\n",
|
|
|
|
type, image_name(fd));
|
2012-08-07 02:51:34 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-17 07:00:18 +04:00
|
|
|
*pobj = NULL;
|
|
|
|
|
|
|
|
ret = read(fd, &size, sizeof(size));
|
|
|
|
if (ret == 0) {
|
|
|
|
if (eof) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2013-03-15 15:03:17 +04:00
|
|
|
pr_err("Unexpected EOF on %s\n",
|
|
|
|
image_name(fd));
|
2012-07-17 07:00:18 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (ret < sizeof(size)) {
|
2013-03-15 15:03:17 +04:00
|
|
|
pr_perror("Read %d bytes while %d expected on %s",
|
|
|
|
ret, (int)sizeof(size),
|
|
|
|
image_name(fd));
|
2012-07-17 07:00:18 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > sizeof(local)) {
|
|
|
|
ret = -1;
|
|
|
|
buf = xmalloc(size);
|
|
|
|
if (!buf)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read(fd, buf, size);
|
|
|
|
if (ret < 0) {
|
2013-03-15 15:03:17 +04:00
|
|
|
pr_perror("Can't read %d bytes from file %s",
|
|
|
|
size, image_name(fd));
|
2012-07-17 07:00:18 +04:00
|
|
|
goto err;
|
|
|
|
} else if (ret != size) {
|
2013-03-15 15:03:17 +04:00
|
|
|
pr_perror("Read %d bytes while %d expected from %s",
|
|
|
|
ret, size, image_name(fd));
|
2012-11-11 13:28:25 +04:00
|
|
|
ret = -1;
|
2012-07-17 07:00:18 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-08-07 02:51:34 +04:00
|
|
|
*pobj = cr_pb_descs[type].unpack(NULL, size, buf);
|
2012-07-17 07:00:18 +04:00
|
|
|
if (!*pobj) {
|
|
|
|
ret = -1;
|
2013-03-15 15:03:17 +04:00
|
|
|
pr_err("Failed unpacking object %p from %s\n",
|
|
|
|
pobj, image_name(fd));
|
2012-07-17 07:00:18 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
err:
|
|
|
|
if (buf != (void *)&local)
|
|
|
|
xfree(buf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Writes PB record (header + packed object pointed by @obj)
|
|
|
|
* to file @fd, using @getpksize to get packed size and @pack
|
|
|
|
* to implement packing
|
|
|
|
*
|
|
|
|
* 0 on success
|
|
|
|
* -1 on error
|
|
|
|
*/
|
2012-08-07 02:26:50 +04:00
|
|
|
int pb_write_one(int fd, void *obj, int type)
|
2012-07-17 07:00:18 +04:00
|
|
|
{
|
|
|
|
u8 local[PB_PKOBJ_LOCAL_SIZE];
|
|
|
|
void *buf = (void *)&local;
|
|
|
|
u32 size, packed;
|
|
|
|
int ret = -1;
|
|
|
|
|
2012-08-07 02:26:50 +04:00
|
|
|
if (!cr_pb_descs[type].pb_desc) {
|
2012-10-03 16:04:48 +04:00
|
|
|
pr_err("Wrong object requested %d\n", type);
|
2012-08-07 02:26:50 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = cr_pb_descs[type].getpksize(obj);
|
2012-07-17 07:00:18 +04:00
|
|
|
if (size > (u32)sizeof(local)) {
|
|
|
|
buf = xmalloc(size);
|
|
|
|
if (!buf)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-08-07 02:26:50 +04:00
|
|
|
packed = cr_pb_descs[type].pack(obj, buf);
|
2012-07-17 07:00:18 +04:00
|
|
|
if (packed != size) {
|
|
|
|
pr_err("Failed packing PB object %p\n", obj);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write(fd, &size, sizeof(size));
|
|
|
|
if (ret != sizeof(size)) {
|
|
|
|
ret = -1;
|
|
|
|
pr_perror("Can't write %d bytes", (int)sizeof(size));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write(fd, buf, size);
|
|
|
|
if (ret != size) {
|
|
|
|
ret = -1;
|
|
|
|
pr_perror("Can't write %d bytes", size);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
err:
|
|
|
|
if (buf != (void *)&local)
|
|
|
|
xfree(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-08-09 13:16:46 +04:00
|
|
|
|
2012-09-02 17:23:50 +04:00
|
|
|
static int __collect_image(int fd_t, int obj_t, unsigned size,
|
|
|
|
int (*collect)(void *obj, ProtobufCMessage *msg),
|
|
|
|
void * (*alloc)(size_t size),
|
|
|
|
void (*free)(void *ptr))
|
2012-08-09 13:16:46 +04:00
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
|
2013-04-09 11:13:51 +04:00
|
|
|
fd = open_image(fd_t, O_RSTR);
|
2012-08-09 13:16:46 +04:00
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
void *obj;
|
|
|
|
ProtobufCMessage *msg;
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
ret = -1;
|
2012-09-02 17:23:50 +04:00
|
|
|
obj = alloc(size);
|
2012-08-09 13:16:46 +04:00
|
|
|
if (!obj)
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
obj = NULL;
|
|
|
|
|
|
|
|
ret = pb_read_one_eof(fd, &msg, obj_t);
|
|
|
|
if (ret <= 0) {
|
2012-09-02 17:23:50 +04:00
|
|
|
free(obj);
|
2012-08-09 13:16:46 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = collect(obj, msg);
|
|
|
|
if (ret < 0) {
|
2012-09-02 17:23:50 +04:00
|
|
|
free(obj);
|
2012-08-09 13:16:46 +04:00
|
|
|
cr_pb_descs[obj_t].free(msg, NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-09-02 17:23:50 +04:00
|
|
|
|
|
|
|
int collect_image(int fd_t, int obj_t, unsigned size,
|
|
|
|
int (*collect)(void *obj, ProtobufCMessage *msg))
|
|
|
|
{
|
|
|
|
return __collect_image(fd_t, obj_t, size, collect, malloc, free);
|
|
|
|
}
|
|
|
|
|
|
|
|
int collect_image_sh(int fd_t, int obj_t, unsigned size,
|
|
|
|
int (*collect)(void *obj, ProtobufCMessage *msg))
|
|
|
|
{
|
|
|
|
return __collect_image(fd_t, obj_t, size, collect, shmalloc, shfree_last);
|
|
|
|
}
|