packaging: bump version to 0.9.23, update changelog
[profile/ivi/ico-uxf-homescreen.git] / tool / ico_change_loginuser.c
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   Login Tool to change login user
11  *
12  * @date    Sep-30-2013
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include <ico_syc_common.h>
21 #include <ico_syc_error.h>
22 #include <ico_syc_userctl.h>
23 #include <ico_log.h>
24
25 /*============================================================================*/
26 /* Function prototype for static(internal) functions                          */
27 /*============================================================================*/
28 static void _sysctl_callback(const ico_syc_ev_e event, const void *detail,
29                              void *user_data);
30
31 /*============================================================================*/
32 /* Tables and Variables                                                       */
33 /*============================================================================*/
34 /* original stdout file discriptor  */
35 static FILE *org_stdout;
36 /* new user name    */
37 static char newuser[128];
38 /* program name     */
39 static char progname[128];
40
41 /*============================================================================*/
42 /* Function                                                                   */
43 /*============================================================================*/
44 /**
45  * @brief _sysctl_callback
46  */
47 static void
48 _sysctl_callback(const ico_syc_ev_e event, const void *detail, void *user_data)
49 {
50     ico_syc_userlist_t  *userlist;
51     int     i;
52
53     switch (event) {
54     case ICO_SYC_EV_USERLIST:
55         userlist = (ico_syc_userlist_t *)detail;
56
57         fprintf(org_stdout, "%s: # of users = %d\n", progname, userlist->user_num);
58         ICO_INF("%s: # of users = %d", progname, userlist->user_num);
59         for (i = 0; i < userlist->user_num; i++) {
60             if (strcmp(userlist->user_login, userlist->userlist[i]))    {
61                 fprintf(org_stdout, "%s:%2d. %s *\n", progname, i+1, userlist->userlist[i]);
62                 ICO_INF("%s:%2d. %s *", progname, i+1, userlist->userlist[i]);
63             }
64             else    {
65                 fprintf(org_stdout, "%s:%2d. %s\n", progname, i+1, userlist->userlist[i]);
66                 ICO_INF("%s:%2d. %s", progname, i+1, userlist->userlist[i]);
67             }
68         }
69         (void)ico_syc_disconnect();
70         ico_log_close();
71         break;
72     case ICO_SYC_EV_AUTH_FAIL:
73         fprintf(org_stdout, "%s: fail to authenticate user<%s>\n", progname, newuser);
74         ICO_ERR("%s: fail to authenticate user<%s>", progname, newuser);
75         (void)ico_syc_disconnect();
76         ico_log_close();
77         break;
78     default:
79         ICO_WRN("other event (%d)", event);
80         break;
81     }
82 }
83
84 /**
85  * @brief main
86  */
87 int
88 main(int argc, char *argv[])
89 {
90     int retry;
91     int ret;
92
93     strncpy(progname, argv[0], sizeof(progname));
94     progname[sizeof(progname)-1] = 0;
95
96     /* setting the log output       */
97     org_stdout = stdout;
98     ico_log_open("ico_change_loginuser");
99
100     memset(newuser, 0, sizeof(newuser));
101     if (argc > 1)   {
102         strncpy(newuser, argv[1], sizeof(newuser)-1);
103         ICO_INF("%s: change user to <%s>", progname, newuser);
104     }
105     else    {
106         ICO_INF("%s: user list", progname);
107     }
108
109     /* connect to SystemController  */
110     ret = ico_syc_connect(_sysctl_callback, NULL);
111     if (ret != ICO_SYC_ERR_NONE) {
112         ICO_WRN("%s: ico_syc_connect failed (ret: %d)", progname, ret);
113         ico_log_close();
114         fprintf(org_stdout, "%s: ico_syc_connect failed (ret: %d)\n", progname, ret);
115         exit(1);
116     }
117     /* connect wait 200 ms          */
118     for (retry = 0; retry < (200/20); retry++) {
119         /* service for conection of SystemController    */
120         ico_syc_service();
121         usleep(20*1000);
122     }
123     if (argc <= 1)  {
124         /* get userlist             */
125         (void)ico_syc_get_userlist();
126     }
127     else    {
128         /* change login user        */
129         if (strcasecmp(newuser, "logoff") == 0) {
130             strcpy(newuser, " ");
131         }
132         (void)ico_syc_change_user(newuser, "\0");
133     }
134
135     /* wait 300 ms                  */
136     for (retry = 0; retry < (300/20); retry++)  {
137         /* service for send/receive */
138         ico_syc_service();
139         usleep(20*1000);
140     }
141
142     (void)ico_syc_disconnect();
143     ico_log_close();
144     return 0;
145 }