summaryrefslogtreecommitdiff
path: root/lscf.c
blob: 19f4e72d9cff14c9399685e04b8dc355a09747c8 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include <stdio.h>
#include "nvidia-xconfig.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);
static int lscf_getprop_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;
}

/* READ THE DEFAULT DEPTH PROPERTY FROM SMF WITH THE LIBSCF FUNCTIONS */
int read_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;
    }
    
    // Get the depth property of the current selection
    if(!lscf_getprop_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 get X server default depth from "
               "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;
}

/* EQUIVALENT TO THE SVCCFG SETPROP COMMAND FOR AN INTEGER TYPED VALUE */
static int lscf_getprop_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_value_t *v = NULL;
    int status = 1;
    int64_t t;
    
    // 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;
    }
       
    // 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 the property 'prop' to the property 
    // specified ny 'name' in the property group 'pg'
    if (scf_pg_get_property(pg, name, prop) != SCF_SUCCESS) {
        status=0;
        goto done;
    }

    // Allocate a value.
    v = scf_value_create(scf_handle);
    if (v == NULL) {
        status=0;
        goto done;
    }
    
    // Get the value
    if (scf_property_get_value(prop, v) != SCF_SUCCESS) {
        status=0;
        goto done;
    }   
    
    // Get the integer value
    if (scf_value_get_integer(v, &t) != SCF_SUCCESS) {
        status=0;
        goto done;
    }
    
    *value = (int)t;
    
done:
    if (entry)      scf_entry_destroy(entry);
    if (pg)         scf_pg_destroy(pg);
    if (prop)       scf_property_destroy(prop);
    if (v)          scf_value_destroy(v);
    return status;
}
#else // NOT SOLARIS
int update_scf_depth(int depth) 
{
    return 1;
}

int read_scf_depth(int *depth) 
{
    return 0;
}
#endif