summaryrefslogtreecommitdiff
path: root/src/lscf.c
blob: cf4e66be4cc040223028f62c0ea79e907fb09e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <stdio.h>
#if defined(NV_SUNOS)

/* Interface to the Solaris Service Management Facility.
 * This facility is responsible for running programs and services
 * and store their configuration informations (named properties)
 * The configuration informations for the X server are managed by 
 * this facility. The functions in this source file use the library 
 * libscf (Service Configuration Facility) to access and modify 
 * the properties for the X server, more specifically the default depth. 
 * On Solaris, changing the default depth in the xorg.conf file is not
 * enough. The session manager overrides the xorg.conf default depth:
 * it passes the option -defdepth  to the X server with the value 
 * retrieved from the Service Management Facility.
 *
 * For more information refer to the manpages of fmf(5), libsfc(3LIB),
 * and to the source code of svccfg(1M) available on cvs.opensolaris.org.
 */


#include <libscf.h>

static int lscf_init_handle(scf_handle_t **scf_handle, 
                            scf_scope_t **scf_scope);
static int lscf_select(scf_handle_t *scf_handle, 
                       scf_scope_t *scf_scope,
                       const char *selection,
                       scf_service_t **current_svc);
static int lscf_setprop_int(scf_handle_t *scf_handle, 
                            scf_scope_t *scf_scope,
                            scf_service_t *current_svc,
                            const char *group, 
                            const char *name, int value);

/* UPDATE THE DEFAULT DEPTH PROPERTY IN SMF WITH THE LIBSCF FUNCTIONS */
int update_scf_depth(int depth) 
{  
    static scf_handle_t *scf_handle = NULL;
    static scf_scope_t *scf_scope  = NULL;
    scf_service_t       *curren_svc = NULL;
    int status = 1;
    
    // Initialization of the handles
    lscf_init_handle(&scf_handle, &scf_scope);
    if (scf_handle == NULL) {
        status =0;
        goto done;
    }
    
    // Set the current selection
    if(!lscf_select(scf_handle, scf_scope, "application/x11/x11-server",
                    &curren_svc)) {
        status =0;
        goto done;
    }
    
    // Set the depth property of the current selection
    if(!lscf_setprop_int(scf_handle, scf_scope, curren_svc,
                         "options", "default_depth", depth)) {
        status =0;
        goto done;
    }

done:
    if(curren_svc)  scf_service_destroy(curren_svc);
    if(scf_scope)       scf_scope_destroy(scf_scope);    
    if(scf_handle) {
                    scf_handle_unbind(scf_handle);
                    scf_handle_destroy(scf_handle);
    }
    if (!status) {
        fmterr("Unable to set X server default depth through "
               "Solaris Service Management Facility");
    }
    return status;
}

/* INITIALIZATION OF THE HANDLES */
static int lscf_init_handle(scf_handle_t **scf_handle, 
                            scf_scope_t **scf_scope) 
{
    scf_handle_t *handle = NULL;
    scf_scope_t *scope = NULL;;
    
    *scf_handle = NULL;
    *scf_scope = NULL;
    
    // Create a new Service Configuration Facility
    // handle, needed for the communication with the
    // configuration repository.
    handle = scf_handle_create(SCF_VERSION);
    if (handle == NULL) {
        return 0;
    }
    
    // Bind the handle to the running svc.config daemon
    if (scf_handle_bind(handle) != 0) {
        scf_handle_destroy(handle);
        return 0;
    }
    
    
    // Allocate a new scope. A scope is a top level of the 
    // SCF repository tree.  
    scope = scf_scope_create(handle);
    if (scope == NULL) {
        scf_handle_unbind(handle);
        scf_handle_destroy(handle);
        return 0;
    }
    
    // Set the scope to the root of the local SCF repository tree.
    if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) !=0) {
        scf_scope_destroy(scope);
        scf_handle_unbind(handle);
        scf_handle_destroy(handle);
        return 0;
    }
    
    *scf_handle = handle;
    *scf_scope = scope;
    
    return 1;
}


