[CRIU] [PATCH 2/2] protobuf: Move protobuf definitions to own files

Cyrill Gorcunov gorcunov at openvz.org
Tue May 7 15:45:46 EDT 2013


On Tue, May 07, 2013 at 11:33:05PM +0400, Pavel Emelyanov wrote:
> 
> I like another rule of a thumb -- don't break solid component into pieces without
> need. Please, list problems you have with using existing pb_ engine.

Here is my protobuf.c

#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>

#include "compiler.h"
#include "xmalloc.h"
#include "types.h"
#include "log.h"

#include "protobuf.h"

/*
 * 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
 */
int pb_write_one(int fd, void *obj, int type)
{
	u8 local[1024];
	void *buf = (void *)&local;
	u32 size, packed;
	int ret = -1;

	if (!cr_pb_descs[type].pb_desc) {
		pr_err("Wrong object requested %d\n", type);
		return -1;
	}

	size = cr_pb_descs[type].getpksize(obj);
	if (size > (u32)sizeof(local)) {
		buf = xmalloc(size);
		if (!buf)
			goto err;
	}

	packed = cr_pb_descs[type].pack(obj, buf);
	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;
}

That's all I need. But with protobuf.c from crtools I need to carry additional
headers (util.h, sockets.h) which have external routines references and I can't
simply compile it without bringing in a bunch of stub-routines simply to resolve
the ext.refs.

Anyway, I think better solution will be simply not touch crtools code at all,
thus lets do a deal -- drop this series completely, I rather pick up the things
I need into cpt2 directly. Sharing .o files is never a good idea until these are
not designed to be a part of library.


More information about the CRIU mailing list