Changeset 5786

Show
Ignore:
Timestamp:
03/10/10 15:00:40 (3 years ago)
Author:
blogic
Message:

make unix socket work bi-directional, cleanups

Location:
luci2/cbi2
Files:
14 modified

Legend:

Unmodified
Added
Removed
  • luci2/cbi2/cbi.h

    r5785 r5786  
    170170 
    171171int cbi_dump_blob(char *file); 
    172 void cbi_request(struct cbi_ctx *ctx, char *request); 
    173172const char* cbi_virt_path(struct cbi_ctx *ctx, const char *path, const char *file); 
    174173void cbi_create_instance(struct cbi_ctx *ctx, struct cbi_element *e1, struct cbi_element *e2); 
  • luci2/cbi2/cbi_element.c

    r5785 r5786  
    173173        return 1; 
    174174    list_add_tail(&e->widget_ref, &e->w->refs); 
    175     if(widget_find_prop(e->w, "id") || (CAPS(e) & CAP_CONTAINER)) 
     175    if(widget_prop_find(e->w, "id") || (CAPS(e) & CAP_CONTAINER)) 
    176176        cbi_element_default_id(e, count); 
    177177    if(e->w->init) 
     
    348348} 
    349349 
    350 void cbi_request(struct cbi_ctx *ctx, char *request) 
    351 { 
    352     json_object *out, *in = json_tokener_parse(request); 
    353     if(is_error(in)) 
    354     { 
    355         printf("bad json\n"); 
    356         return; 
    357     } 
    358     out = json_request(ctx, in); 
    359     if(out) 
    360     { 
    361         printf("res -> %s\n", json_object_to_json_string(out)); 
    362         json_object_put(out); 
    363     } 
    364     json_object_put(in); 
    365 } 
     350 
  • luci2/cbi2/json.c

    r5781 r5786  
    7373    json_object *_pass; 
    7474    const char *pass; 
     75    if(!in) 
     76        return 0; 
    7577 
    7678    _pass = json_object_object_get(in, "pass"); 
     
    115117        if(!strcmp(r, "auth")) 
    116118            out = json_req_auth(ctx, vals); 
    117         else if(!strcmp(r, "logout")) 
     119        else if(!strcmp(r, "logout") && (sauth)) 
    118120            out = session_logout(ctx, json_object_get_string(sauth)); 
    119121        else if(!json_auth(ctx, sauth)) 
     
    133135} 
    134136 
     137char* u_sock_json_request(struct cbi_ctx *ctx, char *request) 
     138{ 
     139    json_object *out = 0, *in = json_tokener_parse(request); 
     140    char *answer = 0; 
     141    printf("in -> %s\n", request); 
     142    if(!is_error(in)) 
     143        out = json_request(ctx, in); 
     144    if(!out) 
     145    { 
     146        out = json_object_new_object(); 
     147        json_object_object_add(out, "error", json_object_new_string("1")); 
     148    } 
     149    answer = strdup(json_object_to_json_string(out)); 
     150    json_object_put(out); 
     151    json_object_put(in); 
     152    printf("out -> %s\n", answer); 
     153    return answer; 
     154} 
  • luci2/cbi2/json.h

    r5696 r5786  
    77json_object* json_request(struct cbi_ctx *ctx, json_object *in); 
    88json_object* json_req_node(struct cbi_ctx *ctx, const char *node); 
     9char* u_sock_json_request(struct cbi_ctx *ctx, char *request); 
    910 
    1011#endif 
  • luci2/cbi2/log.c

    r5784 r5786  
    88{ 
    99    daemonize = daemon; 
    10     openlog("luci2", 0, 0); 
     10    if(daemon) 
     11        openlog("luci2", 0, 0); 
    1112} 
    1213 
  • luci2/cbi2/luci-cli.c

    r5682 r5786  
    1717    int s, len; 
    1818    struct sockaddr_un remote; 
     19    char reply[4096]; 
    1920    if(argc != 2) 
    2021        return 1; 
     
    4142        exit(1); 
    4243    } 
     44    len = recv(s, reply, 4095, 0); 
     45    if(len) 
     46    { 
     47        reply[4095] = 0; 
     48        printf("received -> \"%s\"\n", reply); 
     49    } 
    4350    close(s); 
    4451    return 0; 
  • luci2/cbi2/luci.c

    r5784 r5786  
    169169        if(spath) 
    170170            free(spath); 
    171         u_sock_register_handler(ctx, "json", cbi_request); 
     171        u_sock_register_handler(ctx, "json", u_sock_json_request); 
    172172    } 
    173173    { 
  • luci2/cbi2/Makefile

    r5784 r5786  
    3535 
    3636run: $(BINARY) 
    37     mkdir -p ./root/lib/luci2/ ./root/etc/config/ 
     37    mkdir -p ./root/lib/luci2/ ./root/etc/config/ ./root/var/state/ ./root/tmp/.uci/ 
    3838    cp apps/*.luci ./root/lib/luci2/ 
    3939    cp -r ./config/* ./root/etc/config/ 
    4040    ./lmo_po2lmo ./apps/en.po ./root/lib/luci2/en.lmo 
    4141    ./uvlc ./apps/network.uvl ./root/lib/luci2/test.uvo 
    42     ./luci -s -V ./root 
     42    ./luci -s -V ${PWD}/root 
    4343 
    4444%.o: %.c 
  • luci2/cbi2/session.c

    r5685 r5786  
    4444    ucix_add_option_int(ctx, "luci", "session", session, time(0)); 
    4545    ucix_save(ctx, "luci"); 
    46     ucix_cleanup(ctx); 
     46    ucix_cleanup(ctx); 
    4747    return session; 
    4848} 
     
    6363    struct uci_context *ctx; 
    6464    ctx = ucix_init(cbictx, "luci", 1); 
    65     uci_set_savedir(ctx, "/var/state/"); 
    6665    ucix_for_each_section_option(ctx, "luci", "session", session_expire_cb, ctx); 
    6766    ucix_save(ctx, "luci"); 
     
    7776        goto out; 
    7877    ctx = ucix_init(cbictx, "luci", 1); 
    79     uci_set_savedir(ctx, "/var/state/"); 
    8078    t = ucix_get_option_int(ctx, "luci", "session", auth, 0); 
    8179    if(t) 
     
    10199        goto out; 
    102100    ctx = ucix_init(cbictx, "luci", 1); 
    103     uci_set_savedir(ctx, "/var/state/"); 
    104101    t = ucix_get_option_int(ctx, "luci", "session", sauth, 0); 
    105102    if(t) 
  • luci2/cbi2/uci.c

    r5694 r5786  
    2323        uci_set_confdir(ctx, cbi_virt_path(cbictx, "/etc/config", NULL)); 
    2424    if(state) 
    25         uci_add_history_path(ctx, "/var/state"); 
     25        uci_set_savedir(ctx, cbi_virt_path(cbictx, "/var/state/", NULL)); 
     26    else 
     27        uci_set_savedir(ctx, cbi_virt_path(cbictx, "/tmp/.uci/", NULL)); 
    2628    if(uci_load(ctx, config_file, NULL) != UCI_OK) 
    2729    { 
  • luci2/cbi2/u_sock.c

    r5682 r5786  
    6363} 
    6464 
    65 void u_sock_incoming(struct cbi_ctx *ctx, char *data) 
     65char* u_sock_incoming(struct cbi_ctx *ctx, char *data) 
    6666{ 
    6767    char *t = strtok(data, "|"); 
     
    7373            struct sock_handler *sh = container_of(p, struct sock_handler, list); 
    7474            if(!strcmp(t, sh->name)) 
    75                 sh->cb(ctx, &data[strlen(sh->name) + 1]); 
     75            { 
     76                char *answer = sh->cb(ctx, &data[strlen(sh->name) + 1]); 
     77                if(answer) 
     78                    return answer; 
     79                break; 
     80            } 
    7681        } 
    7782    } 
     83    return 0; 
    7884} 
    7985 
     
    95101    if(n > 0) 
    96102    { 
     103        char *answer; 
    97104        str[n] = '\0'; 
    98         printf("incoming data -> %s\n", str); 
    99         u_sock_incoming(ctx, str); 
     105        answer = u_sock_incoming(ctx, str); 
     106        if(answer) 
     107        { 
     108            send(s, answer, strlen(answer), 0); 
     109            free(answer); 
     110        } 
    100111    } 
    101112    close(s); 
  • luci2/cbi2/u_sock.h

    r5682 r5786  
    33#include "cbi.h" 
    44 
    5 typedef void (*sock_handler_func)(struct cbi_ctx *ctx, char *data); 
     5typedef char* (*sock_handler_func)(struct cbi_ctx *ctx, char *data); 
    66 
    77void u_sock_init(struct cbi_ctx *ctx, const char *path); 
  • luci2/cbi2/widget.c

    r5781 r5786  
    2929} 
    3030 
    31 int widget_find_prop(struct cbi_widget *w, const char *name) 
     31int widget_prop_find(struct cbi_widget *w, const char *name) 
    3232{ 
    3333    int i; 
     
    5959    { 
    6060        struct cbi_property *p = container_of(q, struct cbi_property, list); 
    61         if(!widget_find_prop(e->w, p->key)) 
     61        if(!widget_prop_find(e->w, p->key)) 
    6262        { 
    6363            CBILOG(e, "unknown property %s\n", p->key); 
     
    7878} 
    7979 
    80 static int widget_verify_parent_caps(struct cbi_element *e) 
     80static int widget_verify_parent(struct cbi_element *e) 
    8181{ 
    8282    struct cbi_element *parent; 
     
    9696} 
    9797 
    98 static int widget_abstract(struct cbi_ctx *ctx, struct cbi_element *e) 
     98static int widget_verify_abstract(struct cbi_ctx *ctx, struct cbi_element *e) 
    9999{ 
    100100    const char *ref = cbi_prop_find(e, "ref"); 
     
    115115int widget_verify(struct cbi_ctx *ctx, struct cbi_element *e) 
    116116{ 
    117     if(widget_abstract(ctx, e)) 
     117    if(widget_verify_abstract(ctx, e)) 
    118118        return 1; 
    119     if(widget_verify_parent_caps(e)) 
     119    if(widget_verify_parent(e)) 
    120120        return 1; 
    121121    if(widget_verify_props(e)) 
    122122        return 1; 
    123     if(e->w->caps & CAP_DATA) 
     123    if(CAPS(e) & CAP_DATA) 
    124124        if(source_init_element(ctx, e)) 
    125125        return 1; 
  • luci2/cbi2/widget.h

    r5779 r5786  
    55struct cbi_widget* widget_find(const char *namespace, const char *name); 
    66int widget_verify(struct cbi_ctx *ctx, struct cbi_element *e); 
    7 int widget_find_prop(struct cbi_widget *w, const char *name); 
     7int widget_prop_find(struct cbi_widget *w, const char *name); 
    88 
    99#endif