/* EQUIVALENT TO THE SVCCFG SELECT COMMAND */
static int lscf_select(scf_handle_t *scf_handle, 
                       scf_scope_t *scf_scope,
                       const char *selection,
                       scf_service_t **current_svc) 
{
    scf_service_t *svc;
    
    // Services are childrens of a  scope, and 
    // contain configuration informations for 
    // the service. 
    svc = scf_service_create(scf_handle);
    if (svc == NULL) {
        return 0;
    }

    // Set the service 'svc' to the service specified
    // by 'selection', in the scope 'scf_scope'.
    if (scf_scope_get_service(scf_scope, selection, svc) == SCF_SUCCESS) {
        *current_svc = svc;
        return 1;
    }
    
    scf_service_destroy(svc);
    return 0;
}

/* EQUIVALENT TO THE SVCCFG SETPROP COMMAND FOR AN INTEGER TYPED VALUE */
static int lscf_setprop_int(scf_handle_t *scf_handle, 
                            scf_scope_t *scf_scope,
                            scf_service_t *current_svc,
                            const char *group, 
                            const char *name, int value) 
{
    scf_transaction_entry_t *entry=NULL;
    scf_propertygroup_t *pg = NULL;
    scf_property_t *prop = NULL;
    scf_transaction_t *transax = NULL;
    scf_value_t *v = NULL;
    int status = 1;
    
    // Allocate a new transaction entry handle
    entry = scf_entry_create(scf_handle);
    if (entry == NULL) {
        status=0;
        goto done;
    }
    
    // Allocate a property group. 
    pg = scf_pg_create(scf_handle);
    if (pg == NULL) {
        status=0;
        goto done;
    }
    
    // Allocate a property. A property is a named set
    // of values.
    prop = scf_property_create(scf_handle);
    if (prop == NULL) {
        status=0;
        goto done;
    }
    
    // Allocate a transaction, used to change 
    // property groups.
    transax = scf_transaction_create(scf_handle);
    if (transax == NULL) {
        status=0;
        goto done;
    }
    
    // Allocate a value.
    v = scf_value_create(scf_handle);
    if (v == NULL) {
        status=0;
        goto done;
    }
    
    // Set the the property group 'pg' to the 
    // groups specified by 'group' in the service
    // specified by 'current_svc'
    if (scf_service_get_pg(current_svc, group, pg) != SCF_SUCCESS) {
        status=0;
        goto done;
    }
    
    // Update the property group.
    if (scf_pg_update(pg) == -1) {
        status=0;
        goto done;
    }
    
    // Set up the transaction to modify the property group.
    if (scf_transaction_start(transax, pg) != SCF_SUCCESS) {
        status=0;
        goto done;
    }
    
    // Set the property 'prop' to the property 
    // specified ny 'name' in the property group 'pg'
    if (scf_pg_get_property(pg, name, prop) == SCF_SUCCESS) {
        // Found 
        // It should be already of integer type.
        // To be secure, reset the property type to integer.
        if (scf_transaction_property_change_type(transax, entry,
            name, SCF_TYPE_INTEGER) == -1) {
            status=0;
            goto done;
        }      
    } else {
        // Not found
        // Add a new property to the property group.
        if (scf_transaction_property_new(transax, entry, 
                                         name, SCF_TYPE_INTEGER) 
            == -1) {
            status=0;
            goto done;
        }      
    }
    
    // Set the integer value
    scf_value_set_integer(v, value);
    
    // Set up the value to the property.
    if (scf_entry_add_value(entry, v) != SCF_SUCCESS) {
        status=0;
        goto done;
    }   
    
    // Commit the transaction
    if (scf_transaction_commit(transax) < 0) {
        status=0;
    }   
    
done:
    if (entry)      scf_entry_destroy(entry);
    if (pg)         scf_pg_destroy(pg);
    if (prop)       scf_property_destroy(prop);
    if (transax)    scf_transaction_destroy(transax);
    if (v)          scf_value_destroy(v);
    return status;
}

#else // NOT SOLARIS
int update_scf_depth(int depth) 
{
    return 1;
}
#endif