19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* Copyright 2008 The Android Open Source Project
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdio.h>
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdlib.h>
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <errno.h>
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "binder.h"
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid *svcmgr_lookup(struct binder_state *bs, void *target, const char *name)
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void *ptr;
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    unsigned iodata[512/4];
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    struct binder_io msg, reply;
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bio_init(&msg, iodata, sizeof(iodata), 4);
1727b3a7a75960d026655f3acd1de8a672e80a5776Brad Fitzpatrick    bio_put_uint32(&msg, 0);  // strict mode header
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bio_put_string16_x(&msg, SVC_MGR_NAME);
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bio_put_string16_x(&msg, name);
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (binder_call(bs, &msg, &reply, target, SVC_MGR_CHECK_SERVICE))
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ptr = bio_get_ref(&reply);
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (ptr)
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        binder_acquire(bs, ptr);
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    binder_done(bs, &msg, &reply);
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return ptr;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint svcmgr_publish(struct binder_state *bs, void *target, const char *name, void *ptr)
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    unsigned status;
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    unsigned iodata[512/4];
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    struct binder_io msg, reply;
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bio_init(&msg, iodata, sizeof(iodata), 4);
4127b3a7a75960d026655f3acd1de8a672e80a5776Brad Fitzpatrick    bio_put_uint32(&msg, 0);  // strict mode header
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bio_put_string16_x(&msg, SVC_MGR_NAME);
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bio_put_string16_x(&msg, name);
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bio_put_obj(&msg, ptr);
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (binder_call(bs, &msg, &reply, target, SVC_MGR_ADD_SERVICE))
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    status = bio_get_uint32(&reply);
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    binder_done(bs, &msg, &reply);
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return status;
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectunsigned token;
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint main(int argc, char **argv)
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int fd;
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    struct binder_state *bs;
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void *svcmgr = BINDER_SERVICE_MANAGER;
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bs = binder_open(128*1024);
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    argc--;
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    argv++;
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    while (argc > 0) {
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!strcmp(argv[0],"alt")) {
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            void *ptr = svcmgr_lookup(bs, svcmgr, "alt_svc_mgr");
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (!ptr) {
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                fprintf(stderr,"cannot find alt_svc_mgr\n");
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return -1;
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            svcmgr = ptr;
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr,"svcmgr is via %p\n", ptr);
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (!strcmp(argv[0],"lookup")) {
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            void *ptr;
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (argc < 2) {
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                fprintf(stderr,"argument required\n");
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return -1;
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ptr = svcmgr_lookup(bs, svcmgr, argv[1]);
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr,"lookup(%s) = %p\n", argv[1], ptr);
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            argc--;
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            argv++;
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (!strcmp(argv[0],"publish")) {
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (argc < 2) {
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                fprintf(stderr,"argument required\n");
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return -1;
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            svcmgr_publish(bs, svcmgr, argv[1], &token);
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            argc--;
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            argv++;
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr,"unknown command %s\n", argv[0]);
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return -1;
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        argc--;
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        argv++;
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return 0;
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
104