Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json_type changes during inter process communication. #412

Closed
boddumanohar opened this issue May 3, 2018 · 3 comments
Closed

json_type changes during inter process communication. #412

boddumanohar opened this issue May 3, 2018 · 3 comments

Comments

@boddumanohar
Copy link

Hi, I am working on two programs that communicate with each other using IPC. when I am putting json_object in the IPC buffer, I checked the type as 4(json_type_object). But when I check the object type in receiver side, then I get the type as 0 (json_type_null).

/// server.c   
// compiled with gcc server.c -o server -ljson-c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <json-c/json.h>

#define MAXSIZE 40 

void die(char *s)
{
    perror(s);
	printf("die");
    exit(1);
}

int main()
{
    char c;
    int shmid;
    key_t key;
    json_object *shm;

    key = 56789;

    if ((shmid = shmget(key, MAXSIZE, IPC_CREAT | 0666)) < 0)
        die("shmget");

    if ((shm = shmat(shmid, NULL, 0)) == (json_object *) -1)
        die("shmat");


	char *string = "{\"ame\" : \"joys of programming\"}";
	json_object * jobj = json_tokener_parse(string);
	enum json_type type = json_object_get_type(jobj);
	shm = jobj;
	printf("type %d \n", type);

    while (1) {}
}
// client.c
// compiled with gcc client.c -o client -ljson-c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
#include<json-c/json.h>
#define MAXSIZE 40 

void die(char *s) {
	perror(s);
	exit(0);
}

int main() {
	int shmid;
	key_t key;
	json_object *shm;
	key = 56789;
	if ((shmid = shmget(key, MAXSIZE, 0666)) < 0)
			die("shmget");
	if ((shm = shmat(shmid, NULL, 0)) == (json_object *) -1)
			die("shmat");

json_object * jobj = shm;
enum json_type type = json_object_get_type(jobj);
	printf("type %d \n", type);
}

First run the server and then run the client in an other terminal tab/window.
is this a problem with libjson-c?

@ploxiln
Copy link
Contributor

ploxiln commented May 3, 2018

Nope. This does not work at all: shm = jobj; - shm is the address of a chunk of memory which you have to copy data into.

Trying to put a tree of json_object structs with pointers between them into this chunk of memory is very difficult, I wouldn't try it. What you could do is copy the serialized json string into the shm memory chunk and decode it on the other side. But using shm is a whole separate topic.

@boddumanohar
Copy link
Author

Thankyou very much this solves my problem.

@hawicz
Copy link
Member

hawicz commented May 4, 2018

One way to do this, if you really wanted to, would be to replace the malloc functions with ones that can use a custom memory region instead of the heap, then have a way to only have those enabled during json-c calls. It'd be pretty hacky though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants